const PRIVATE_PERSON_TYPE = "IranianPrivatePerson";
const LEGAL_PERSON_TYPE = "IranianLegalPerson";
const PRIVATE_PERSON_ALIASES = new Set([
  true,
  "true",
  "private",
  PRIVATE_PERSON_TYPE,
]);
const LEGAL_PERSON_ALIASES = new Set([
  false,
  "false",
  "legal",
  LEGAL_PERSON_TYPE,
]);

export const USER_IDENTITY_PROJECTION =
  "fullName nationalId businessID isPrivatePerson mobile";

export const normalizeIdentifier = (value = "") =>
  String(value || "")
    .replace(/\D/g, "")
    .trim();

export const isPrivatePersonType = (value) => value === PRIVATE_PERSON_TYPE;

export const isLegalPersonType = (value) => value === LEGAL_PERSON_TYPE;

export const resolveIsPrivatePerson = (value) => {
  if (LEGAL_PERSON_ALIASES.has(value)) {
    return false;
  }

  if (PRIVATE_PERSON_ALIASES.has(value)) {
    return true;
  }

  return true;
};

export const getIdentifierField = (user = {}) =>
  resolveIsPrivatePerson(user?.isPrivatePerson) ? "nationalId" : "businessID";

export const getIdentifierLabel = (user = {}) =>
  resolveIsPrivatePerson(user?.isPrivatePerson) ? "کد ملی" : "شناسه ملی";

export const getUserIdentifier = (user = {}) =>
  normalizeIdentifier(user?.[getIdentifierField(user)]);

export const getIdentifierValueByPersonType = ({ personType, identifier }) => {
  const normalizedIdentifier = normalizeIdentifier(identifier);

  return resolveIsPrivatePerson(personType)
    ? {
        nationalId: normalizedIdentifier,
        isPrivatePerson: true,
      }
    : {
        businessID: normalizedIdentifier,
        isPrivatePerson: false,
      };
};

export const getUserIdentityQuery = (user = {}) => {
  const identifierField = getIdentifierField(user);
  const identifier = getUserIdentifier(user);

  return identifier ? { [identifierField]: identifier } : null;
};

export const buildUserIdentityPayload = (user = {}) => ({
  identifier: getUserIdentifier(user),
  identifierField: getIdentifierField(user),
  identifierLabel: getIdentifierLabel(user),
});

export const isValidNationalId = (value = "") =>
  /^\d{10}$/.test(normalizeIdentifier(value));

export const isValidBusinessId = (value = "") => {
  const normalizedValue = normalizeIdentifier(value);

  if (!/^\d{11}$/.test(normalizedValue)) {
    return false;
  }

  if (/^(\d)\1{10}$/.test(normalizedValue)) {
    return false;
  }

  return true;
};

export const validateIdentifierByPersonType = ({ personType, identifier }) => {
  const normalizedIdentifier = normalizeIdentifier(identifier);
  const isPrivatePerson = resolveIsPrivatePerson(personType);

  if (!normalizedIdentifier) {
    return {
      isValid: false,
      message: isPrivatePerson ? "کد ملی الزامی است" : "شناسه ملی الزامی است",
    };
  }

  if (isPrivatePerson) {
    return isValidNationalId(normalizedIdentifier)
      ? { isValid: true, normalizedIdentifier, isPrivatePerson }
      : {
          isValid: false,
          message: "کد ملی باید 10 رقم معتبر باشد",
        };
  }

  return isValidBusinessId(normalizedIdentifier)
    ? { isValid: true, normalizedIdentifier, isPrivatePerson }
    : {
        isValid: false,
        message: "شناسه ملی باید 11 رقم معتبر باشد",
      };
};

