import {
  FIELD_CHECKBOX,
  FIELD_DATE,
  FIELD_EMAIL,
  FIELD_RADIO,
  FIELD_SELECT,
  FIELD_TEXT,
  FIELD_TEXTAREA,
} from '@/modules/forms/editor/constants';

const createBase = (component, props = {}) => ({
  id: props.name || `name-${Date.now()}`,
  isSelected: props.isSelected || false,
  component,
  value: '',
  placeholder: 'Enter Label *',
  isRequire: props.validation ? props.validation?.required?.value : true,
  isTypeDisabled: props.isTypeDisabled || false,
});

export const createOption = (id = 1) => ({
  id,
  name: '',
  placeholder: `Option ${id}`,
  isEditable: false,
  isDefault: false,
});

export const createOptions = (options) =>
  options.map((option) => ({
    id: option.value,
    name: option.label,
    placeholder: `Option ${option.value}`,
    isDefault: option.isSelected,
    isEditable: false,
  }));

export const createDynamicField = (component, props = {}) => {
  const base = createBase(component, props);

  switch (component) {
    case FIELD_TEXT:
    case FIELD_TEXTAREA:
      return {
        ...base,
        value: props.placeholder || '',
      };
    case FIELD_EMAIL:
      return {
        ...base,
        value: props.placeholder || '',
      };
    case FIELD_DATE:
      return {
        ...base,
        value: props.label || '',
      };
    case FIELD_RADIO:
      return {
        ...base,
        value: props.label || '',
        options: props.options?.length ? createOptions(props.options) : [createOption()],
      };
    case FIELD_SELECT:
      return {
        ...base,
        value: props.label || '',
        options: props.options?.length ? createOptions(props.options) : [createOption()],
      };
    case FIELD_CHECKBOX:
      return {
        ...base,
        value: props.label || '',
        options: props.options?.length ? createOptions(props.options) : [createOption()],
      };
    default:
      return base;
  }
};

export const createDefaultField = (props) => createDynamicField(FIELD_TEXT, props);

export default createDynamicField;
