import { componentName } from '../src/components/mapping-fields/descope-saml-group-mappings';
import { withForm } from './helpers';
import {
  sizeControl,
  fullWidthControl,
  disabledControl,
  directionControl,
  readOnlyControl,
  errorPatternMismatchControl,
  errorMessageIconAttrs,
  errorMessageIconControl,
} from './commonControls';

const Template = ({
  size,
  variant,
  'full-width': fullWidth,
  disabled,
  direction,
  'label-group': labelGroup,
  'label-value': labelValue,
  'label-attr': labelAttr,
  'button-label': buttonLabel,
  separator,
  options,
  readonly,
  'data-errormessage-pattern-mismatch': customErrorPatternMismatch,
  errorMsgIcon,
}) => {
  let serializedOptions;

  try {
    serializedOptions = JSON.stringify(options);
  } catch (e) {
    // do nothing
  }

  return `
  <descope-saml-group-mappings
    size="${size}"
    variant="${variant}"
    disabled="${disabled || false}"
    readonly="${readonly || false}"
    full-width="${fullWidth || false}"
    st-host-direction="${direction ?? ''}"
    options='${serializedOptions || ''}'
    button-label="${buttonLabel}"
    separator="${separator}"
    label-group="${labelGroup}"
    label-value="${labelValue}"
    label-attr="${labelAttr}"
    data-errormessage-pattern-mismatch="${customErrorPatternMismatch || ''}"
    ${errorMsgIcon ? errorMessageIconAttrs : ''}
  >
  </descope-saml-group-mappings>`;
};

export default {
  component: componentName,
  title: 'descope-saml-group-mappings',
  decorators: [withForm],
  argTypes: {
    'label-group': {
      name: 'Label Group Value',
      control: { type: 'text' },
    },
    'label-value': {
      name: 'Label Value',
      control: { type: 'text' },
    },
    'label-attr': {
      name: 'Label Attribute',
      control: { type: 'text' },
    },
    'button-label': {
      name: 'Button Label',
      control: { type: 'text' },
    },
    separator: {
      name: 'Separator',
      control: { type: 'text' },
    },
    ...sizeControl,
    ...fullWidthControl,
    ...disabledControl,
    ...readOnlyControl,
    ...directionControl,
    ...errorPatternMismatchControl,
    ...errorMessageIconControl,
  },
};

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

Default.args = {
  size: 'md',
  'full-width': false,
  'label-group': 'Group Attribute Name',
  'label-value': 'IdP Group Name',
  'label-attr': 'Role',
  'button-label': 'Add group mapping',
  separator: 'map to',
  options: [
    { value: 'itemId1', label: 'item 1' },
    { value: 'itemId2', label: 'item 2' },
    { value: 'itemId3', label: 'item 3' },
    { value: 'itemId4', label: 'item 4' },
  ],
};
