import { jsxs as s, jsx as t, Fragment as O } from "react/jsx-runtime";
import { useState as l, useCallback as q, useEffect as H } from "react";
import { useTranslation as _ } from "react-i18next";
import { Plus as K, AlertCircle as Y, Loader2 as S, Search as z, Edit as G, Trash2 as Q } from "lucide-react";
import { a5 as h, B as U } from "./index-Dh9kUeDn.js";
const P = [
  "RoundRobin",
  "LoadBalancing",
  "SkillBased",
  "Affinity",
  "PriorityBased"
], J = ["Low", "Medium", "High", "Critical"], V = ["Bug", "FeatureRequest", "Support", "Question", "Suggestion", "ApplicationRequest"], k = {
  name: "",
  priority: 0,
  strategyType: "RoundRobin",
  ticketPriorityFilter: null,
  ticketTypeFilter: null,
  maxConcurrentTickets: 10,
  isFallback: !1
};
function W(a, c) {
  if (!a.ticketPriorityFilter && !a.ticketTypeFilter)
    return c("assignment.noFilter");
  const o = [];
  return a.ticketPriorityFilter && o.push(c(`tickets.priorities.${a.ticketPriorityFilter}`)), a.ticketTypeFilter && o.push(c(`tickets.types.${a.ticketTypeFilter}`)), o.join(" + ");
}
function se() {
  const { t: a } = _("support"), [c, o] = l(!0), [F, m] = l(null), [u, R] = l([]), [T, A] = l(""), [x, E] = l(""), [g, $] = l(""), [L, y] = l(!1), [b, v] = l(null), [i, n] = l(k), [r, w] = l(!1), p = q(async () => {
    try {
      o(!0), m(null);
      const e = await h.getAll();
      R(e);
    } catch (e) {
      m(e instanceof Error ? e.message : "Failed to load assignment rules"), console.error("Failed to load assignment rules:", e);
    } finally {
      o(!1);
    }
  }, []);
  H(() => {
    p();
  }, [p]);
  const j = u.filter((e) => {
    const N = e.name.toLowerCase().includes(T.toLowerCase()), d = !x || e.strategyType === x, D = !g || (g === "active" ? e.isActive : !e.isActive);
    return N && d && D;
  }), C = () => {
    v(null), n(k), y(!0);
  }, I = (e) => {
    v(e), n({
      name: e.name,
      priority: e.priority,
      strategyType: e.strategyType,
      ticketPriorityFilter: e.ticketPriorityFilter,
      ticketTypeFilter: e.ticketTypeFilter,
      maxConcurrentTickets: e.maxConcurrentTickets,
      isFallback: e.isFallback
    }), y(!0);
  }, f = () => {
    y(!1), v(null), n(k);
  }, B = async () => {
    if (!i.name.trim()) {
      alert(a("assignment.form.nameRequired"));
      return;
    }
    try {
      w(!0), m(null);
      const e = {
        name: i.name,
        priority: i.priority,
        strategyType: i.strategyType,
        ticketPriorityFilter: i.ticketPriorityFilter || void 0,
        ticketTypeFilter: i.ticketTypeFilter || void 0,
        maxConcurrentTickets: i.maxConcurrentTickets,
        isFallback: i.isFallback
      };
      b ? await h.update(b.id, e) : await h.create(e), await p(), f();
    } catch (e) {
      m(e instanceof Error ? e.message : "Failed to save rule"), console.error("Failed to save rule:", e);
    } finally {
      w(!1);
    }
  }, M = async (e, N) => {
    if (window.confirm(a("assignment.confirmDelete", { name: N })))
      try {
        m(null), await h.delete(e), await p();
      } catch (d) {
        m(d instanceof Error ? d.message : "Failed to delete rule"), console.error("Failed to delete rule:", d);
      }
  };
  return /* @__PURE__ */ s("div", { className: "space-y-6", children: [
    /* @__PURE__ */ t(
      U,
      {
        items: [
          { label: a("title", "Support"), href: "/support" },
          { label: a("assignment.title") }
        ]
      }
    ),
    /* @__PURE__ */ s("div", { className: "flex items-center justify-between", children: [
      /* @__PURE__ */ s("div", { children: [
        /* @__PURE__ */ t("h1", { className: "text-2xl font-bold", children: a("assignment.title") }),
        /* @__PURE__ */ t("p", { className: "text-[var(--text-secondary)]", children: a("assignment.subtitle", "Manage auto-assignment rules for tickets") })
      ] }),
      /* @__PURE__ */ s(
        "button",
        {
          onClick: C,
          className: "btn btn-primary flex items-center gap-2",
          children: [
            /* @__PURE__ */ t(K, { className: "w-4 h-4" }),
            a("assignment.create")
          ]
        }
      )
    ] }),
    F && /* @__PURE__ */ s("div", { className: "card p-4 bg-[var(--error-bg)] text-[var(--error-text)] border border-[var(--error-border)] flex items-start gap-3", children: [
      /* @__PURE__ */ t(Y, { className: "w-5 h-5 flex-shrink-0 mt-0.5" }),
      /* @__PURE__ */ s("div", { children: [
        /* @__PURE__ */ t("p", { className: "font-medium", children: a("common:error") }),
        /* @__PURE__ */ t("p", { className: "text-sm", children: F })
      ] })
    ] }),
    c && /* @__PURE__ */ t("div", { className: "flex items-center justify-center py-12", children: /* @__PURE__ */ t(S, { className: "w-8 h-8 animate-spin text-[var(--color-primary-600)]" }) }),
    !c && u.length === 0 && /* @__PURE__ */ s("div", { className: "card p-12 text-center", children: [
      /* @__PURE__ */ t("p", { className: "text-xl font-semibold mb-2", children: a("assignment.noRules") }),
      /* @__PURE__ */ t("p", { className: "text-[var(--text-secondary)] mb-6", children: a("assignment.noRulesSubtitle", "Create your first assignment rule to get started") }),
      /* @__PURE__ */ t(
        "button",
        {
          onClick: C,
          className: "btn btn-primary",
          children: a("assignment.create")
        }
      )
    ] }),
    !c && u.length > 0 && /* @__PURE__ */ s(O, { children: [
      /* @__PURE__ */ s("div", { className: "card p-4 space-y-4", children: [
        /* @__PURE__ */ s("div", { children: [
          /* @__PURE__ */ t("label", { className: "block text-sm font-medium mb-2", children: a("assignment.search") }),
          /* @__PURE__ */ s("div", { className: "relative", children: [
            /* @__PURE__ */ t(z, { className: "absolute left-3 top-3 w-4 h-4 text-[var(--text-secondary)]" }),
            /* @__PURE__ */ t(
              "input",
              {
                type: "text",
                placeholder: a("assignment.searchPlaceholder", "Search by name..."),
                value: T,
                onChange: (e) => A(e.target.value),
                className: "input w-full pl-10"
              }
            )
          ] })
        ] }),
        /* @__PURE__ */ s("div", { className: "grid grid-cols-1 md:grid-cols-2 gap-4", children: [
          /* @__PURE__ */ s("div", { children: [
            /* @__PURE__ */ t("label", { className: "block text-sm font-medium mb-2", children: a("assignment.strategy") }),
            /* @__PURE__ */ s(
              "select",
              {
                value: x,
                onChange: (e) => E(e.target.value),
                className: "input w-full",
                children: [
                  /* @__PURE__ */ t("option", { value: "", children: a("assignment.allStrategies", "All Strategies") }),
                  P.map((e) => /* @__PURE__ */ t("option", { value: e, children: a(`assignment.strategyTypes.${e.charAt(0).toLowerCase()}${e.slice(1)}`) }, e))
                ]
              }
            )
          ] }),
          /* @__PURE__ */ s("div", { children: [
            /* @__PURE__ */ t("label", { className: "block text-sm font-medium mb-2", children: a("assignment.status") }),
            /* @__PURE__ */ s(
              "select",
              {
                value: g,
                onChange: (e) => $(e.target.value),
                className: "input w-full",
                children: [
                  /* @__PURE__ */ t("option", { value: "", children: a("assignment.allStatus", "All Status") }),
                  /* @__PURE__ */ t("option", { value: "active", children: a("common:active") }),
                  /* @__PURE__ */ t("option", { value: "inactive", children: a("common:inactive") })
                ]
              }
            )
          ] })
        ] })
      ] }),
      /* @__PURE__ */ t("div", { className: "card overflow-hidden", children: /* @__PURE__ */ s("table", { className: "w-full", children: [
        /* @__PURE__ */ t("thead", { className: "border-b border-[var(--border-color)] bg-[var(--bg-secondary)]", children: /* @__PURE__ */ s("tr", { children: [
          /* @__PURE__ */ t("th", { className: "px-6 py-3 text-left text-sm font-semibold text-[var(--text-secondary)]", children: a("assignment.name") }),
          /* @__PURE__ */ t("th", { className: "px-6 py-3 text-left text-sm font-semibold text-[var(--text-secondary)]", children: a("assignment.strategy") }),
          /* @__PURE__ */ t("th", { className: "px-6 py-3 text-left text-sm font-semibold text-[var(--text-secondary)]", children: a("assignment.priority") }),
          /* @__PURE__ */ t("th", { className: "px-6 py-3 text-left text-sm font-semibold text-[var(--text-secondary)]", children: a("assignment.filters") }),
          /* @__PURE__ */ t("th", { className: "px-6 py-3 text-left text-sm font-semibold text-[var(--text-secondary)]", children: a("assignment.maxTickets") }),
          /* @__PURE__ */ t("th", { className: "px-6 py-3 text-left text-sm font-semibold text-[var(--text-secondary)]", children: a("assignment.active") }),
          /* @__PURE__ */ t("th", { className: "px-6 py-3 text-right text-sm font-semibold text-[var(--text-secondary)]", children: a("common:actions.title") })
        ] }) }),
        /* @__PURE__ */ t("tbody", { className: "divide-y divide-[var(--border-color)]", children: j.map((e) => /* @__PURE__ */ s("tr", { className: "hover:bg-[var(--bg-secondary)] transition", children: [
          /* @__PURE__ */ t("td", { className: "px-6 py-4 text-sm font-medium", children: e.name }),
          /* @__PURE__ */ t("td", { className: "px-6 py-4 text-sm text-[var(--text-secondary)]", children: a(`assignment.strategyTypes.${e.strategyType.charAt(0).toLowerCase()}${e.strategyType.slice(1)}`) }),
          /* @__PURE__ */ t("td", { className: "px-6 py-4 text-sm text-[var(--text-secondary)]", children: e.priority }),
          /* @__PURE__ */ t("td", { className: "px-6 py-4 text-sm text-[var(--text-secondary)]", children: /* @__PURE__ */ t("span", { className: "inline-block text-xs bg-[var(--bg-secondary)] px-2 py-1 rounded", children: W(e, a) }) }),
          /* @__PURE__ */ t("td", { className: "px-6 py-4 text-sm text-[var(--text-secondary)]", children: e.maxConcurrentTickets }),
          /* @__PURE__ */ t("td", { className: "px-6 py-4 text-sm", children: /* @__PURE__ */ t("span", { className: `px-3 py-1 rounded-full text-xs font-medium ${e.isActive ? "bg-[var(--success-bg)] text-[var(--success-text)]" : "bg-[var(--bg-secondary)] text-[var(--text-secondary)]"}`, children: e.isActive ? a("common:active") : a("common:inactive") }) }),
          /* @__PURE__ */ t("td", { className: "px-6 py-4 text-right", children: /* @__PURE__ */ s("div", { className: "flex items-center justify-end gap-2", children: [
            /* @__PURE__ */ s(
              "button",
              {
                onClick: () => I(e),
                className: "btn btn-secondary btn-sm flex items-center gap-1",
                children: [
                  /* @__PURE__ */ t(G, { className: "w-4 h-4" }),
                  a("common:actions.edit")
                ]
              }
            ),
            /* @__PURE__ */ s(
              "button",
              {
                onClick: () => M(e.id, e.name),
                className: "btn btn-secondary btn-sm flex items-center gap-1 text-[var(--error-text)]",
                children: [
                  /* @__PURE__ */ t(Q, { className: "w-4 h-4" }),
                  a("common:actions.delete")
                ]
              }
            )
          ] }) })
        ] }, e.id)) })
      ] }) })
    ] }),
    L && /* @__PURE__ */ t("div", { className: "fixed inset-0 bg-black/50 flex items-center justify-center z-50 p-4", children: /* @__PURE__ */ s("div", { className: "bg-[var(--bg-primary)] rounded-lg shadow-xl max-w-2xl w-full max-h-[90vh] overflow-y-auto", children: [
      /* @__PURE__ */ s("div", { className: "sticky top-0 bg-[var(--bg-secondary)] border-b border-[var(--border-color)] px-6 py-4 flex items-center justify-between", children: [
        /* @__PURE__ */ t("h2", { className: "text-lg font-semibold", children: a(b ? "assignment.edit" : "assignment.create") }),
        /* @__PURE__ */ t(
          "button",
          {
            onClick: f,
            disabled: r,
            className: "text-[var(--text-secondary)] hover:text-[var(--text-primary)]",
            children: "✕"
          }
        )
      ] }),
      /* @__PURE__ */ s("div", { className: "p-6 space-y-4", children: [
        /* @__PURE__ */ s("div", { children: [
          /* @__PURE__ */ s("label", { className: "block text-sm font-medium mb-1", children: [
            a("assignment.name"),
            " ",
            /* @__PURE__ */ t("span", { className: "text-[var(--error-text)]", children: "*" })
          ] }),
          /* @__PURE__ */ t(
            "input",
            {
              type: "text",
              value: i.name,
              onChange: (e) => n({ ...i, name: e.target.value }),
              placeholder: a("assignment.form.namePlaceholder", "Rule name"),
              className: "input w-full",
              disabled: r
            }
          )
        ] }),
        /* @__PURE__ */ s("div", { className: "grid grid-cols-1 md:grid-cols-2 gap-4", children: [
          /* @__PURE__ */ s("div", { children: [
            /* @__PURE__ */ t("label", { className: "block text-sm font-medium mb-1", children: a("assignment.priority") }),
            /* @__PURE__ */ t(
              "input",
              {
                type: "number",
                value: i.priority,
                onChange: (e) => n({ ...i, priority: parseInt(e.target.value) || 0 }),
                placeholder: "0",
                className: "input w-full",
                disabled: r
              }
            ),
            /* @__PURE__ */ t("p", { className: "text-xs text-[var(--text-secondary)] mt-1", children: a("assignment.form.priorityHint", "Lower number = higher priority") })
          ] }),
          /* @__PURE__ */ s("div", { children: [
            /* @__PURE__ */ s("label", { className: "block text-sm font-medium mb-1", children: [
              a("assignment.strategy"),
              " ",
              /* @__PURE__ */ t("span", { className: "text-[var(--error-text)]", children: "*" })
            ] }),
            /* @__PURE__ */ t(
              "select",
              {
                value: i.strategyType,
                onChange: (e) => n({ ...i, strategyType: e.target.value }),
                className: "input w-full",
                disabled: r,
                children: P.map((e) => /* @__PURE__ */ t("option", { value: e, children: a(`assignment.strategyTypes.${e.charAt(0).toLowerCase()}${e.slice(1)}`) }, e))
              }
            )
          ] })
        ] }),
        /* @__PURE__ */ s("div", { className: "grid grid-cols-1 md:grid-cols-2 gap-4", children: [
          /* @__PURE__ */ s("div", { children: [
            /* @__PURE__ */ t("label", { className: "block text-sm font-medium mb-1", children: a("assignment.form.ticketPriority", "Ticket Priority Filter") }),
            /* @__PURE__ */ s(
              "select",
              {
                value: i.ticketPriorityFilter || "",
                onChange: (e) => n({ ...i, ticketPriorityFilter: e.target.value || null }),
                className: "input w-full",
                disabled: r,
                children: [
                  /* @__PURE__ */ t("option", { value: "", children: a("assignment.form.noFilter", "No filter") }),
                  J.map((e) => /* @__PURE__ */ t("option", { value: e, children: a(`tickets.priorities.${e}`) }, e))
                ]
              }
            )
          ] }),
          /* @__PURE__ */ s("div", { children: [
            /* @__PURE__ */ t("label", { className: "block text-sm font-medium mb-1", children: a("assignment.form.ticketType", "Ticket Type Filter") }),
            /* @__PURE__ */ s(
              "select",
              {
                value: i.ticketTypeFilter || "",
                onChange: (e) => n({ ...i, ticketTypeFilter: e.target.value || null }),
                className: "input w-full",
                disabled: r,
                children: [
                  /* @__PURE__ */ t("option", { value: "", children: a("assignment.form.noFilter", "No filter") }),
                  V.map((e) => /* @__PURE__ */ t("option", { value: e, children: a(`tickets.types.${e}`) }, e))
                ]
              }
            )
          ] })
        ] }),
        /* @__PURE__ */ s("div", { children: [
          /* @__PURE__ */ t("label", { className: "block text-sm font-medium mb-1", children: a("assignment.maxTickets") }),
          /* @__PURE__ */ t(
            "input",
            {
              type: "number",
              min: "1",
              value: i.maxConcurrentTickets,
              onChange: (e) => n({ ...i, maxConcurrentTickets: Math.max(1, parseInt(e.target.value) || 1) }),
              className: "input w-full",
              disabled: r
            }
          )
        ] }),
        /* @__PURE__ */ s("div", { className: "flex items-center gap-2", children: [
          /* @__PURE__ */ t(
            "input",
            {
              type: "checkbox",
              id: "isFallback",
              checked: i.isFallback,
              onChange: (e) => n({ ...i, isFallback: e.target.checked }),
              className: "rounded border-[var(--border-color)]",
              disabled: r
            }
          ),
          /* @__PURE__ */ t("label", { htmlFor: "isFallback", className: "text-sm font-medium", children: a("assignment.fallback") }),
          /* @__PURE__ */ t("p", { className: "text-xs text-[var(--text-secondary)] ml-auto", children: a("assignment.form.fallbackHint", "Used when other rules do not match") })
        ] })
      ] }),
      /* @__PURE__ */ s("div", { className: "border-t border-[var(--border-color)] px-6 py-4 flex justify-end gap-2 bg-[var(--bg-secondary)]", children: [
        /* @__PURE__ */ t(
          "button",
          {
            onClick: f,
            disabled: r,
            className: "btn btn-secondary",
            children: a("common:actions.cancel")
          }
        ),
        /* @__PURE__ */ s(
          "button",
          {
            onClick: B,
            disabled: r,
            className: "btn btn-primary flex items-center gap-2",
            children: [
              r && /* @__PURE__ */ t(S, { className: "w-4 h-4 animate-spin" }),
              a("common:actions.save")
            ]
          }
        )
      ] })
    ] }) })
  ] });
}
export {
  se as AssignmentRulesPage
};
//# sourceMappingURL=AssignmentRulesPage-Bm80cujQ.js.map
