import { TAB_GENERAL } from '../constants';

const urlRegex = /^(https?):\/\/[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_+.~#?&/=]*)/;
const macrosRegex = /(.*(\${ID}).*)/;
const macrosQuestionMarkRegex = /(.*(\?).*)/;

export const validationScheme = [
  {
    fieldName: 'name',
    tabId: TAB_GENERAL,
    validation: (value) => (!value ? 'Filed cant be empty' : ''),
  },
  {
    fieldName: 'url',
    tabId: TAB_GENERAL,
    validation: (value) => {
      if (!value) {
        return 'Field cant be empty';
      }

      if (!urlRegex.test(value)) {
        return 'Please provide correct url';
      }

      return '';
    },
  },
  {
    fieldName: 'macro',
    tabId: TAB_GENERAL,
    validation: (value) => {
      if (!value) {
        return 'Field cant be empty';
      }

      if (!(macrosRegex.test(value) && macrosQuestionMarkRegex.test(value))) {
        return 'Please provide correct macros';
      }

      return '';
    },
  },
];
