import { useI18n } from "vue-i18n";

export const useInputRules = () => {
  const { t } = useI18n();

  const emailPattern =
    /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
  const passwordPattern = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[a-zA-Z]).{8,}$/;
  const cepPattern = /^[0-9]{5}-[0-9]{3}$/;

  const inputRules = {
    required: (value) => !!value || t("validation.rules.required"),
    requiredNumber: (value) => !isNaN(value) || t("validation.rules.required"),
    requiredHideMessage: (value) => !!value,
    min: (min) => (value) =>
      value.length >= min ||
      `${t("validation.rules.min")} ${min} ${t("validation.rules.characters")}`,
    max: (max) => (value) =>
      value.length <= max ||
      `${t("validation.rules.max")} ${max} ${t("validation.rules.characters")}`,
    minValue: (min) => (value) =>
      value >= min || `${t("validation.rules.min")} ${min}`,
    maxValue: (max) => (value) =>
      value <= max || `${t("validation.rules.max")} ${max}`,
    email: (value) =>
      !value || emailPattern.test(value) || t("validation.rules.email"),
    emails: (value) => {
      if (!value) return true;
      const emails = value.split(";").map((email) => email.trim());
      return (
        emails.every((email) => inputRules.email(email) === true) ||
        t("validation.rules.email")
      );
    },
    cpf: (value) => !value || isValidCPF(value) || t("validation.rules.cpf"),
    cnpj: (value) => !value || isValidCNPJ(value) || t("validation.rules.cnpj"),
    samePassword: (otherValue) => (value) =>
      value === otherValue || t("validation.rules.same_password"),
    password: (value) =>
      !value || passwordPattern.test(value) || t("validation.rules.password"),
    cep: (value) => {
      if (!value) return true;
      return cepPattern.test(value) || t("validation.rules.cep");
    },
    notSameDocument: (allDocuments) => (value) => {
      if (!value) return true;
      let found = 0;
      for (const doc of allDocuments) {
        if (doc == value) {
          found++;
        }
        if (found > 1) {
          return t("validation.rules.not_same_document");
        }
      }
      return true;
    },
  };

  return {
    inputRules,
  };
};

function isValidCPF(cpf) {
  if (!cpf) return false;
  cpf = cpf.replace(/[^\d]+/g, "");
  if (cpf.length !== 11) return false;
  if (/^(\d)\1+$/.test(cpf)) return false;
  let sum = 0;
  for (let i = 0; i < 9; i++) sum += parseInt(cpf.charAt(i)) * (10 - i);
  let rev = 11 - (sum % 11);
  if (rev === 10 || rev === 11) rev = 0;
  if (rev !== parseInt(cpf.charAt(9))) return false;
  sum = 0;
  for (let i = 0; i < 10; i++) sum += parseInt(cpf.charAt(i)) * (11 - i);
  rev = 11 - (sum % 11);
  if (rev === 10 || rev === 11) rev = 0;
  if (rev !== parseInt(cpf.charAt(10))) return false;
  return true;
}

function isValidCNPJ(cnpj) {
  if (!cnpj) return false;
  cnpj = cnpj.replace(/[^\d]+/g, "");
  if (cnpj.length !== 14) return false;
  if (/^(\d)\1+$/.test(cnpj)) return false;
  let length = cnpj.length - 2;
  let numbers = cnpj.substring(0, length);
  let digits = cnpj.substring(length);
  let sum = 0;
  let pos = length - 7;
  for (let i = length; i >= 1; i--) {
    sum += parseInt(numbers.charAt(length - i)) * pos--;
    if (pos < 2) pos = 9;
  }
  let result = sum % 11 < 2 ? 0 : 11 - (sum % 11);
  if (result !== parseInt(digits.charAt(0))) return false;
  length = length + 1;
  numbers = cnpj.substring(0, length);
  sum = 0;
  pos = length - 7;
  for (let i = length; i >= 1; i--) {
    sum += parseInt(numbers.charAt(length - i)) * pos--;
    if (pos < 2) pos = 9;
  }
  result = sum % 11 < 2 ? 0 : 11 - (sum % 11);
  if (result !== parseInt(digits.charAt(1))) return false;
  return true;
}
