import { TAB_ADVANCED, TAB_GENERAL, TAB_MARKETPLACE_SELF_SERVICE } from '../constants';

export const validationScheme = [
  {
    fieldName: 'account',
    tabId: TAB_GENERAL,
    validation: (value) => {
      if (!value) {
        return 'Field cannot be empty';
      }

      return '';
    },
  },
  {
    fieldName: 'name',
    tabId: TAB_GENERAL,
    validation: (title) => {
      const value = title?.trim();

      if (!value) {
        return 'Field cannot be empty';
      }
      if (value.length < 2) {
        return 'Field cannot be less then 2 symbols';
      }

      return '';
    },
  },
  {
    fieldName: 'domains',
    tabId: TAB_GENERAL,
    validation: (domains) => {
      let error = '';

      const hasDuplicates = () =>
        domains.reduce(
          (acc, current) => {
            if (acc.seen[current.label]) {
              acc.hasDuplicates = true;
            } else {
              acc.seen[current.label] = true;
            }
            return acc;
          },
          { seen: {}, hasDuplicates: false },
        ).hasDuplicates;

      if (!domains.length) {
        error = 'Please add at least one domain';
      } else if (hasDuplicates()) {
        error = 'Domain must be unique';
      }

      return error;
    },
  },
  {
    fieldName: 'demandAccount',
    tabId: TAB_MARKETPLACE_SELF_SERVICE,
    validation: (value) => {
      if (!value) {
        return 'Field cannot be empty';
      }

      return '';
    },
  },
  {
    fieldName: 'defaultRule.cleanUrlRuleValue',
    tabId: TAB_ADVANCED,
    validation: (value) => {
      if (!value) {
        return 'Field cannot be empty';
      }

      return '';
    },
  },
];
