(function initFivaNormalizeOptionText(global) {
  "use strict";

  if (global.FIVA?.NormalizeOptionText) {
    if (typeof module !== "undefined" && module.exports) module.exports = global.FIVA.NormalizeOptionText;
    return;
  }

  const HIDDEN_UNICODE = /[\u0000-\u001F\u007F-\u009F\u00A0\u1680\u180E\u2000-\u200F\u2028-\u202F\u205F\u2060\u3000\uFEFF]/g;
  const PLACEHOLDER_PATTERN = /\b(select|choose|loading|wait|please wait|please select|none)\b/i;
  const DASHES = /[‐‑‒–—―−]/g;
  const COUNTRY_ALIASES = Object.freeze({
    BD: "BANGLADESH",
    BGD: "BANGLADESH",
    BANGLADESH: "BANGLADESH",
    BANGLADESHI: "BANGLADESH",
    BANGLADES: "BANGLADESH",
    BANGLADESHINATIONAL: "BANGLADESH",
    PBGD: "BANGLADESH",
    "P<BGD": "BANGLADESH",
    IND: "INDIA",
    INDIA: "INDIA",
    INDIAN: "INDIA",
    PIND: "INDIA",
    "P<IND": "INDIA",
    PAK: "PAKISTAN",
    PAKISTAN: "PAKISTAN",
    PAKISTANI: "PAKISTAN",
    PPAK: "PAKISTAN",
    "P<PAK": "PAKISTAN",
    NPL: "NEPAL",
    NEPAL: "NEPAL",
    NEPALESE: "NEPAL",
    PNPL: "NEPAL",
    "P<NPL": "NEPAL",
    LKA: "SRI LANKA",
    "SRI LANKA": "SRI LANKA",
    SRILANKA: "SRI LANKA",
    "SRI LANKAN": "SRI LANKA",
    SRILANKAN: "SRI LANKA",
    PLKA: "SRI LANKA",
    "P<LKA": "SRI LANKA"
  });

  function cleanOcrArtifacts(value = "") {
    return String(value || "")
      .normalize("NFKC")
      .replace(HIDDEN_UNICODE, " ")
      .replace(DASHES, "-")
      .replace(/\r?\n+/g, " ")
      .replace(/\bB\s*A\s*N\s*G\s*L\s*A\s*D\s*E\s*S\s*H\b/gi, "BANGLADESH")
      .replace(/\bBAN\s+GLA\s+DESH\b/gi, "BANGLADESH")
      .replace(/\bBANG\s+LA\s+DESH\b/gi, "BANGLADESH")
      .replace(/\bBANGLA\s+DESH\b/gi, "BANGLADESH")
      .replace(/\bBANGLADES\s+H\b/gi, "BANGLADESH")
      .replace(/\bBANGLAD\s+ESH\b/gi, "BANGLADESH")
      .replace(/\bB\s*G\s*D\b/gi, "BGD")
      .replace(/\s+/g, " ")
      .trim();
  }

  function clean(value = "") {
    return cleanOcrArtifacts(value)
      .replace(/\s+/g, " ")
      .trim();
  }

  function nationalityAliasResolver(value = "") {
    const cleaned = cleanOcrArtifacts(value).toUpperCase().trim();
    if (!cleaned) return "";
    const normalized = cleaned
      .replace(DASHES, "-")
      .replace(/[_/]+/g, " ")
      .replace(/\s+/g, " ")
      .trim();
    const compact = normalized.replace(/[^A-Z0-9<]/g, "");
    const mrz = compact.match(/^P<([A-Z]{3})/) || compact.match(/\bP<([A-Z]{3})\b/);
    if (mrz?.[1] && COUNTRY_ALIASES[mrz[1]]) return COUNTRY_ALIASES[mrz[1]];
    return COUNTRY_ALIASES[normalized]
      || COUNTRY_ALIASES[compact]
      || COUNTRY_ALIASES[compact.replace(/</g, "")]
      || "";
  }

  function normalizeVisaValue(value = "", fieldKey = "") {
    const cleaned = cleanOcrArtifacts(value).toUpperCase();
    if (!cleaned) return "";
    const alias = nationalityAliasResolver(cleaned);
    if (alias && (!fieldKey || /country|nationality/i.test(fieldKey))) return alias;
    return cleaned
      .replace(DASHES, "-")
      .replace(/[_/]+/g, " ")
      .replace(/\s*-\s*/g, " ")
      .replace(/[^A-Z0-9@.<\s]+/g, " ")
      .replace(/\s+/g, " ")
      .trim();
  }

  function normalizeOptionText(value = "", fieldKey = "") {
    return normalizeVisaValue(value, fieldKey);
  }

  function compactOptionText(value = "", fieldKey = "") {
    return normalizeOptionText(value, fieldKey).replace(/\s+/g, "");
  }

  function optionText(option) {
    return clean(option?.text || option?.textContent || option?.label || option?.innerText || "");
  }

  function optionValue(option) {
    return clean(option?.value || "");
  }

  function isPlaceholderOption(option) {
    const text = optionText(option);
    const value = optionValue(option);
    if (!text && !value) return true;
    if (PLACEHOLDER_PATTERN.test(text)) return true;
    if (!text && PLACEHOLDER_PATTERN.test(value)) return true;
    return false;
  }

  function matchesOptionValue(option, target = "") {
    const normalizedTarget = normalizeOptionText(target);
    if (!normalizedTarget) return false;
    const normalizedText = normalizeOptionText(optionText(option));
    const normalizedValue = normalizeOptionText(optionValue(option));
    const compactTarget = compactOptionText(target);
    const compactText = compactOptionText(optionText(option));
    const compactValue = compactOptionText(optionValue(option));
    const compactParts = [compactText, compactValue].filter((part) => part.length >= 2);
    return normalizedText === normalizedTarget
      || normalizedValue === normalizedTarget
      || (compactTarget.length >= 2 && (
        compactParts.some((part) => part === compactTarget)
        || compactParts.some((part) => part.includes(compactTarget))
        || compactParts.some((part) => compactTarget.includes(part))
      ));
  }

  const NormalizeOptionText = Object.freeze({
    clean,
    cleanOcrArtifacts,
    compactOptionText,
    isPlaceholderOption,
    matchesOptionValue,
    nationalityAliasResolver,
    normalizeOptionText,
    normalizeVisaValue,
    optionText,
    optionValue
  });

  global.FIVA = Object.assign(global.FIVA || {}, { NormalizeOptionText });

  if (typeof module !== "undefined" && module.exports) {
    module.exports = NormalizeOptionText;
  }
})(typeof globalThis !== "undefined" ? globalThis : this);
