import { faker } from '@faker-js/faker';
import { action } from '@storybook/addon-actions';
import { Meta, StoryObj } from '@storybook/react';
import React, { useState } from 'react';
import { createGroups } from '../../../helpers/storybook';
import { DateTimeText } from './DateTimeText';

const groups = createGroups({
  Behavior: [
    'autoComplete',
    'autoFocus',
    'disabled',
    'inlineMode',
    'name',
    'id',
    'modifyTime',
  ],
  Content: ['label', 'placeholder', 'error', 'tooltipContent', 'value'],
  Styling: ['className'],
  Events: ['onChange'],
});

const meta: Meta<typeof DateTimeText> = {
  title: 'Primary Components/Form Elements/DateTime',
  component: DateTimeText,
  argTypes: {
    ...groups,
  },
};
export default meta;

export const Main: StoryObj<typeof DateTimeText> = {
  args: {
    label: 'Start',
    name: 'start',
    tooltipContent: faker.lorem.sentences(4),
  },
  render: (args) =>
    React.createElement(() => {
      const [value, setValue] = useState(args.value);
      const [error, setError] = useState(args.error);

      return (
        <DateTimeText
          {...args}
          error={error}
          value={value}
          onChange={(value, valid) => {
            action('onChange')(value);
            setValue(value as string);
            setError(!valid ? 'Invalid Date' : undefined);
          }}
        />
      );
    }),
};
