import { componentName } from '../src/components/button-selection-group-fields/descope-button-multi-selection-group';
import {
  sizeControl,
  labelControl,
  requiredControl,
  readOnlyControl,
  helperTextControl,
  disabledControl,
  minItemsSelectionControl,
  maxItemsSelectionControl,
  errorMissingValueControl,
  fullWidthControl,
  directionControl,
  overrideRenderItemControl,
  itemsSourceControl,
  errorMessageIconControl,
  errorMessageIconAttrs,
  defaultValueMissingControl,
} from './commonControls';
import { toggleDefaultErrorMessageValueMissingDecorator, withForm } from './helpers';

const altRenderItem = ({ label, value }) => `<span value="${value}">${label}</span>`;

const Template = ({
  size,
  label,
  required,
  readonly,
  'full-width': fullWidth,
  direction,
  disabled,
  minItemsSelection,
  maxItemsSelection,
  'helper-text': helperText,
  'default-values': defaultValues,
  'data-errormessage-value-missing': customErrorMissingValue,
  data,
  itemsSource,
  errorMsgIcon,
}) => {
  let serializedData;
  let serializedDefaultValues;

  try {
    serializedData = JSON.stringify(data);
    serializedDefaultValues = JSON.stringify(defaultValues);
  } catch (e) {
    // do nothing
  }

  return `
  <descope-button-multi-selection-group
    data='${(itemsSource === 'attr' && serializedData) || ''}'
    full-width="${fullWidth || false}"
    size="${size || ''}"
    label="${label || ''}"
    helper-text="${helperText || ''}"
    default-values='${serializedDefaultValues || ''}'
    required="${required || false}"
    readonly="${readonly || false}"
    disabled="${disabled || false}"
    data-errormessage-value-missing="${customErrorMissingValue || ''}"
    st-host-direction="${direction ?? ''}"
    min-items-selection="${minItemsSelection || ''}"
    max-items-selection="${maxItemsSelection || ''}"
    ${errorMsgIcon ? errorMessageIconAttrs : ''}
  >
    <descope-button-selection-group-item value="1">item1</descope-button-selection-group-item>
    <descope-button-selection-group-item value="2">test test test test test test test test</descope-button-selection-group-item>
    <descope-button-selection-group-item value="3">item2</descope-button-selection-group-item>
    <descope-button-selection-group-item value="4">another item</descope-button-selection-group-item>
  </descope-button-multi-selection-group>
`;
};

export default {
  component: componentName,
  title: 'descope-button-multi-selection-group',
  decorators: [
    (story, { args }) => {
      setTimeout(() => {
        const ele = document.querySelector('descope-button-multi-selection-group');

        if (args.itemsSource === 'prop') ele.data = args.data;
        if (args.overrideRenderItem) ele.renderItem = altRenderItem;
      });
      return story();
    },
    toggleDefaultErrorMessageValueMissingDecorator,
    withForm,
  ],
  argTypes: {
    ...sizeControl,
    ...labelControl,
    ...readOnlyControl,
    ...requiredControl,
    ...helperTextControl,
    ...disabledControl,
    ...errorMissingValueControl,
    ...fullWidthControl,
    ...minItemsSelectionControl,
    ...maxItemsSelectionControl,
    'default-values': {
      name: 'Default Values',
      control: { type: 'object' },
    },
    readonly: {
      name: 'Read Only',
      control: { type: 'boolean' },
    },
    ...overrideRenderItemControl,
    ...itemsSourceControl,
    ...directionControl,
    ...errorMessageIconControl,
    ...defaultValueMissingControl,
  },
};

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

Default.args = {
  data: [
    { value: 'itemId1', label: 'data item 1' },
    { value: 'itemId2', label: 'data item 2' },
    { value: 'itemId3', label: 'data item 3' },
    { value: 'itemId4', label: 'data item 4' },
  ],
  itemsSource: 'children',
  'data-errormessage-value-missing': 'Please fill out this field.',
};
