import registry from "language-subtag-registry/data/json/registry.json";
function isIsoAlpha2(entry) {
  if (entry.Type !== "region") return false;
  if (!entry.Subtag || !/^[A-Z]{2}$/.test(entry.Subtag)) return false;
  if (entry.Deprecated) return false;
  if (!entry.Description || !entry.Description.length) return false;
  if (entry.Description[0] === "Private use") return false;
  return true;
}
const RAW_COUNTRIES = registry.filter(isIsoAlpha2).map((entry) => ({
  code: entry.Subtag,
  name: entry.Description.join(", ")
}));
const SUPPLEMENTAL_COUNTRIES = [
  { code: "XK", name: "Kosovo" }
];
const RAW_COUNTRY_CODES = new Set(RAW_COUNTRIES.map((entry) => entry.code));
const ISO_COUNTRIES = [
  ...RAW_COUNTRIES,
  ...SUPPLEMENTAL_COUNTRIES.filter((entry) => !RAW_COUNTRY_CODES.has(entry.code))
].sort((a, b) => a.name.localeCompare(b.name, "en", { sensitivity: "base" }));
const COUNTRY_PRIORITY = ["PL", "DE", "ES", "FR", "IT", "US", "GB", "CA"];
const displayNameCache = /* @__PURE__ */ new Map();
function getDisplayNames(locale) {
  if (typeof Intl === "undefined" || typeof Intl.DisplayNames === "undefined") return null;
  const key = locale || "en";
  let instance = displayNameCache.get(key);
  if (!instance) {
    try {
      instance = new Intl.DisplayNames([key], { type: "region" });
      displayNameCache.set(key, instance);
    } catch {
      return null;
    }
  }
  return instance;
}
function resolveCountryName(code, options = {}) {
  const normalized = code.toUpperCase();
  const fallback = ISO_COUNTRIES.find((entry) => entry.code === normalized)?.name ?? normalized;
  const displayNames = getDisplayNames(options.locale ?? "en");
  if (!displayNames) return fallback;
  try {
    const label = displayNames.of(normalized);
    if (typeof label !== "string" || label === normalized) return fallback;
    return label;
  } catch {
    return fallback;
  }
}
function buildCountryOptions(options = {}) {
  const { locale, transformLabel } = options;
  const priority = (options.priority ?? COUNTRY_PRIORITY).map((code) => code.toUpperCase());
  const prioritySet = new Set(priority);
  const labelFor = (code) => {
    const base = resolveCountryName(code, { locale });
    return transformLabel ? transformLabel(code, base) : base;
  };
  const byCode = new Map(ISO_COUNTRIES.map((entry) => [entry.code, entry]));
  const prioritized = priority.map((code) => {
    const base = byCode.get(code);
    if (!base) return null;
    return { code, label: labelFor(code) };
  }).filter((entry) => !!entry).sort((a, b) => a.label.localeCompare(b.label, locale ?? "en", { sensitivity: "base" }));
  const remaining = ISO_COUNTRIES.filter((entry) => !prioritySet.has(entry.code)).map((entry) => ({ code: entry.code, label: labelFor(entry.code) })).sort((a, b) => a.label.localeCompare(b.label, locale ?? "en", { sensitivity: "base" }));
  return [...prioritized, ...remaining];
}
export {
  COUNTRY_PRIORITY,
  ISO_COUNTRIES,
  buildCountryOptions,
  resolveCountryName
};
//# sourceMappingURL=countries.js.map
