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 { SingleLineText } from './SingleLineText';

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

const meta: Meta<typeof SingleLineText> = {
  title: 'Primary Components/Form Elements/SingleLineText',
  component: SingleLineText,
  argTypes: {
    ...groups,
    isSet: {
      ...groups.isSet,
      if: { arg: 'type', eq: 'password' },
    },
  },
};
export default meta;

export const Main: StoryObj<typeof SingleLineText> = {
  args: {
    name: 'name',
    label: 'Name',
    placeholder: 'Enter Name...',
    tooltipContent: faker.lorem.paragraph(2),
  },
  render: (args) =>
    React.createElement(() => {
      const [value, setValue] = useState(args.value);

      return (
        <SingleLineText
          {...args}
          value={value}
          onChange={(e) => {
            action('onChange')(e);
            setValue(e.currentTarget.value);
          }}
        />
      );
    }),
};
