import { getUrlParams as i, flatTree as P } from "@das-fed/utils/common-tools";
import { getSubApps as v, getPageAuth as o } from "@das-fed/utils/common-info";
import { getEl as h } from "@das-fed/utils/directive/utils";
const A = (e, t) => {
  if (!e || !t) return !1;
  if (typeof t == "string")
    return !!e.find((s) => s.code === t);
  {
    let r = !1;
    for (let s of t)
      if (e.find((a) => a.code === s)) {
        r = !0;
        break;
      }
    return r;
  }
}, m = (e) => {
  var n;
  const t = (e == null ? void 0 : e.code) ?? "", r = (e == null ? void 0 : e.path) ?? (location == null ? void 0 : location.pathname) ?? "", s = (e == null ? void 0 : e.applicationCode) ?? i(location.href, "app");
  let l = "";
  if (l = e != null && e.menuCode ? e.menuCode : i(location.href, "menu"), !l) {
    const c = ((n = v().currentSubApp) == null ? void 0 : n.menus) ?? [];
    if (c && c.length) {
      const d = P(c).filter((f) => f == null ? void 0 : f.routeUrl);
      if (d && d.length) {
        const f = d.find((u) => u != null && u.applicationPath ? r == null ? void 0 : r.includes("/" + u.applicationPath + u.routeUrl) : r == null ? void 0 : r.includes(u.routeUrl));
        f && (l = (f == null ? void 0 : f.code) ?? "");
      }
    }
  }
  let a = !1;
  if (t) {
    const c = o(l, s) ?? [];
    a = A(c, t);
  } else
    a = !!o(l, s);
  return a;
}, g = (e, t) => {
  t === "hide" ? e.classList.add("is-hide") : (e.setAttribute("disabled", ""), e.classList.add("is-disabled"));
}, b = (e, t) => {
  t === "hide" ? e.classList.remove("is-hide") : (e.removeAttribute("disabled"), e.classList.remove("is-disabled"));
}, U = (e, t) => {
  let r = (e ? t.value : t) || {};
  typeof r == "string" && (r = { code: r });
  const s = (r == null ? void 0 : r.type) ?? "hide", l = m(r);
  if (e)
    if (!t.oldValue)
      l || h(e, r).then((a) => {
        g(a, s);
      });
    else {
      const a = m(t.oldValue), n = l;
      if (a === n) return;
      h(e, r).then((c) => {
        n ? b(c, s) : g(c, s);
      });
    }
  return l;
};
export {
  U as vAuth
};
