import { createPinia as q, defineStore as y } from "pinia";
const x = () => ({
  lang: "en_us",
  /** 路由切换 / 异步页面 chunk 加载期间为 true，由 router guard 维护 */
  pageLoading: !1
}), L = {
  saveData(t, s) {
    this[t] = s;
  },
  setLang(t) {
    this.lang = t;
  },
  setPageLoading(t) {
    this.pageLoading = !!t;
  }
}, A = {
  state: x,
  actions: L
}, g = (t) => t && typeof t == "object" ? t : {}, u = (t) => t && typeof t == "object" ? t : {}, j = () => {
  var t;
  return ((t = globalThis.$config) == null ? void 0 : t.loginPageName) || "Entry";
}, C = (t) => {
  var a;
  const s = Array.isArray(t == null ? void 0 : t.matched) ? t.matched : [];
  return !!((a = t == null ? void 0 : t.meta) != null && a.public || s.some((e) => {
    var n;
    return (n = e.meta) == null ? void 0 : n.public;
  }));
}, p = (t) => {
  const s = t == null ? void 0 : t.name;
  return !s || s === "Login" || s === j() || C(t);
}, S = (t) => Array.isArray(t) ? t.filter((s) => !p(s)) : [], D = () => ({
  useTab: !1,
  // 是否使用tab
  tabs: [],
  ctab: {}
  // current tab
}), F = {
  saveData(t, s) {
    if (t === "tabs") {
      const a = S(s);
      this[t] = a, localStorage.setItem("tabs", JSON.stringify(a));
      return;
    }
    if (t === "ctab" && p(s)) {
      this[t] = {}, localStorage.removeItem("ctab");
      return;
    }
    this[t] = s;
  },
  saveCurrentTab(t) {
    this.ctab = t, localStorage.setItem("ctab", JSON.stringify(t));
  },
  saveTab(t) {
    const s = S(this.tabs);
    if (s.length !== this.tabs.length && (this.tabs = s, localStorage.setItem("tabs", JSON.stringify(this.tabs))), p(this.ctab) && (this.ctab = {}, localStorage.removeItem("ctab")), p(t))
      return;
    const { data: a, index: e } = this.tabs.filter1((h) => h.name === t.name);
    if (a) {
      const h = JSON.stringify(g(a.params)) === JSON.stringify(g(t.params)), I = JSON.stringify(u(a.query)) === JSON.stringify(u(t.query));
      if (h && I) {
        this.saveCurrentTab(a);
        return;
      }
      const { fullPath: w, name: E, meta: b, params: J, query: P } = t, l = w.split("/");
      let f;
      l[l.length - 1] === "Home" ? f = l[l.length - 2] : f = l[l.length - 1], b != null && b.icon && (f = b.icon);
      const d = {
        name: E,
        meta: b ?? a.meta,
        path: l,
        params: g(J),
        query: u(P),
        icon: f
      };
      this.tabs.splice(e, 1, d), this.saveCurrentTab(d);
      return;
    }
    const { fullPath: n, name: o, meta: i, params: v, query: N } = t, c = n.split("/");
    let m;
    c[c.length - 1] === "Home" ? m = c[c.length - 2] : m = c[c.length - 1], i != null && i.icon && (m = i.icon);
    const T = {
      name: o,
      meta: i,
      path: c,
      params: g(v),
      query: u(N),
      icon: m
    }, { index: O } = this.tabs.filter1(
      (h) => h.name === this.ctab.name
    );
    this.tabs.splice(O + 1, 0, T), this.saveCurrentTab(T);
  },
  closeTab(t, s) {
    var a;
    this.tabs.length > 1 && (this.tabs.splice(s, 1), t.name === this.ctab.name && ((a = globalThis.$router) == null || a.push({
      name: this.tabs[s - 1 > 0 ? s - 1 : 0].name
    })));
  },
  closeRightTab(t, s) {
    var n;
    const a = [];
    let e = !1;
    this.tabs.forEach((o, i) => {
      s >= i ? a.push(o) : o.name === this.ctab.name && (e = !0);
    }), this.tabs = a, e && ((n = globalThis.$router) == null || n.push({ name: t.name }));
  },
  closeLeftTab(t, s) {
    var n;
    const a = [];
    let e = !1;
    this.tabs.forEach((o, i) => {
      s <= i ? a.push(o) : o.name === this.ctab.name && (e = !0);
    }), this.tabs = a, e && ((n = globalThis.$router) == null || n.push({ name: t.name }));
  },
  closeOtherTab(t, s) {
    var a;
    this.tabs = [t], t.name !== this.ctab.name && ((a = globalThis.$router) == null || a.push({ name: t.name }));
  },
  closeAllTab() {
    this.tabs = [];
  }
}, z = {}, B = {
  state: D,
  actions: F,
  getters: z
}, H = () => ({
  type: "",
  visible: !1,
  options: [],
  el: null,
  position: {
    x: 0,
    y: 0
  }
}), R = {
  saveData(t, s) {
    this[t] = s;
  },
  show({ el: t, position: s } = {}) {
    this.visible = !0, s ? (this.position = s, this.el = null) : t && (this.el = t, this.position = {
      x: 0,
      y: 0
    });
  },
  hide() {
    this.visible = !1, this.el = null, this.position = {
      x: 0,
      y: 0
    }, this.type = "";
  }
}, k = {}, Q = {
  state: H,
  actions: R,
  getters: k
}, _ = q(), r = {}, G = (t) => {
  const s = t.match(/(?:^|[/\\])([^/\\]+)\.js$/);
  return s ? s[1] : null;
}, K = (t) => {
  Object.entries(t).forEach(([s, a]) => {
    const e = G(s);
    if (!e || !(a != null && a.default)) {
      console.warn(`Invalid store module: ${s}`);
      return;
    }
    try {
      r[e] = y(e, a.default);
    } catch (n) {
      console.error(`Failed to register store module '${e}':`, n);
    }
  });
};
r.init = y("init", A);
r.tab = y("tab", B);
r.rmenu = y("rmenu", Q);
const $ = (t) => {
  try {
    switch (window.$getType(t)) {
      case "Array":
        t.forEach($);
        break;
      case "String":
        const a = localStorage.getItem(t);
        a && r.tab().saveData(t, JSON.parse(a));
        break;
      default:
        t = [], console.warn(`Invalid key: ${t}`);
        break;
    }
  } catch (s) {
    console.error(`Failed to parse '${t}' from localStorage:`, s);
  }
}, U = (t, { storeChips: s, useTab: a } = {}) => {
  try {
    t.use(_).provide("store", r);
  } catch (e) {
    throw new Error("Failed to inject store into app:", e);
  }
  return a && (r.tab().saveData("useTab", !0), $(["tabs", "ctab"])), s && K(s), r;
};
export {
  U as a,
  _ as p,
  r as s
};
