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