import { uniqueChannelsMessages } from '../../constants';
import { TAB_GENERAL, TAB_LOOK_AND_FEEL, TAB_PROMOTIONS } from '../constants';

const urlRegExp = /^(https?):\/\/[^\s/$.?#].[^\s]*\.[a-z]{2,}(\/[^\s]*)?$/i;

export const validationScheme = [
  {
    fieldName: 'title',
    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: 'MRSSAliasDomain',
    tabId: TAB_GENERAL,
    validation: (title, fields) => {
      const value = title?.trim();

      if (fields.MRSSForExport && !value) {
        return 'Field cannot be empty';
      }

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

      if (fields.MRSSForExport && value && !urlRegExp.test(value)) {
        return 'Field must be a valid URL';
      }

      return '';
    },
  },
  {
    fieldName: 'design',
    tabId: TAB_LOOK_AND_FEEL,
    validation: (value, allProps, unique) => {
      if (unique[value]) {
        return uniqueChannelsMessages[value];
      }

      return '';
    },
  },
  {
    fieldName: 'frequency',
    tabId: TAB_PROMOTIONS,
    validation: (value, allProps) => {
      if (!allProps.includeSponsoredContent) {
        return '';
      }

      if (!value) {
        return 'empty';
      }

      if (value < 1 || value > 10) {
        return 'between 1 and 10';
      }

      return '';
    },
  },
  {
    fieldName: 'supplyTag',
    tabId: TAB_PROMOTIONS,
    validation: (value, allProps) => {
      if (!allProps.includeSponsoredContent) {
        return '';
      }

      if (!value || !value.id) {
        return 'Field cannot be empty';
      }

      return '';
    },
  },
];
