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

// req: https://anyclip.atlassian.net/browse/VMW-4729
const isValidURL = (url) => {
  const urlPattern = /^(https?:\/\/)([a-zA-Z0-9.-]+\.[a-zA-Z]{2,})(:\d{1,5})?(.*)?$/;

  return urlPattern.test(url);
};

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

      if (value.length < 2) {
        return 'Minimum 2 letters';
      }

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

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

      if (value.length < 2) {
        return 'Minimum 2 letters';
      }

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

      if (value.length < 2) {
        return 'Minimum 2 letters';
      }

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

      if (!isValidURL(value)) {
        return 'Field should be a valid URL';
      }

      return '';
    },
  },
  {
    fieldName: 'buttonUrl2',
    tabId: TAB_GENERAL,
    validation: (value) => {
      if (value && !isValidURL(value)) {
        return 'Field should be a valid URL';
      }

      return '';
    },
  },
  {
    fieldName: 'impressionTracker',
    tabId: TAB_ADVANCED,
    validation: (value) => {
      if (value && !isValidURL(value)) {
        return 'Field should be a valid URL';
      }

      return '';
    },
  },
  {
    fieldName: 'clickTracker1',
    tabId: TAB_ADVANCED,
    validation: (value) => {
      if (value && !isValidURL(value)) {
        return 'Field should be a valid URL';
      }

      return '';
    },
  },
  {
    fieldName: 'clickTracker2',
    tabId: TAB_ADVANCED,
    validation: (value) => {
      if (value && !isValidURL(value)) {
        return 'Field should be a valid URL';
      }

      return '';
    },
  },
];
