import { FALLBACK_LOCALE as a, SUPPORTED_LOCALES as d, DEFAULT_TRANSLATIONS as A } from "./constants/localization.js";
import { hasOwnProperty as f } from "../../utils/struct/property.js";
import { EMPTY_OBJECT as c } from "../../utils/value/constants.js";
import { asPlainObject as C } from "../../utils/struct/main.js";
import { isFunction as O } from "../../utils/value/is.js";
const T = { values: c, count: 0 }, g = /^[a-z]{2}-[A-Z]{2}$/, i = (e) => e.substring(0, 2).toLowerCase();
function s(e, n) {
  if (!e) return null;
  const t = i(e);
  return n.find((r) => i(r) === t) || null;
}
function m(e) {
  const n = e.replace("_", "-");
  if (g.test(n)) return n;
  const [t, r] = n.split("-");
  if (!t || !r) return null;
  const o = `${t.toLowerCase()}-${r.toUpperCase()}`;
  return o.length === 5 ? o : null;
}
function p(e, n) {
  const t = e.trim();
  if (!t || t.length < 1 || t.length > 5)
    return a;
  const r = m(t);
  return r && n.includes(r) ? r : s(r ?? t, n);
}
function F(e = c, n) {
  return e === c ? e : Object.keys(e).reduce((t, r) => {
    const o = m(r) || p(r, n);
    return o && e[r] && (t[o] = e[r]), t;
  }, {});
}
const l = (e, n) => {
  if (O(n)) {
    const t = /* @__PURE__ */ new Map();
    let r = -1;
    return e.replace(/%{(\w+)}/g, (o, u) => {
      let L = t.get(u) ?? -1;
      const _ = n(u, ++r, ++L) ?? "";
      return t.set(u, L), _;
    });
  }
  return e.replace(/%{(\w+)}/g, (t, r) => n?.[r] ?? "");
}, I = (e, n, t = T) => {
  const r = t.count ?? 0, o = `${n}__${r}`;
  if (f(e, o) && e[o])
    return l(e[o], t.values);
  const u = `${n}__plural`;
  return f(e, u) && r > 1 && e[u] ? l(e[u], t.values) : f(e, n) && e[n] ? l(e[n], t.values) : null;
}, N = async (e, n, t = c) => {
  const r = p(e, d) || a, o = n(r);
  return {
    ...A,
    // Default en-US translations (in case any other translation file is missing any key)
    ...await o ?? c,
    // Merge with our locale file of the locale they are loading
    ...C(t?.[e])
    // Merge with their custom locales if available
  };
};
export {
  F as formatCustomTranslations,
  m as formatLocale,
  I as getTranslation,
  N as loadTranslations,
  s as matchLocale,
  p as parseLocale,
  i as toTwoLetterCode
};
