import {
  EMAIL_REGEX,
  PASSWORD_REGEX
} from '../config/constants'

import { RegisterOptions } from 'react-hook-form'

export const validation = (
  name: string,
  required: boolean,
  validEmail: boolean = false,
  validPassword: boolean = false
): RegisterOptions => ({
  required: {
    value: required,
    message: `${name} is required`
  },
  validate: {
    matchesPattern: (value: string) => {
      if (validEmail && (EMAIL_REGEX.exec(value) == null)) {
        return 'Valid email is required.'
      }
      if (validPassword !== null && validPassword && (PASSWORD_REGEX.exec(value) == null)) {
        return 'The password should be at least 8 characters long. To make it strong, use the upper and lower case letter(s), the digit(s), and special characters'
      }
      return true
    }
  }
})
