import { jsx as a, jsxs as s, Fragment as F } from "react/jsx-runtime";
import { useState as c, useCallback as O, useEffect as L } from "react";
import { useTranslation as P } from "react-i18next";
import { Loader2 as b, Users as U, Bell as w, AlertTriangle as R, Clock as A, X as W, Check as q, Edit as z, TrendingUp as V } from "lucide-react";
import { az as X, o as y, P as _ } from "./index-CeEMms9z.js";
const d = ["Supervisor", "Manager", "Director"], N = {
  None: "bg-[var(--bg-secondary)] text-[var(--text-secondary)]",
  Supervisor: "bg-[var(--info-bg)] text-[var(--info-text)]",
  Manager: "bg-[var(--warning-bg)] text-[var(--warning-text)]",
  Director: "bg-[var(--error-bg)] text-[var(--error-text)]"
}, G = {
  ResponseBreach: A,
  ResolutionBreach: R,
  ManualRequest: w
};
function Z() {
  const { t } = P("support"), [k, f] = c(!0), [v, g] = c(!1), [C, $] = c([]), [u, E] = c([]), [m, h] = c(null), [l, o] = c({ delayAfterBreachMinutes: 0, notifyRoleId: null, isActive: !0 }), p = O(async () => {
    try {
      f(!0);
      const [e, n] = await Promise.all([
        X.getEscalationRules(),
        y.get("/api/administration/permissions/roles")
      ]);
      $(e), E(n);
    } catch (e) {
      console.error("Failed to load escalation data:", e);
    } finally {
      f(!1);
    }
  }, []);
  L(() => {
    p();
  }, [p]);
  const M = (e) => {
    h(e), o({
      delayAfterBreachMinutes: e.delayAfterBreachMinutes,
      notifyRoleId: e.notifyRoleId,
      isActive: e.isActive
    });
  }, B = () => {
    h(null), o({ delayAfterBreachMinutes: 0, notifyRoleId: null, isActive: !0 });
  }, I = async () => {
    if (m)
      try {
        g(!0), await y.put(`/api/support/sla/escalation-rules/${m.id}`, l), p(), h(null);
      } catch (e) {
        console.error("Failed to save escalation rule:", e);
      } finally {
        g(!1);
      }
  }, T = (e) => {
    if (e === 0) return t("escalation.delay.immediate");
    if (e < 60) return `${e} min`;
    const n = Math.floor(e / 60), i = e % 60;
    return i === 0 ? `${n}h` : `${n}h ${i}min`;
  }, j = (e) => e ? u.find((i) => i.id === e)?.name || t("escalation.unknownRole") : t("escalation.notConfigured"), D = C.reduce((e, n) => (e[n.level] || (e[n.level] = []), e[n.level].push(n), e), {});
  return /* @__PURE__ */ a(
    _,
    {
      title: t("escalation.title"),
      subtitle: t("escalation.subtitle"),
      icon: /* @__PURE__ */ a(V, { className: "w-6 h-6" }),
      breadcrumbs: [
        { label: t("title", "Support"), href: "/support" },
        { label: t("escalation.title") }
      ],
      children: k ? /* @__PURE__ */ a("div", { className: "flex items-center justify-center py-12", children: /* @__PURE__ */ a(b, { className: "w-8 h-8 animate-spin text-[var(--info-dot)]" }) }) : /* @__PURE__ */ s(F, { children: [
        /* @__PURE__ */ s("div", { className: "card p-6 bg-[var(--bg-secondary)]", children: [
          /* @__PURE__ */ s("h3", { className: "text-lg font-semibold mb-4 flex items-center gap-2", children: [
            /* @__PURE__ */ a(U, { className: "w-5 h-5" }),
            t("escalation.hierarchy")
          ] }),
          /* @__PURE__ */ a("div", { className: "flex items-center gap-4", children: d.map((e, n) => /* @__PURE__ */ s("div", { className: "flex items-center gap-2", children: [
            /* @__PURE__ */ a("div", { className: `px-4 py-2 rounded-[var(--radius-badge)] font-medium ${N[e]}`, children: t(`escalation.levels.${e}`) }),
            n < 2 && /* @__PURE__ */ a("div", { className: "text-[var(--text-secondary)]", children: "→" })
          ] }, e)) }),
          /* @__PURE__ */ a("p", { className: "mt-4 text-sm text-[var(--text-secondary)]", children: t("escalation.hierarchyDescription") })
        ] }),
        /* @__PURE__ */ a("div", { className: "space-y-6", children: Object.entries(D).filter(([e]) => e !== "None").sort((e, n) => d.indexOf(e[0]) - d.indexOf(n[0])).map(([e, n]) => /* @__PURE__ */ s("div", { className: "card overflow-hidden", children: [
          /* @__PURE__ */ a("div", { className: `px-6 py-3 ${N[e]} border-b border-[var(--border-color)]`, children: /* @__PURE__ */ a("h3", { className: "font-semibold", children: t("escalation.level", {
            number: d.indexOf(e) + 1,
            label: t(`escalation.levels.${e}`)
          }) }) }),
          /* @__PURE__ */ a("div", { className: "divide-y divide-[var(--border-color)]", children: n.map((i) => {
            const x = G[i.triggerType] || R, S = m?.id === i.id;
            return /* @__PURE__ */ a(
              "div",
              {
                className: `p-4 ${i.isActive ? "" : "opacity-60"}`,
                children: S ? /* @__PURE__ */ s("div", { className: "space-y-4", children: [
                  /* @__PURE__ */ s("div", { className: "flex items-center gap-2", children: [
                    /* @__PURE__ */ a(x, { className: "w-5 h-5" }),
                    /* @__PURE__ */ a("span", { className: "font-medium", children: t(`escalation.triggerTypes.${i.triggerType}`) })
                  ] }),
                  /* @__PURE__ */ s("div", { className: "grid grid-cols-1 md:grid-cols-3 gap-4", children: [
                    /* @__PURE__ */ s("div", { children: [
                      /* @__PURE__ */ a("label", { className: "block text-sm font-medium mb-1", children: t("escalation.form.delayAfterBreach") }),
                      /* @__PURE__ */ s("div", { className: "flex items-center gap-2", children: [
                        /* @__PURE__ */ a(
                          "input",
                          {
                            type: "number",
                            min: "0",
                            value: l.delayAfterBreachMinutes,
                            onChange: (r) => o({
                              ...l,
                              delayAfterBreachMinutes: parseInt(r.target.value) || 0
                            }),
                            className: "input w-24"
                          }
                        ),
                        /* @__PURE__ */ a("span", { className: "text-sm text-[var(--text-secondary)]", children: t("escalation.form.minutes") })
                      ] })
                    ] }),
                    /* @__PURE__ */ s("div", { children: [
                      /* @__PURE__ */ a("label", { className: "block text-sm font-medium mb-1", children: t("escalation.form.notifyRole") }),
                      /* @__PURE__ */ s(
                        "select",
                        {
                          value: l.notifyRoleId || "",
                          onChange: (r) => o({
                            ...l,
                            notifyRoleId: r.target.value || null
                          }),
                          className: "input w-full",
                          children: [
                            /* @__PURE__ */ a("option", { value: "", children: t("escalation.form.none") }),
                            u.map((r) => /* @__PURE__ */ a("option", { value: r.id, children: r.name }, r.id))
                          ]
                        }
                      )
                    ] }),
                    /* @__PURE__ */ s("div", { children: [
                      /* @__PURE__ */ a("label", { className: "block text-sm font-medium mb-1", children: t("escalation.form.status") }),
                      /* @__PURE__ */ s("label", { className: "flex items-center gap-2", children: [
                        /* @__PURE__ */ a(
                          "input",
                          {
                            type: "checkbox",
                            checked: l.isActive,
                            onChange: (r) => o({
                              ...l,
                              isActive: r.target.checked
                            }),
                            className: "rounded border-[var(--border-color)]"
                          }
                        ),
                        /* @__PURE__ */ a("span", { className: "text-sm", children: t("escalation.form.active") })
                      ] })
                    ] })
                  ] }),
                  /* @__PURE__ */ s("div", { className: "flex justify-end gap-2", children: [
                    /* @__PURE__ */ s(
                      "button",
                      {
                        onClick: B,
                        className: "btn btn-secondary flex items-center gap-1",
                        children: [
                          /* @__PURE__ */ a(W, { className: "w-4 h-4" }),
                          t("common:actions.cancel")
                        ]
                      }
                    ),
                    /* @__PURE__ */ s(
                      "button",
                      {
                        onClick: I,
                        disabled: v,
                        className: "btn btn-primary flex items-center gap-1",
                        children: [
                          v ? /* @__PURE__ */ a(b, { className: "w-4 h-4 animate-spin" }) : /* @__PURE__ */ a(q, { className: "w-4 h-4" }),
                          t("common:actions.save")
                        ]
                      }
                    )
                  ] })
                ] }) : /* @__PURE__ */ s("div", { className: "flex items-center justify-between", children: [
                  /* @__PURE__ */ s("div", { className: "flex items-center gap-4", children: [
                    /* @__PURE__ */ s("div", { className: "flex items-center gap-2", children: [
                      /* @__PURE__ */ a(x, { className: "w-5 h-5 text-[var(--text-secondary)]" }),
                      /* @__PURE__ */ a("span", { className: "font-medium", children: t(`escalation.triggerTypes.${i.triggerType}`) }),
                      i.isSystemDefault ? /* @__PURE__ */ a("span", { className: "text-[10px] px-1.5 py-0.5 rounded bg-[var(--bg-secondary)] text-[var(--text-secondary)] border border-[var(--border-color)]", children: t("escalation.scope.system") }) : /* @__PURE__ */ a("span", { className: "text-[10px] px-1.5 py-0.5 rounded bg-[var(--info-bg)] text-[var(--info-text)] border border-[var(--info-border)]", children: t("escalation.scope.tenant") })
                    ] }),
                    /* @__PURE__ */ s("div", { className: "text-sm text-[var(--text-secondary)]", children: [
                      /* @__PURE__ */ a(A, { className: "w-4 h-4 inline mr-1" }),
                      T(i.delayAfterBreachMinutes)
                    ] }),
                    /* @__PURE__ */ s("div", { className: "text-sm text-[var(--text-secondary)]", children: [
                      /* @__PURE__ */ a(w, { className: "w-4 h-4 inline mr-1" }),
                      j(i.notifyRoleId)
                    ] }),
                    !i.isActive && /* @__PURE__ */ a("span", { className: "text-xs px-2 py-0.5 rounded bg-[var(--bg-secondary)] text-[var(--text-secondary)]", children: t("escalation.disabled") })
                  ] }),
                  /* @__PURE__ */ s(
                    "button",
                    {
                      onClick: () => M(i),
                      className: "btn btn-secondary btn-sm flex items-center gap-1",
                      children: [
                        /* @__PURE__ */ a(z, { className: "w-4 h-4" }),
                        t("common:actions.edit")
                      ]
                    }
                  )
                ] })
              },
              i.id
            );
          }) })
        ] }, e)) }),
        /* @__PURE__ */ s("div", { className: "card p-6", children: [
          /* @__PURE__ */ a("h3", { className: "text-lg font-semibold mb-4", children: t("escalation.howItWorks.title") }),
          /* @__PURE__ */ a("div", { className: "space-y-4 text-sm text-[var(--text-secondary)]", children: [1, 2, 3, 4].map((e) => /* @__PURE__ */ s("div", { className: "flex gap-3", children: [
            /* @__PURE__ */ a("div", { className: "w-6 h-6 rounded-full bg-[var(--info-bg)] text-[var(--info-text)] flex items-center justify-center text-xs font-bold flex-shrink-0", children: e }),
            /* @__PURE__ */ a("p", { children: t(`escalation.howItWorks.step${e}`) })
          ] }, e)) })
        ] })
      ] })
    }
  );
}
export {
  Z as EscalationConfigPage
};
//# sourceMappingURL=EscalationConfigPage-GsHifbE3.js.map
