import { Config as h } from "../../conf/index.js";
import { get as y } from "../../api/commons.js";
function f(n) {
  return JSON.parse(JSON.stringify(n));
}
const p = process.env.VITE_REACT_APP_API_ROOT || h.REACT_APP_API_ROOT || null, C = typeof window < "u" ? window.location.origin : "", g = {}, l = {}, q = 3 * 60 * 1e3;
function P(n) {
  return Object.keys(n).map((t) => encodeURIComponent(t) + "=" + encodeURIComponent(n[t])).join("&");
}
function $(n, t = !1) {
  const e = n, r = Date.now();
  if (l[e] && r - l[e].timestamp < q)
    return Promise.resolve(f(l[e].data));
  if (g[e])
    return g[e].then(f);
  const u = y(n, {}, t).then((c) => (l[e] = { data: c, timestamp: Date.now() }, t ? { data: f(c), meta: l[e].meta } : f(c))).finally(() => {
    delete g[e];
  });
  return g[e] = u, u;
}
const j = ({ app: n, params: t }) => {
  const e = `${p || C}/api/${n}/categories${t ? "?" + P(t) : ""}`;
  return $(e);
}, D = ({ app: n, type: t, params: e }) => {
  const r = `${p || ""}/api/${n}/categories/${t}${e ? "?" + P(e) : ""}`;
  return console.log("categories==>", r), $(r);
}, b = ({ source: n, app: t, params: e }) => {
  const r = `${p || C}/api/${t}/stats/${n}${e ? "?" + P(e) : ""}`;
  return $(r);
}, k = ({ postType: n, taxonomy: t, category: e, taxonomyFilters: r, before: u, perPage: c, page: O, locale: T, after: m, ordering: _, orderingDirection: R }) => {
  const w = `${h.REACT_APP_WP_API}/wp/v2/${n}`, o = new URLSearchParams(), d = /* @__PURE__ */ new Map(), S = (s, i) => {
    if (!s || i == null) return;
    const a = d.get(s) || [];
    Array.isArray(i) ? i.forEach((A) => {
      A != null && a.push(String(A));
    }) : a.push(String(i)), d.set(s, a);
  };
  r && r instanceof Map && r.forEach((s, i) => {
    S(i, s);
  }), t && e != null && S(t, e), d.forEach((s, i) => {
    const a = Array.from(new Set(s));
    a.length !== 0 && o.set(i, a.join(","));
  }), u && o.append("before", u.toISOString()), c && o.append("per_page", c.toString()), o.append("page", O.toString()), o.append("locale", T), m && o.append("after", m.toISOString()), o.append("orderby", _), o.append("order", R);
  const I = o.toString().replace(/%2C/g, ",");
  return y(`${w}?${I}`, {
    headers: {
      "Content-Type": "application/json"
    },
    credentials: "include"
  }, !0);
};
export {
  j as getCategories,
  D as getCategory,
  k as getCustomPosts,
  b as getData
};
