import { FEE_BUSINESS_MODEL_IMPRESSIONS_CPM, FEE_BUSINESS_MODEL_RAV_SHARE, TAB_DETAILS } from '../constants';

const VALID_URL_REGEX =
  // eslint-disable-next-line max-len
  /^(https?:\/\/)((([a-z\d]([a-z\d-]*[a-z\d])*)\.)+[a-z]{2,}|((\d{1,3}\.){3}\d{1,3}))(:\d+)?(\/[-a-z\d%_.~+]*)*(\?[;&a-z\d%_.~+=[\]-]*)?(#\S*)?$/;

// validation logic getting from
// src/pcn/src/utils/stringUtils.js
const isValidURL = (string = '') => {
  if (!VALID_URL_REGEX.test(string)) {
    return false;
  }

  try {
    const { protocol } = new URL(string);
    return protocol === 'http:' || protocol === 'https:';
  } catch {
    return false;
  }
};

const validateAdsServerFee = (value, field) => {
  const isEmpty = typeof value !== 'number' || Number.isNaN(value);

  if (!field.publisherDemand) {
    return '';
  }
  if (field.adServingFeeBusinessModel === FEE_BUSINESS_MODEL_IMPRESSIONS_CPM) {
    if (isEmpty) {
      return 'Field cannot be empty';
    }
    if (value > 9.99) {
      return 'Must be at most 9.99';
    }
  }
  if (field.adServingFeeBusinessModel === FEE_BUSINESS_MODEL_RAV_SHARE) {
    if (isEmpty) {
      return 'Field cannot be empty';
    }
    if (value < 0.01) {
      return 'Must be at least 0.01';
    }
    if (value > 100) {
      return 'Must be at most 100';
    }
  }

  return '';
};

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

      return '';
    },
  },
  {
    fieldName: 'adServingFeeVideo',
    tabId: TAB_DETAILS,
    validation: validateAdsServerFee,
  },
  {
    fieldName: 'adServingFeeDisplay',
    tabId: TAB_DETAILS,
    validation: validateAdsServerFee,
  },
  {
    fieldName: 'customLogoUrl',
    tabId: TAB_DETAILS,
    validation: (value) => {
      if (value && !isValidURL(value)) {
        return 'Field should be a valid URL';
      }

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

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

      if (+value > 9999999) {
        return 'Field must be less than or equal to 9999999';
      }

      return '';
    },
  },
  {
    fieldName: 'publisherRevShare',
    tabId: TAB_DETAILS,
    validation: (value) => {
      if (value !== 0 && !value) {
        return 'Field cannot be empty';
      }
      if (!Number.isInteger(Number(value))) {
        return 'Must be an integer';
      }
      if (value < 0) {
        return 'Must be at least 0';
      }
      if (value > 100) {
        return 'Must be at most 100';
      }

      return '';
    },
  },
  {
    fieldName: 'expenses',
    tabId: TAB_DETAILS,
    validation: (value) => {
      if (value !== 0 && !value) {
        return 'Field cannot be empty';
      }
      if (!Number.isInteger(Number(value))) {
        return 'Must be an integer';
      }
      if (value < 0) {
        return 'Must be at least 0';
      }
      if (value > 100) {
        return 'Must be at most 100';
      }

      return '';
    },
  },
];
