import { Validator } from "../types/controller";

//this regex accepts uk postcode formats - AA9A 9AA, A9A 9AA, A9 9AA, A99 9AA, AA9 9AA, AA99 9AA also withoput spaces 
const postCodeRegex = /^([A-Z]{1,2}[0-9][A-Z0-9]? ?[0-9][A-Z]{2})$/i
const ukPhoneNumberRegex = /^(?:(?:\(?(?:0(?:0|11)\)?[\s-]?\(?|\+)44\)?[\s-]?(?:\(?0\)?[\s-]?)?)|(?:\(?0))(?:(?:\d{5}\)?[\s-]?\d{4,5})|(?:\d{4}\)?[\s-]?(?:\d{5}|\d{3}[\s-]?\d{3}))|(?:\d{3}\)?[\s-]?\d{3}[\s-]?\d{3,4})|(?:\d{2}\)?[\s-]?\d{4}[\s-]?\d{4}))(?:[\s-]?(?:x|ext\.?|#)\d{3,4})?$/
const emailRegex = /^[\w-.]+@([\w-]+\.)+[\w-]{2,4}$\b/g; //added 13 AUG  


export const emailValidator: Validator<string> = (value: string): boolean => {

  const isValid = value.match(emailRegex) || false;
  if (isValid) {
    return true;
  }
  return isValid;
};

export const postCodeValidator: Validator<string> = (postcode: string): boolean => {
  const matches = postcode?.trim().toUpperCase().match(postCodeRegex) || false;

  if (matches) {
    return true;
  }
  return matches;
};

export const selectddlValidator: Validator<string> = (selectedValue: string): boolean => {
  const isValid = (selectedValue != '' && selectedValue != null) ? true : false;
  return isValid
}

export const phoneNumberValidator: Validator<string> = (value: string): boolean => {
  const isValid = value.match(ukPhoneNumberRegex) || false;
  if (isValid) {
    return true;
  }
  return isValid;
} 