import { componentName } from '../src/components/phone-fields/descope-phone-input-box-field';
import CountryCodes from '../src/components/phone-fields/CountryCodes';
import { toggleDefaultErrorMessageValueMissingDecorator, withForm } from './helpers';
import {
  labelControl,
  sizeControl,
  fullWidthControl,
  directionControl,
  disabledControl,
  readOnlyControl,
  requiredControl,
  borderedControl,
  minLengthControl,
  maxLengthControl,
  errorMissingValueControl,
  inputLabelTypeControl,
  errorMessageIconControl,
  errorMessageIconAttrs,
  errorPatternMismatchControl,
  defaultValueMissingControl,
} from './commonControls';

const Template = ({
  label,
  size,
  bordered,
  direction,
  'phone-input-placeholder': placeholder,
  minlength,
  maxlength,
  readonly,
  required,
  disabled,
  'default-code': defaultCode,
  'full-width': fullWidth,
  'restrict-countries': restrictCountries,
  'data-errormessage-value-missing': customErrorMissingValue,
  'data-errormessage-pattern-mismatch': customPatternMismatch,
  'label-type': labelType,
  'allow-alphanumeric-input': allowAlphanumericInput,
  'format-value': formatValue,
  'strict-validation': strictValidation,
  errorMsgIcon,
  'phone-input-type': phoneInputType,
}) =>
  `<descope-phone-input-box-field
    size="${size}"
    bordered="${bordered}"
    label="${label || ''}"
    required="${required || false}"
    phone-input-placeholder="${placeholder || ''}"
    default-code="${defaultCode || ''}"
    full-width="${fullWidth || false}"
    readonly="${readonly || false}"
    disabled="${disabled || false}"
    minlength="${minlength}"
    maxlength="${maxlength}"
    restrict-countries="${restrictCountries || ''}"
    data-errormessage-value-missing="${customErrorMissingValue || ''}"
    data-errormessage-pattern-mismatch="${customPatternMismatch || ''}"
    allow-alphanumeric-input="${allowAlphanumericInput ? 'true' : 'false'}"
    st-host-direction="${direction ?? ''}"
    label-type="${labelType || ''}"
    format-value="${formatValue || false}"
    strict-validation="${strictValidation || false}"
    ${errorMsgIcon ? errorMessageIconAttrs : ''}
    phone-input-type="${phoneInputType ?? ''}"
  ></descope-phone-input-box-field>`;
export default {
  component: componentName,
  title: 'descope-phone-input-box-field',
  decorators: [toggleDefaultErrorMessageValueMissingDecorator, withForm],
  argTypes: {
    ...labelControl,
    ...sizeControl,
    ...inputLabelTypeControl,
    ...fullWidthControl,
    ...disabledControl,
    ...readOnlyControl,
    ...requiredControl,
    ...borderedControl,
    ...minLengthControl,
    ...maxLengthControl,
    ...errorMissingValueControl,
    ...errorPatternMismatchControl,
    ...directionControl,
    ...errorMessageIconControl,
    ...defaultValueMissingControl,
    'phone-input-placeholder': {
      name: 'Phone Placeholder',
      control: { type: 'text' },
    },
    'default-code': {
      name: 'Default Code',
      options: [''].concat(CountryCodes.map((c) => c.code)),
      control: { type: 'select' },
    },
    'restrict-countries': {
      name: 'Allowed Countries',
      control: { type: 'text' },
    },
    'allow-alphanumeric-input': {
      control: { type: 'boolean' },
    },
    'format-value': {
      name: 'Format Value',
      control: { type: 'boolean' },
    },
    'strict-validation': {
      name: 'Strict Validation',
      control: { type: 'boolean' },
    },
    'phone-input-type': {
      name: 'Phone Input Type',
      control: { type: 'select' },
      options: ['tel', 'text'],
    },
  },
};

export const Default = Template.bind({});

Default.args = {
  size: 'md',
  bordered: true,
  'data-errormessage-value-missing': 'Please fill out this field.',
  'data-errormessage-pattern-mismatch': 'Please match the requested format.',
};
