import * as React from 'react';
import type {Meta} from '@storybook/react';
import {TextControl} from '@tesseract-design/web-base';
import {MenuSelect as Component, MenuSelectProps as Props} from '.';

const meta: Meta<typeof Component> = {
	component: Component,
	argTypes: {
		label: {
			control: { type: 'text' },
		},
		hint: {
			control: { type: 'text' },
		},
		variant: {
			control: {
				type: 'select',
			},
			options: TextControl.AVAILABLE_VARIANTS,
		},
		size: {
			control: {
				type: 'select',
			},
			options: TextControl.AVAILABLE_SIZES,
		},
		indicator: {
			control: { type: 'text' },
		},
		children: {
			control: { type: 'text' },
		},
		onChange: {
			table: {
				disable: true,
			},
			action: 'changed',
		},
	},
	args: {
		...(Component.defaultProps ?? {}),
		label: Component.displayName,
		children: `Chocolate
Vanilla
Mango
Strawberry
`,
	},
};

export const MenuSelect = ({ children = '', ...args }: Omit<Props, 'ref'>) => (
	<Component
		{...args}
	>
		{(children ? children.toString() : '').split('\n').filter(s => s.length > 0).map((s, i) => {
			const [label, value = label] = s.split(':').map(s => s.trim()) as string[];
			return (
				<option
					key={`${s}:${i}`}
					value={value}
				>
					{label}
				</option>
			);
		})}
	</Component>
);

export default meta;
