import { safeLocalStorage as E, REFERENCE_LS_KEYS as m } from "@scalar/helpers/object/local-storage";
import { objectReplace as p } from "@scalar/helpers/object/object-replace";
import { snippetz as k } from "@scalar/snippetz";
import { computed as o, readonly as a, ref as C, reactive as A } from "vue";
const y = {
  targetKey: "shell",
  clientKey: "curl"
}, { clients: H } = k();
function d(t) {
  var n;
  return ((n = r.value.find((e) => e.key === t.targetKey)) == null ? void 0 : n.title) ?? t.targetKey;
}
function K(t) {
  var n, e;
  return ((e = (n = r.value.find((i) => i.key === t.targetKey)) == null ? void 0 : n.clients.find((i) => i.client === t.clientKey)) == null ? void 0 : e.title) ?? t.clientKey;
}
const L = o(() => d(l)), S = o(() => K(l));
function b(t, n) {
  return n.value === !0 ? [] : t.flatMap((e) => {
    var i;
    return typeof n.value != "object" ? [] : Array.isArray(n.value) ? (e.clients = e.clients.filter(
      // @ts-expect-error Typescript, chill. It’s all good. It has to be an array.
      (s) => !n.value.includes(s.client)
    ), e.clients.length ? [e] : []) : n.value[e.key] === !0 ? [] : (Array.isArray(n.value[e.key]) && (e.clients = e.clients.filter((s) => !// @ts-expect-error We checked whether it’s an Array already.
    n.value[e.key].includes(s.client))), (i = e == null ? void 0 : e.clients) != null && i.length ? [e] : []);
  });
}
const r = o(() => b(H(), c)), c = C({}), f = C();
function _(t) {
  t !== void 0 && (f.value = t, T(u()));
}
const u = () => {
  var t, n, e, i;
  return v(f.value) ? f.value : v(y) ? y : {
    targetKey: (t = r.value[0]) == null ? void 0 : t.key,
    clientKey: (i = (e = (n = r.value[0]) == null ? void 0 : n.clients) == null ? void 0 : e[0]) == null ? void 0 : i.client
  };
};
function v(t) {
  return t === void 0 ? !1 : !!r.value.find(
    (n) => n.key === t.targetKey && n.clients.find((e) => e.client === t.clientKey)
  );
}
function h() {
  p(l, u());
}
const l = A(u()), T = (t) => {
  Object.assign(l, {
    ...l,
    ...t
  }), E().setItem(m.SELECTED_CLIENT, JSON.stringify(l));
}, R = () => ({
  httpClient: a(l),
  resetState: h,
  setHttpClient: T,
  setDefaultHttpClient: _,
  excludedClients: a(c),
  setExcludedClients: (t) => {
    c.value = t, p(l, u());
  },
  availableTargets: r,
  getClientTitle: K,
  getTargetTitle: d,
  httpTargetTitle: L,
  httpClientTitle: S
});
export {
  b as filterHiddenClients,
  R as useHttpClientStore
};
