import { jsxs as a, jsx as r } from "react/jsx-runtime";
import { useState as i, useCallback as K, useEffect as Q } from "react";
import { useTranslation as V } from "react-i18next";
import * as W from "lucide-react";
import { Loader2 as T, ChevronDown as Y, ChevronRight as Z, MoreHorizontal as _, Edit2 as S, EyeOff as $, Eye as ee, X as E } from "lucide-react";
import { k as u, B as re } from "./index-CwSaRXXg.js";
const D = ({ name: o, className: p }) => {
  const s = W[o];
  return s ? /* @__PURE__ */ r(s, { className: p }) : null;
};
function ne() {
  const { t: o } = V("admin"), [p, M] = i([]), [s, L] = i(!0), [A, F] = i(/* @__PURE__ */ new Set()), [v, U] = i({}), [z, y] = i(!1), [R, h] = i(!1), [g, q] = i(null), [c, B] = i(null), [l, d] = i({
    label: "",
    description: "",
    icon: "",
    iconType: "Lucide",
    route: "",
    displayOrder: 0
  }), [n, b] = i({
    label: "",
    description: "",
    icon: "",
    iconType: "Lucide",
    route: "",
    displayOrder: 0
  }), [f, N] = i(!1), [w, m] = i(null), [j, C] = i(null), k = K(async () => {
    try {
      L(!0);
      const e = await u.applications.getAll();
      M(e);
    } catch (e) {
      console.error("Failed to load applications:", e);
    } finally {
      L(!1);
    }
  }, []), O = async (e) => {
    try {
      const t = await u.applications.getModules(e);
      U((x) => ({ ...x, [e]: t }));
    } catch (t) {
      console.error("Failed to load modules:", t);
    }
  };
  Q(() => {
    k();
  }, [k]);
  const I = async (e) => {
    const t = new Set(A);
    t.has(e) ? t.delete(e) : (t.add(e), v[e] || await O(e)), F(t);
  }, H = (e) => {
    q(e), d({
      label: e.label,
      description: e.description || "",
      icon: e.icon || "",
      iconType: e.iconType,
      route: e.route || "",
      displayOrder: e.displayOrder
    }), m(null), y(!0);
  }, P = (e, t) => {
    B({ appId: e, module: t }), b({
      label: t.label,
      description: t.description || "",
      icon: t.icon || "",
      iconType: t.iconType,
      route: t.route || "",
      displayOrder: t.displayOrder
    }), m(null), h(!0);
  }, X = async (e) => {
    if (e.preventDefault(), !!g) {
      N(!0), m(null);
      try {
        const t = {
          label: l.label,
          description: l.description,
          icon: l.icon,
          iconType: l.iconType,
          route: l.route,
          displayOrder: l.displayOrder
        };
        await u.applications.update(g.id, t), y(!1), k();
      } catch (t) {
        const x = t instanceof Error ? t.message : "An error occurred";
        m(x);
      } finally {
        N(!1);
      }
    }
  }, G = async (e) => {
    if (e.preventDefault(), !!c) {
      N(!0), m(null);
      try {
        const t = {
          label: n.label,
          description: n.description,
          icon: n.icon,
          iconType: n.iconType,
          route: n.route,
          displayOrder: n.displayOrder
        };
        await u.applications.updateModule(c.appId, c.module.id, t), h(!1), O(c.appId);
      } catch (t) {
        const x = t instanceof Error ? t.message : "An error occurred";
        m(x);
      } finally {
        N(!1);
      }
    }
  }, J = async (e) => {
    try {
      e.isActive ? await u.applications.deactivate(e.id) : await u.applications.activate(e.id), k();
    } catch (t) {
      console.error("Failed to toggle app status:", t);
    }
    C(null);
  };
  return /* @__PURE__ */ a("div", { className: "space-y-6", children: [
    /* @__PURE__ */ r(
      re,
      {
        items: [
          { label: o("header.title"), href: "/administration" },
          { label: o("applications.title") }
        ]
      }
    ),
    /* @__PURE__ */ r("div", { className: "flex items-center justify-between", children: /* @__PURE__ */ a("div", { children: [
      /* @__PURE__ */ r("h1", { className: "text-2xl font-bold text-[var(--text-primary)]", children: o("applications.title") }),
      /* @__PURE__ */ r("p", { className: "text-[var(--text-secondary)] mt-1", children: o("applications.subtitle") })
    ] }) }),
    /* @__PURE__ */ a("div", { className: "space-y-4", children: [
      s && /* @__PURE__ */ r("div", { className: "flex justify-center py-12", children: /* @__PURE__ */ r(T, { className: "w-6 h-6 animate-spin text-primary-500" }) }),
      !s && p.length === 0 && /* @__PURE__ */ r("div", { className: "text-center py-12 text-[var(--text-secondary)] bg-[var(--bg-secondary)] rounded-xl border border-[var(--border-color)]", children: o("applications.noApps") }),
      !s && p.length > 0 && p.map((e) => /* @__PURE__ */ a("div", { className: "bg-[var(--bg-secondary)] rounded-xl border border-[var(--border-color)] overflow-hidden", children: [
        /* @__PURE__ */ a("div", { className: "flex items-center gap-4 p-4", children: [
          /* @__PURE__ */ r(
            "button",
            {
              onClick: () => I(e.id),
              className: "p-1 rounded hover:bg-[var(--bg-hover)]",
              children: A.has(e.id) ? /* @__PURE__ */ r(Y, { className: "w-5 h-5 text-[var(--text-secondary)]" }) : /* @__PURE__ */ r(Z, { className: "w-5 h-5 text-[var(--text-secondary)]" })
            }
          ),
          /* @__PURE__ */ a("div", { className: "w-10 h-10 rounded-xl bg-primary-100 dark:bg-primary-900 flex items-center justify-center", children: [
            e.iconType === "Lucide" && e.icon && /* @__PURE__ */ r(D, { name: e.icon, className: "w-5 h-5 text-primary-600 dark:text-primary-400" }),
            e.iconType === "Url" && e.icon && /* @__PURE__ */ r("img", { src: e.icon, alt: "", className: "w-6 h-6" }),
            !(e.iconType === "Lucide" && e.icon) && !(e.iconType === "Url" && e.icon) && /* @__PURE__ */ r("div", { className: "w-5 h-5 bg-primary-500 rounded" })
          ] }),
          /* @__PURE__ */ a("div", { className: "flex-1", children: [
            /* @__PURE__ */ a("div", { className: "flex items-center gap-2", children: [
              /* @__PURE__ */ r("span", { className: "font-medium text-[var(--text-primary)]", children: e.label }),
              /* @__PURE__ */ r("span", { className: "px-2 py-0.5 text-xs rounded-full bg-[var(--bg-tertiary)] text-[var(--text-secondary)]", children: e.zone }),
              !e.isActive && /* @__PURE__ */ r("span", { className: "px-2 py-0.5 text-xs rounded-full bg-red-100 dark:bg-red-900 text-red-700 dark:text-red-300", children: o("common.inactive") })
            ] }),
            /* @__PURE__ */ r("div", { className: "text-sm text-[var(--text-secondary)]", children: e.description || e.route })
          ] }),
          /* @__PURE__ */ a("div", { className: "text-sm text-[var(--text-secondary)]", children: [
            e.modulesCount,
            " ",
            o("applications.modules")
          ] }),
          /* @__PURE__ */ a("div", { className: "relative", children: [
            /* @__PURE__ */ r(
              "button",
              {
                onClick: () => C(j === e.id ? null : e.id),
                className: "p-2 rounded-lg hover:bg-[var(--bg-tertiary)] transition-colors",
                children: /* @__PURE__ */ r(_, { className: "w-4 h-4 text-[var(--text-secondary)]" })
              }
            ),
            j === e.id && /* @__PURE__ */ a("div", { className: "absolute right-0 top-full mt-1 w-48 py-1 bg-[var(--bg-secondary)] rounded-xl border border-[var(--border-color)] shadow-lg z-10", children: [
              /* @__PURE__ */ a(
                "button",
                {
                  onClick: () => {
                    H(e), C(null);
                  },
                  className: "w-full flex items-center gap-2 px-4 py-2 text-left text-[var(--text-primary)] hover:bg-[var(--bg-hover)] transition-colors",
                  children: [
                    /* @__PURE__ */ r(S, { className: "w-4 h-4" }),
                    o("common.edit")
                  ]
                }
              ),
              /* @__PURE__ */ a(
                "button",
                {
                  onClick: () => J(e),
                  className: "w-full flex items-center gap-2 px-4 py-2 text-left text-[var(--text-primary)] hover:bg-[var(--bg-hover)] transition-colors",
                  children: [
                    e.isActive ? /* @__PURE__ */ r($, { className: "w-4 h-4" }) : /* @__PURE__ */ r(ee, { className: "w-4 h-4" }),
                    e.isActive ? o("common.deactivate") : o("common.activate")
                  ]
                }
              )
            ] })
          ] })
        ] }),
        A.has(e.id) && /* @__PURE__ */ a("div", { className: "border-t border-[var(--border-color)] p-4 bg-[var(--bg-primary)]", children: [
          /* @__PURE__ */ r("div", { className: "flex items-center justify-between mb-4", children: /* @__PURE__ */ r("h3", { className: "font-medium text-[var(--text-primary)]", children: o("applications.modules") }) }),
          v[e.id] ? v[e.id].length === 0 ? /* @__PURE__ */ r("div", { className: "text-center py-8 text-[var(--text-secondary)]", children: o("applications.noModules") }) : /* @__PURE__ */ r("div", { className: "space-y-2", children: v[e.id].map((t) => /* @__PURE__ */ a(
            "div",
            {
              className: "flex items-center gap-3 p-3 rounded-lg bg-[var(--bg-secondary)] border border-[var(--border-color)]",
              children: [
                /* @__PURE__ */ r("div", { className: "w-8 h-8 rounded-lg bg-[var(--bg-tertiary)] flex items-center justify-center", children: t.iconType === "Lucide" && t.icon ? /* @__PURE__ */ r(D, { name: t.icon, className: "w-4 h-4 text-[var(--text-secondary)]" }) : /* @__PURE__ */ r("div", { className: "w-4 h-4 bg-[var(--text-tertiary)] rounded" }) }),
                /* @__PURE__ */ a("div", { className: "flex-1", children: [
                  /* @__PURE__ */ r("div", { className: "font-medium text-sm text-[var(--text-primary)]", children: t.label }),
                  /* @__PURE__ */ r("div", { className: "text-xs text-[var(--text-secondary)]", children: t.route })
                ] }),
                /* @__PURE__ */ r("div", { className: "flex items-center gap-1", children: /* @__PURE__ */ r(
                  "button",
                  {
                    onClick: () => P(e.id, t),
                    className: "p-1.5 rounded-lg hover:bg-[var(--bg-hover)] transition-colors",
                    children: /* @__PURE__ */ r(S, { className: "w-3.5 h-3.5 text-[var(--text-secondary)]" })
                  }
                ) })
              ]
            },
            t.id
          )) }) : /* @__PURE__ */ r("div", { className: "flex justify-center py-4", children: /* @__PURE__ */ r(T, { className: "w-5 h-5 animate-spin text-primary-500" }) })
        ] })
      ] }, e.id))
    ] }),
    z && g && /* @__PURE__ */ r("div", { className: "fixed inset-0 bg-black/50 flex items-center justify-center z-50", children: /* @__PURE__ */ a("div", { className: "bg-[var(--bg-primary)] rounded-2xl w-full max-w-lg p-6 m-4 max-h-[90vh] overflow-y-auto", children: [
      /* @__PURE__ */ a("div", { className: "flex items-center justify-between mb-6", children: [
        /* @__PURE__ */ r("h2", { className: "text-xl font-bold text-[var(--text-primary)]", children: o("applications.edit") }),
        /* @__PURE__ */ r("button", { onClick: () => y(!1), className: "p-2 rounded-lg hover:bg-[var(--bg-hover)]", children: /* @__PURE__ */ r(E, { className: "w-5 h-5 text-[var(--text-secondary)]" }) })
      ] }),
      w && /* @__PURE__ */ r("div", { className: "mb-4 p-3 rounded-lg bg-red-100 dark:bg-red-900/30 text-red-700 dark:text-red-300 text-sm", children: w }),
      /* @__PURE__ */ a("form", { onSubmit: X, className: "space-y-4", children: [
        /* @__PURE__ */ a("div", { children: [
          /* @__PURE__ */ r("label", { className: "block text-sm font-medium text-[var(--text-secondary)] mb-1", children: o("applications.form.code") }),
          /* @__PURE__ */ r(
            "input",
            {
              type: "text",
              value: g.code,
              disabled: !0,
              className: "w-full px-4 py-2 rounded-xl border border-[var(--border-color)] bg-[var(--bg-tertiary)] text-[var(--text-muted)] cursor-not-allowed"
            }
          )
        ] }),
        /* @__PURE__ */ a("div", { children: [
          /* @__PURE__ */ r("label", { className: "block text-sm font-medium text-[var(--text-secondary)] mb-1", children: o("applications.form.label") }),
          /* @__PURE__ */ r(
            "input",
            {
              type: "text",
              required: !0,
              value: l.label,
              onChange: (e) => d((t) => ({ ...t, label: e.target.value })),
              className: "w-full px-4 py-2 rounded-xl border border-[var(--border-color)] bg-[var(--bg-secondary)] text-[var(--text-primary)] focus:outline-none focus:ring-2 focus:ring-primary-500"
            }
          )
        ] }),
        /* @__PURE__ */ a("div", { children: [
          /* @__PURE__ */ r("label", { className: "block text-sm font-medium text-[var(--text-secondary)] mb-1", children: o("applications.form.description") }),
          /* @__PURE__ */ r(
            "textarea",
            {
              value: l.description,
              onChange: (e) => d((t) => ({ ...t, description: e.target.value })),
              rows: 2,
              className: "w-full px-4 py-2 rounded-xl border border-[var(--border-color)] bg-[var(--bg-secondary)] text-[var(--text-primary)] focus:outline-none focus:ring-2 focus:ring-primary-500 resize-none"
            }
          )
        ] }),
        /* @__PURE__ */ a("div", { className: "grid grid-cols-2 gap-4", children: [
          /* @__PURE__ */ a("div", { children: [
            /* @__PURE__ */ r("label", { className: "block text-sm font-medium text-[var(--text-secondary)] mb-1", children: o("applications.form.iconType") }),
            /* @__PURE__ */ a(
              "select",
              {
                value: l.iconType,
                onChange: (e) => d((t) => ({ ...t, iconType: e.target.value })),
                className: "w-full px-4 py-2 rounded-xl border border-[var(--border-color)] bg-[var(--bg-secondary)] text-[var(--text-primary)] focus:outline-none focus:ring-2 focus:ring-primary-500",
                children: [
                  /* @__PURE__ */ r("option", { value: "Lucide", children: "Lucide Icon" }),
                  /* @__PURE__ */ r("option", { value: "Url", children: "URL" })
                ]
              }
            )
          ] }),
          /* @__PURE__ */ a("div", { children: [
            /* @__PURE__ */ r("label", { className: "block text-sm font-medium text-[var(--text-secondary)] mb-1", children: o("applications.form.icon") }),
            /* @__PURE__ */ r(
              "input",
              {
                type: "text",
                value: l.icon,
                onChange: (e) => d((t) => ({ ...t, icon: e.target.value })),
                placeholder: l.iconType === "Lucide" ? "Shield, Users, Settings..." : "https://...",
                className: "w-full px-4 py-2 rounded-xl border border-[var(--border-color)] bg-[var(--bg-secondary)] text-[var(--text-primary)] focus:outline-none focus:ring-2 focus:ring-primary-500"
              }
            )
          ] })
        ] }),
        /* @__PURE__ */ a("div", { children: [
          /* @__PURE__ */ r("label", { className: "block text-sm font-medium text-[var(--text-secondary)] mb-1", children: o("applications.form.route") }),
          /* @__PURE__ */ r(
            "input",
            {
              type: "text",
              value: l.route,
              onChange: (e) => d((t) => ({ ...t, route: e.target.value })),
              placeholder: "/administration/...",
              className: "w-full px-4 py-2 rounded-xl border border-[var(--border-color)] bg-[var(--bg-secondary)] text-[var(--text-primary)] focus:outline-none focus:ring-2 focus:ring-primary-500"
            }
          )
        ] }),
        /* @__PURE__ */ a("div", { className: "flex justify-end gap-2 pt-4", children: [
          /* @__PURE__ */ r(
            "button",
            {
              type: "button",
              onClick: () => y(!1),
              className: "px-4 py-2 rounded-xl bg-[var(--bg-tertiary)] hover:bg-[var(--bg-hover)] text-[var(--text-primary)] font-medium transition-colors",
              children: o("common.cancel")
            }
          ),
          /* @__PURE__ */ a(
            "button",
            {
              type: "submit",
              disabled: f,
              className: "px-4 py-2 rounded-xl bg-primary-600 hover:bg-primary-700 text-white font-medium transition-colors disabled:opacity-50 flex items-center gap-2",
              children: [
                f && /* @__PURE__ */ r(T, { className: "w-4 h-4 animate-spin" }),
                o("common.save")
              ]
            }
          )
        ] })
      ] })
    ] }) }),
    R && c && /* @__PURE__ */ r("div", { className: "fixed inset-0 bg-black/50 flex items-center justify-center z-50", children: /* @__PURE__ */ a("div", { className: "bg-[var(--bg-primary)] rounded-2xl w-full max-w-lg p-6 m-4 max-h-[90vh] overflow-y-auto", children: [
      /* @__PURE__ */ a("div", { className: "flex items-center justify-between mb-6", children: [
        /* @__PURE__ */ r("h2", { className: "text-xl font-bold text-[var(--text-primary)]", children: o("applications.editModule") }),
        /* @__PURE__ */ r("button", { onClick: () => h(!1), className: "p-2 rounded-lg hover:bg-[var(--bg-hover)]", children: /* @__PURE__ */ r(E, { className: "w-5 h-5 text-[var(--text-secondary)]" }) })
      ] }),
      w && /* @__PURE__ */ r("div", { className: "mb-4 p-3 rounded-lg bg-red-100 dark:bg-red-900/30 text-red-700 dark:text-red-300 text-sm", children: w }),
      /* @__PURE__ */ a("form", { onSubmit: G, className: "space-y-4", children: [
        /* @__PURE__ */ a("div", { children: [
          /* @__PURE__ */ r("label", { className: "block text-sm font-medium text-[var(--text-secondary)] mb-1", children: o("applications.form.code") }),
          /* @__PURE__ */ r(
            "input",
            {
              type: "text",
              value: c.module.code,
              disabled: !0,
              className: "w-full px-4 py-2 rounded-xl border border-[var(--border-color)] bg-[var(--bg-tertiary)] text-[var(--text-muted)] cursor-not-allowed"
            }
          )
        ] }),
        /* @__PURE__ */ a("div", { children: [
          /* @__PURE__ */ r("label", { className: "block text-sm font-medium text-[var(--text-secondary)] mb-1", children: o("applications.form.label") }),
          /* @__PURE__ */ r(
            "input",
            {
              type: "text",
              required: !0,
              value: n.label,
              onChange: (e) => b((t) => ({ ...t, label: e.target.value })),
              className: "w-full px-4 py-2 rounded-xl border border-[var(--border-color)] bg-[var(--bg-secondary)] text-[var(--text-primary)] focus:outline-none focus:ring-2 focus:ring-primary-500"
            }
          )
        ] }),
        /* @__PURE__ */ a("div", { className: "grid grid-cols-2 gap-4", children: [
          /* @__PURE__ */ a("div", { children: [
            /* @__PURE__ */ r("label", { className: "block text-sm font-medium text-[var(--text-secondary)] mb-1", children: o("applications.form.iconType") }),
            /* @__PURE__ */ a(
              "select",
              {
                value: n.iconType,
                onChange: (e) => b((t) => ({ ...t, iconType: e.target.value })),
                className: "w-full px-4 py-2 rounded-xl border border-[var(--border-color)] bg-[var(--bg-secondary)] text-[var(--text-primary)] focus:outline-none focus:ring-2 focus:ring-primary-500",
                children: [
                  /* @__PURE__ */ r("option", { value: "Lucide", children: "Lucide Icon" }),
                  /* @__PURE__ */ r("option", { value: "Url", children: "URL" })
                ]
              }
            )
          ] }),
          /* @__PURE__ */ a("div", { children: [
            /* @__PURE__ */ r("label", { className: "block text-sm font-medium text-[var(--text-secondary)] mb-1", children: o("applications.form.icon") }),
            /* @__PURE__ */ r(
              "input",
              {
                type: "text",
                value: n.icon,
                onChange: (e) => b((t) => ({ ...t, icon: e.target.value })),
                className: "w-full px-4 py-2 rounded-xl border border-[var(--border-color)] bg-[var(--bg-secondary)] text-[var(--text-primary)] focus:outline-none focus:ring-2 focus:ring-primary-500"
              }
            )
          ] })
        ] }),
        /* @__PURE__ */ a("div", { children: [
          /* @__PURE__ */ r("label", { className: "block text-sm font-medium text-[var(--text-secondary)] mb-1", children: o("applications.form.route") }),
          /* @__PURE__ */ r(
            "input",
            {
              type: "text",
              value: n.route,
              onChange: (e) => b((t) => ({ ...t, route: e.target.value })),
              className: "w-full px-4 py-2 rounded-xl border border-[var(--border-color)] bg-[var(--bg-secondary)] text-[var(--text-primary)] focus:outline-none focus:ring-2 focus:ring-primary-500"
            }
          )
        ] }),
        /* @__PURE__ */ a("div", { className: "flex justify-end gap-2 pt-4", children: [
          /* @__PURE__ */ r(
            "button",
            {
              type: "button",
              onClick: () => h(!1),
              className: "px-4 py-2 rounded-xl bg-[var(--bg-tertiary)] hover:bg-[var(--bg-hover)] text-[var(--text-primary)] font-medium transition-colors",
              children: o("common.cancel")
            }
          ),
          /* @__PURE__ */ a(
            "button",
            {
              type: "submit",
              disabled: f,
              className: "px-4 py-2 rounded-xl bg-primary-600 hover:bg-primary-700 text-white font-medium transition-colors disabled:opacity-50 flex items-center gap-2",
              children: [
                f && /* @__PURE__ */ r(T, { className: "w-4 h-4 animate-spin" }),
                o("common.save")
              ]
            }
          )
        ] })
      ] })
    ] }) })
  ] });
}
export {
  ne as NavigationAppsPage
};
//# sourceMappingURL=NavigationAppsPage-CeHbxfZw.js.map
