import { jsxs as r, jsx as e } from "react/jsx-runtime";
import { useState as m, useCallback as B, useEffect as R } from "react";
import { useTranslation as G } from "react-i18next";
import { useNavigate as W } from "react-router-dom";
import { Shield as g, FolderTree as q, List as z, LayoutGrid as S, Layers as j, FileCode as C, Search as H, Loader2 as w, ChevronDown as L, ChevronRight as P, Sparkles as J, ExternalLink as K } from "lucide-react";
import { k, B as O } from "./index-CwSaRXXg.js";
function Q({ app: t, expandedNodes: i, toggleNode: o, t: l }) {
  return /* @__PURE__ */ r("div", { children: [
    /* @__PURE__ */ r(
      "button",
      {
        onClick: () => o(t.id),
        className: "w-full flex items-center gap-3 py-3 px-4 pl-14 hover:bg-[var(--bg-hover)] transition-colors group",
        children: [
          /* @__PURE__ */ e("div", { className: "flex items-center justify-center w-5 h-5 rounded bg-[var(--bg-tertiary)] group-hover:bg-[var(--info-bg)] transition-colors", children: i.has(t.id) ? /* @__PURE__ */ e(L, { className: "w-3.5 h-3.5 text-[var(--text-secondary)] group-hover:text-[var(--info-text)]" }) : /* @__PURE__ */ e(P, { className: "w-3.5 h-3.5 text-[var(--text-secondary)] group-hover:text-[var(--info-text)]" }) }),
          /* @__PURE__ */ e("div", { className: "w-7 h-7 rounded-[var(--radius-button)] bg-[var(--info-bg)] flex items-center justify-center", children: /* @__PURE__ */ e(S, { className: "w-3.5 h-3.5 text-[var(--info-text)]" }) }),
          /* @__PURE__ */ r("div", { className: "flex-1 text-left", children: [
            /* @__PURE__ */ r("div", { className: "flex items-center gap-2", children: [
              /* @__PURE__ */ e("span", { className: "font-medium text-[var(--text-primary)] text-sm", children: t.label }),
              /* @__PURE__ */ e("span", { className: "px-1.5 py-0.5 text-[10px] rounded bg-[var(--info-bg)] text-[var(--info-text)] font-medium", children: l("permissions.levels.application") })
            ] }),
            /* @__PURE__ */ e("code", { className: "text-xs text-[var(--text-tertiary)] font-mono", children: t.permissionPath })
          ] }),
          /* @__PURE__ */ r("div", { className: "text-xs text-[var(--text-secondary)]", children: [
            t.modules.length,
            " ",
            l("permissions.stats.modules").toLowerCase()
          ] })
        ]
      }
    ),
    i.has(t.id) && /* @__PURE__ */ e("div", { className: "bg-[var(--bg-tertiary)]/50", children: t.modules.map((s) => /* @__PURE__ */ e(
      U,
      {
        module: s,
        expandedNodes: i,
        toggleNode: o,
        t: l
      },
      s.id
    )) })
  ] }, t.id);
}
function U({ module: t, expandedNodes: i, toggleNode: o, t: l }) {
  return /* @__PURE__ */ r("div", { children: [
    /* @__PURE__ */ r(
      "button",
      {
        onClick: () => o(t.id),
        className: "w-full flex items-center gap-3 py-2.5 px-4 pl-24 hover:bg-[var(--bg-hover)] transition-colors group",
        children: [
          t.sections.length > 0 ? /* @__PURE__ */ e("div", { className: "flex items-center justify-center w-4 h-4 rounded bg-[var(--bg-tertiary)] group-hover:bg-[var(--success-bg)] transition-colors", children: i.has(t.id) ? /* @__PURE__ */ e(L, { className: "w-3 h-3 text-[var(--text-secondary)] group-hover:text-[var(--success-text)]" }) : /* @__PURE__ */ e(P, { className: "w-3 h-3 text-[var(--text-secondary)] group-hover:text-[var(--success-text)]" }) }) : /* @__PURE__ */ e("div", { className: "w-4 h-4" }),
          /* @__PURE__ */ e("div", { className: "w-6 h-6 rounded-[var(--radius-button)] bg-[var(--success-bg)] flex items-center justify-center", children: /* @__PURE__ */ e(j, { className: "w-3 h-3 text-[var(--success-text)]" }) }),
          /* @__PURE__ */ r("div", { className: "flex-1 text-left", children: [
            /* @__PURE__ */ r("div", { className: "flex items-center gap-2", children: [
              /* @__PURE__ */ e("span", { className: "text-sm text-[var(--text-primary)]", children: t.label }),
              /* @__PURE__ */ e("span", { className: "px-1.5 py-0.5 text-[10px] rounded bg-[var(--success-bg)] text-[var(--success-text)] font-medium", children: l("permissions.levels.module") })
            ] }),
            /* @__PURE__ */ e("code", { className: "text-xs text-[var(--text-tertiary)] font-mono", children: t.permissionPath })
          ] }),
          t.sections.length > 0 && /* @__PURE__ */ r("div", { className: "text-xs text-[var(--text-secondary)]", children: [
            t.sections.length,
            " ",
            l("permissions.stats.sections").toLowerCase()
          ] })
        ]
      }
    ),
    i.has(t.id) && t.sections.length > 0 && /* @__PURE__ */ e("div", { className: "bg-[var(--bg-primary)]", children: t.sections.map((s) => /* @__PURE__ */ r(
      "div",
      {
        className: "flex items-center gap-3 py-2 px-4 pl-36 text-sm",
        children: [
          /* @__PURE__ */ e("div", { className: "w-5 h-5 rounded bg-[var(--warning-bg)] flex items-center justify-center", children: /* @__PURE__ */ e(C, { className: "w-3 h-3 text-[var(--warning-text)]" }) }),
          /* @__PURE__ */ r("div", { className: "flex-1", children: [
            /* @__PURE__ */ r("div", { className: "flex items-center gap-2", children: [
              /* @__PURE__ */ e("span", { className: "text-[var(--text-primary)]", children: s.label }),
              /* @__PURE__ */ e("span", { className: "px-1.5 py-0.5 text-[10px] rounded bg-[var(--warning-bg)] text-[var(--warning-text)] font-medium", children: l("permissions.levels.section") })
            ] }),
            /* @__PURE__ */ e("code", { className: "text-xs text-[var(--text-tertiary)] font-mono", children: s.permissionPath })
          ] })
        ]
      },
      s.id
    )) })
  ] }, t.id);
}
function X({ permission: t, navigate: i }) {
  return /* @__PURE__ */ r(
    "tr",
    {
      onClick: () => i(`/administration/permissions/permissions/${t.id}`),
      className: "hover:bg-[var(--bg-hover)] transition-colors cursor-pointer group",
      children: [
        /* @__PURE__ */ e("td", { className: "px-4 py-3", children: /* @__PURE__ */ r("div", { className: "flex items-center gap-2", children: [
          /* @__PURE__ */ e("code", { className: "font-mono text-sm text-[var(--text-primary)] bg-[var(--bg-tertiary)] px-2 py-1 rounded group-hover:bg-[var(--color-accent-100)] dark:group-hover:bg-[var(--color-accent-900)]/30 transition-colors", children: t.path }),
          t.isWildcard && /* @__PURE__ */ r("span", { className: "inline-flex items-center gap-1 px-2 py-0.5 text-xs rounded-full bg-[var(--warning-bg)] text-[var(--warning-text)] font-medium", children: [
            /* @__PURE__ */ e(J, { className: "w-3 h-3" }),
            "wildcard"
          ] })
        ] }) }),
        /* @__PURE__ */ e("td", { className: "px-4 py-3 hidden md:table-cell", children: /* @__PURE__ */ e("span", { className: "px-2.5 py-1 text-xs rounded-full bg-[var(--color-accent-100)] dark:bg-[var(--color-accent-900)]/30 text-[var(--color-accent-700)] dark:text-[var(--color-accent-300)] font-medium", children: t.level }) }),
        /* @__PURE__ */ e("td", { className: "px-4 py-3 text-[var(--text-secondary)] hidden lg:table-cell", children: t.description || "-" }),
        /* @__PURE__ */ e("td", { className: "px-4 py-3", children: /* @__PURE__ */ r("div", { className: "flex items-center gap-3", children: [
          /* @__PURE__ */ e("span", { className: "inline-flex items-center justify-center min-w-[2rem] px-2 py-1 text-sm font-medium rounded-full bg-[var(--bg-tertiary)] text-[var(--text-primary)]", children: t.rolesCount }),
          /* @__PURE__ */ e(K, { className: "w-4 h-4 text-[var(--text-tertiary)] opacity-0 group-hover:opacity-100 transition-opacity" })
        ] }) })
      ]
    },
    t.id
  );
}
function ie() {
  const { t } = G("admin"), i = W(), [o, l] = m([]), [s, T] = m(null), [d, b] = m(!0), [h, E] = m(""), [x, f] = m("list"), [A, N] = m(/* @__PURE__ */ new Set()), p = B(async () => {
    try {
      b(!0);
      const a = await k.permissions.getAll({ search: h || void 0 });
      l(a);
    } catch (a) {
      console.error("Failed to load permissions:", a);
    } finally {
      b(!1);
    }
  }, [h]), F = (a) => {
    const n = /* @__PURE__ */ new Set();
    return (a.applications ?? []).forEach((c) => {
      n.add(c.id), (c.modules ?? []).forEach((v) => {
        n.add(v.id);
      });
    }), n;
  }, D = async () => {
    try {
      const a = await k.permissions.getTree();
      T(a);
      const n = F(a);
      N(n);
    } catch (a) {
      console.error("Failed to load permission tree:", a);
    }
  };
  R(() => {
    p(), D();
  }, [p]);
  const I = (a) => {
    a.preventDefault(), p();
  }, M = (a) => {
    N((n) => {
      const c = new Set(n);
      return c.has(a) ? c.delete(a) : c.add(a), c;
    });
  }, u = s ? ((a) => {
    const n = a.applications ?? [];
    let c = 0, v = 0;
    return n.forEach((V) => {
      const y = V.modules ?? [];
      c += y.length, y.forEach(($) => {
        v += ($.sections ?? []).length;
      });
    }), { applications: n.length, modules: c, sections: v };
  })(s) : { applications: 0, modules: 0, sections: 0 };
  return /* @__PURE__ */ r("div", { className: "space-y-6", children: [
    /* @__PURE__ */ e(
      O,
      {
        items: [
          { label: t("header.title"), href: "/administration" },
          { label: t("permissions.title") }
        ]
      }
    ),
    /* @__PURE__ */ r("div", { className: "flex items-center justify-between", children: [
      /* @__PURE__ */ r("div", { className: "flex items-center gap-4", children: [
        /* @__PURE__ */ e("div", { className: "w-12 h-12 rounded-[var(--radius-button)] bg-[var(--color-accent-100)] dark:bg-[var(--color-accent-900)]/30 flex items-center justify-center", children: /* @__PURE__ */ e(g, { className: "w-6 h-6 text-[var(--color-accent-600)] dark:text-[var(--color-accent-400)]" }) }),
        /* @__PURE__ */ r("div", { children: [
          /* @__PURE__ */ e("h1", { className: "text-2xl font-bold text-[var(--text-primary)]", children: t("permissions.title") }),
          /* @__PURE__ */ e("p", { className: "text-[var(--text-secondary)] mt-0.5", children: t("permissions.subtitle") })
        ] })
      ] }),
      /* @__PURE__ */ r("div", { className: "flex items-center gap-1 p-1 bg-[var(--bg-tertiary)] rounded-[var(--radius-button)]", children: [
        /* @__PURE__ */ r(
          "button",
          {
            onClick: () => f("tree"),
            className: `flex items-center gap-2 px-4 py-2 rounded-[var(--radius-button)] font-medium transition-all ${x === "tree" ? "bg-[var(--color-accent-500)] text-white shadow-sm" : "text-[var(--text-secondary)] hover:text-[var(--text-primary)] hover:bg-[var(--bg-hover)]"}`,
            children: [
              /* @__PURE__ */ e(q, { className: "w-4 h-4" }),
              t("permissions.treeView")
            ]
          }
        ),
        /* @__PURE__ */ r(
          "button",
          {
            onClick: () => f("list"),
            className: `flex items-center gap-2 px-4 py-2 rounded-[var(--radius-button)] font-medium transition-all ${x === "list" ? "bg-[var(--color-accent-500)] text-white shadow-sm" : "text-[var(--text-secondary)] hover:text-[var(--text-primary)] hover:bg-[var(--bg-hover)]"}`,
            children: [
              /* @__PURE__ */ e(z, { className: "w-4 h-4" }),
              t("permissions.listView")
            ]
          }
        )
      ] })
    ] }),
    /* @__PURE__ */ r("div", { className: "grid grid-cols-3 gap-4", children: [
      /* @__PURE__ */ e("div", { className: "bg-[var(--bg-card)] rounded-[var(--radius-card)] border border-[var(--border-color)] p-4", children: /* @__PURE__ */ r("div", { className: "flex items-center gap-3", children: [
        /* @__PURE__ */ e("div", { className: "w-10 h-10 rounded-[var(--radius-button)] bg-[var(--info-bg)] flex items-center justify-center", children: /* @__PURE__ */ e(S, { className: "w-5 h-5 text-[var(--info-text)]" }) }),
        /* @__PURE__ */ r("div", { children: [
          /* @__PURE__ */ e("div", { className: "text-2xl font-bold text-[var(--text-primary)]", children: u.applications }),
          /* @__PURE__ */ e("div", { className: "text-sm text-[var(--text-secondary)]", children: t("permissions.stats.applications") })
        ] })
      ] }) }),
      /* @__PURE__ */ e("div", { className: "bg-[var(--bg-card)] rounded-[var(--radius-card)] border border-[var(--border-color)] p-4", children: /* @__PURE__ */ r("div", { className: "flex items-center gap-3", children: [
        /* @__PURE__ */ e("div", { className: "w-10 h-10 rounded-[var(--radius-button)] bg-[var(--success-bg)] flex items-center justify-center", children: /* @__PURE__ */ e(j, { className: "w-5 h-5 text-[var(--success-text)]" }) }),
        /* @__PURE__ */ r("div", { children: [
          /* @__PURE__ */ e("div", { className: "text-2xl font-bold text-[var(--text-primary)]", children: u.modules }),
          /* @__PURE__ */ e("div", { className: "text-sm text-[var(--text-secondary)]", children: t("permissions.stats.modules") })
        ] })
      ] }) }),
      /* @__PURE__ */ e("div", { className: "bg-[var(--bg-card)] rounded-[var(--radius-card)] border border-[var(--border-color)] p-4", children: /* @__PURE__ */ r("div", { className: "flex items-center gap-3", children: [
        /* @__PURE__ */ e("div", { className: "w-10 h-10 rounded-[var(--radius-button)] bg-[var(--warning-bg)] flex items-center justify-center", children: /* @__PURE__ */ e(C, { className: "w-5 h-5 text-[var(--warning-text)]" }) }),
        /* @__PURE__ */ r("div", { children: [
          /* @__PURE__ */ e("div", { className: "text-2xl font-bold text-[var(--text-primary)]", children: u.sections }),
          /* @__PURE__ */ e("div", { className: "text-sm text-[var(--text-secondary)]", children: t("permissions.stats.sections") })
        ] })
      ] }) })
    ] }),
    x === "list" && /* @__PURE__ */ r("form", { onSubmit: I, className: "flex gap-2", children: [
      /* @__PURE__ */ r("div", { className: "relative flex-1 max-w-md", children: [
        /* @__PURE__ */ e(H, { className: "absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-[var(--text-secondary)]" }),
        /* @__PURE__ */ e(
          "input",
          {
            type: "text",
            value: h,
            onChange: (a) => E(a.target.value),
            placeholder: t("common.search"),
            className: "w-full pl-10 pr-4 py-2.5 rounded-[var(--radius-button)] border border-[var(--border-color)] bg-[var(--bg-secondary)] text-[var(--text-primary)] focus:outline-none focus:ring-2 focus:ring-[var(--color-accent-500)] focus:border-transparent transition-all"
          }
        )
      ] }),
      /* @__PURE__ */ e(
        "button",
        {
          type: "submit",
          className: "px-4 py-2.5 rounded-[var(--radius-button)] bg-[var(--color-accent-500)] hover:bg-[var(--color-accent-600)] text-white font-medium transition-colors",
          children: t("common.search")
        }
      )
    ] }),
    x === "tree" ? /* @__PURE__ */ r("div", { className: "bg-[var(--bg-card)] rounded-[var(--radius-card)] border border-[var(--border-color)] overflow-hidden", children: [
      d && /* @__PURE__ */ e("div", { className: "flex justify-center py-16", children: /* @__PURE__ */ e(w, { className: "w-8 h-8 animate-spin text-[var(--color-accent-500)]" }) }),
      !d && s?.applications?.length ? /* @__PURE__ */ e("div", { className: "divide-y divide-[var(--border-color)]", children: s.applications.map((a) => /* @__PURE__ */ e(
        Q,
        {
          app: a,
          expandedNodes: A,
          toggleNode: M,
          t
        },
        a.id
      )) }) : !d && /* @__PURE__ */ r("div", { className: "flex flex-col items-center justify-center py-16 text-[var(--text-secondary)]", children: [
        /* @__PURE__ */ e(g, { className: "w-12 h-12 mb-4 text-[var(--text-tertiary)]" }),
        /* @__PURE__ */ e("p", { children: t("permissions.noData") })
      ] })
    ] }) : (
      /* List View */
      /* @__PURE__ */ e("div", { className: "card overflow-hidden", children: /* @__PURE__ */ r("table", { className: "w-full", children: [
        /* @__PURE__ */ e("thead", { children: /* @__PURE__ */ r("tr", { className: "bg-[var(--bg-secondary)]", children: [
          /* @__PURE__ */ e("th", { className: "text-left px-4 py-3 text-xs font-semibold text-[var(--text-tertiary)] uppercase tracking-wider", children: t("permissions.columns.path") }),
          /* @__PURE__ */ e("th", { className: "text-left px-4 py-3 text-xs font-semibold text-[var(--text-tertiary)] uppercase tracking-wider hidden md:table-cell", children: t("permissions.columns.level") }),
          /* @__PURE__ */ e("th", { className: "text-left px-4 py-3 text-xs font-semibold text-[var(--text-tertiary)] uppercase tracking-wider hidden lg:table-cell", children: t("permissions.columns.description") }),
          /* @__PURE__ */ e("th", { className: "text-left px-4 py-3 text-xs font-semibold text-[var(--text-tertiary)] uppercase tracking-wider", children: t("permissions.columns.roles") })
        ] }) }),
        /* @__PURE__ */ r("tbody", { className: "divide-y divide-[var(--border-color)]", children: [
          d && /* @__PURE__ */ e("tr", { children: /* @__PURE__ */ e("td", { colSpan: 4, className: "px-6 py-16 text-center", children: /* @__PURE__ */ e(w, { className: "w-8 h-8 animate-spin mx-auto text-[var(--color-accent-500)]" }) }) }),
          !d && o.length === 0 && /* @__PURE__ */ e("tr", { children: /* @__PURE__ */ e("td", { colSpan: 4, className: "px-6 py-16 text-center", children: /* @__PURE__ */ r("div", { className: "flex flex-col items-center text-[var(--text-secondary)]", children: [
            /* @__PURE__ */ e(g, { className: "w-12 h-12 mb-4 text-[var(--text-tertiary)]" }),
            /* @__PURE__ */ e("p", { children: t("permissions.noPermissions") })
          ] }) }) }),
          !d && o.length > 0 && o.map((a) => /* @__PURE__ */ e(
            X,
            {
              permission: a,
              navigate: i
            },
            a.id
          ))
        ] })
      ] }) })
    )
  ] });
}
export {
  ie as PermissionsPage
};
//# sourceMappingURL=PermissionsPage-DLy9U3P3.js.map
