import { jsx as t, jsxs as a } from "react/jsx-runtime";
import { useState as i, useRef as Z, useMemo as d, useEffect as ee, useCallback as M } from "react";
import { useTranslation as te } from "react-i18next";
import { Globe as Y, Building2 as $, ChevronDown as re, X as z, AlertCircle as ae, Search as G, Filter as ne, Loader2 as _, Trash2 as se, Lock as oe, Plus as le } from "lucide-react";
import { v as ie } from "./index-CeEMms9z.js";
import { C as ce, D as de, g as me } from "./roleUtils-Cv7WloWa.js";
function fe({
  roles: b,
  targets: l,
  initialTargetId: O,
  lockTarget: Q = !1,
  onTargetChange: C,
  assignedForTarget: R,
  onAssign: A,
  onRemove: k,
  onClose: I,
  title: q,
  subtitle: T
}) {
  const { t: n } = te(["admin", "groups"]), [f, K] = i(O), [m, P] = i(""), [u, U] = i("all"), [p, S] = i("all"), [X, L] = i(null), [g, x] = i(null), [E, y] = i(null), N = Z(!1), o = d(
    () => l.find((e) => e.id === f) ?? l[0],
    [l, f]
  ), v = o?.kind, w = o?.tenantId, H = !Q && l.length > 1;
  ee(() => {
    if (v !== "tenant" || !w) {
      x(null);
      return;
    }
    let e = !1;
    return x(null), ie.tenants.getApplications(w).then((r) => {
      e || x(new Set(r.map((s) => s.applicationId)));
    }).catch(() => {
      e || x(null);
    }), () => {
      e = !0;
    };
  }, [v, w]);
  const c = d(() => v !== "tenant" || !g ? b : b.filter((e) => !e.applicationId || g.has(e.applicationId)), [b, v, g]), F = d(() => {
    const e = /* @__PURE__ */ new Map();
    for (const r of c)
      !r.applicationId || !r.applicationName || e.has(r.applicationId) || e.set(r.applicationId, r.applicationName);
    return Array.from(e.entries()).map(([r, s]) => ({ id: r, name: s })).sort((r, s) => r.name.localeCompare(s.name));
  }, [c]), J = d(() => {
    const e = new Set(c.map((r) => r.category || "Custom"));
    return Array.from(e).sort((r, s) => r.localeCompare(s));
  }, [c]), j = d(() => {
    let e = c;
    if (p !== "all" && (e = e.filter((r) => r.applicationId === p)), u !== "all" && (e = e.filter((r) => (r.category || "Custom") === u)), m.trim()) {
      const r = m.toLowerCase().trim();
      e = e.filter((s) => s.name.toLowerCase().includes(r) || s.shortName?.toLowerCase().includes(r) || s.description?.toLowerCase().includes(r));
    }
    return e;
  }, [c, p, u, m]), V = d(() => {
    const e = /* @__PURE__ */ new Map();
    for (const r of R) e.set(r.roleId, r);
    return e;
  }, [R]), W = M((e) => {
    const r = l.find((s) => s.id === e);
    r && (K(e), S("all"), C?.(r));
  }, [l, C]), B = M(async (e, r) => {
    if (!(N.current || !o)) {
      N.current = !0, L(e), y(null);
      try {
        r === "assign" ? await A(o, e) : await k(o, e);
      } catch (s) {
        console.error(`Failed to ${r} role:`, s), y(r === "assign" ? n("admin:userRoles.errors.assignFailed", "Erreur lors de l'assignation du rôle") : n("admin:userRoles.errors.removeFailed", "Erreur lors du retrait du rôle"));
      } finally {
        L(null), N.current = !1;
      }
    }
  }, [o, A, k, n]);
  return /* @__PURE__ */ t("div", { className: "fixed inset-0 z-[100] flex items-center justify-center p-4 bg-black/60 backdrop-blur-sm", children: /* @__PURE__ */ a("div", { className: "relative w-full max-w-2xl h-[80vh] bg-[var(--bg-card)] rounded-[var(--radius-card)] shadow-2xl border border-[var(--border-color)] overflow-hidden flex flex-col animate-in fade-in zoom-in duration-200", children: [
    /* @__PURE__ */ a("div", { className: "flex items-start justify-between px-6 py-4 border-b border-[var(--border-color)] gap-3", children: [
      /* @__PURE__ */ a("div", { className: "min-w-0", children: [
        /* @__PURE__ */ a("div", { className: "flex items-center gap-3 flex-wrap", children: [
          /* @__PURE__ */ t("h2", { className: "text-lg font-semibold text-[var(--text-primary)]", children: q ?? n("admin:userRoles.assignRole", "Assigner un rôle") }),
          H ? /* @__PURE__ */ a("div", { className: "relative inline-flex items-center", children: [
            o?.kind === "global" ? /* @__PURE__ */ t(Y, { className: "absolute left-2.5 w-3.5 h-3.5 pointer-events-none text-[var(--info-text)]" }) : /* @__PURE__ */ t($, { className: "absolute left-2.5 w-3.5 h-3.5 pointer-events-none text-[var(--color-accent-600)]" }),
            /* @__PURE__ */ t(
              "select",
              {
                value: f,
                onChange: (e) => W(e.target.value),
                "aria-label": n("admin:userRoles.scope.targetLabel", "Cible"),
                className: "pl-8 pr-8 py-1 text-xs font-medium rounded-full bg-[var(--bg-secondary)] border border-[var(--border-color)] text-[var(--text-primary)] appearance-none focus:outline-none focus:ring-2 focus:ring-[var(--color-accent-500)]",
                children: l.map((e) => /* @__PURE__ */ t("option", { value: e.id, children: e.label }, e.id))
              }
            ),
            /* @__PURE__ */ t(re, { className: "absolute right-2 w-3.5 h-3.5 pointer-events-none text-[var(--text-tertiary)]" })
          ] }) : o?.kind === "global" ? /* @__PURE__ */ a("span", { className: "inline-flex items-center gap-1.5 px-2.5 py-1 text-xs font-medium rounded-full bg-[var(--info-bg)] text-[var(--info-text)] border border-[var(--info-border)]", children: [
            /* @__PURE__ */ t(Y, { className: "w-3.5 h-3.5" }),
            n("admin:userRoles.scope.modalTarget", "Cible : {{scope}}", { scope: o?.label })
          ] }) : /* @__PURE__ */ a("span", { className: "inline-flex items-center gap-1.5 px-2.5 py-1 text-xs font-medium rounded-full bg-[var(--accent-bg)] text-[var(--color-accent-700)] border border-[var(--color-accent-300)]", children: [
            /* @__PURE__ */ t($, { className: "w-3.5 h-3.5" }),
            n("admin:userRoles.scope.modalTarget", "Cible : {{scope}}", { scope: o?.label })
          ] })
        ] }),
        T && /* @__PURE__ */ t("p", { className: "text-sm text-[var(--text-secondary)] mt-1 truncate", children: T })
      ] }),
      /* @__PURE__ */ t(
        "button",
        {
          type: "button",
          onClick: I,
          className: "p-1.5 rounded-[var(--radius-button)] text-[var(--text-tertiary)] hover:text-[var(--text-primary)] hover:bg-[var(--bg-hover)] transition-colors shrink-0",
          children: /* @__PURE__ */ t(z, { className: "w-5 h-5" })
        }
      )
    ] }),
    E && /* @__PURE__ */ a("div", { className: "mx-6 mt-3 p-3 rounded-[var(--radius-card)] bg-[var(--error-bg)] border border-[var(--error-border)] flex items-center gap-3", children: [
      /* @__PURE__ */ t(ae, { className: "w-4 h-4 text-[var(--error-text)] flex-shrink-0" }),
      /* @__PURE__ */ t("p", { className: "flex-1 text-sm text-[var(--error-text)]", children: E }),
      /* @__PURE__ */ t("button", { type: "button", onClick: () => y(null), children: /* @__PURE__ */ t(z, { className: "w-3.5 h-3.5 text-[var(--error-text)]" }) })
    ] }),
    /* @__PURE__ */ a("div", { className: "px-6 py-3 border-b border-[var(--border-color)] flex flex-col gap-3", children: [
      /* @__PURE__ */ a("div", { className: "relative", children: [
        /* @__PURE__ */ t(G, { className: "absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-[var(--text-muted)]" }),
        /* @__PURE__ */ t(
          "input",
          {
            type: "text",
            value: m,
            onChange: (e) => P(e.target.value),
            placeholder: n("admin:userRoles.search", "Rechercher un rôle..."),
            className: "w-full pl-10 pr-4 py-2 text-sm bg-[var(--bg-secondary)] border border-[var(--border-color)] rounded-[var(--radius-input)] text-[var(--text-primary)] placeholder-[var(--text-muted)] focus:outline-none focus:ring-2 focus:ring-[var(--color-accent-500)]",
            autoFocus: !0
          }
        )
      ] }),
      /* @__PURE__ */ a("div", { className: "flex items-center gap-2", children: [
        /* @__PURE__ */ t(ne, { className: "w-4 h-4 text-[var(--text-secondary)] shrink-0" }),
        /* @__PURE__ */ a(
          "select",
          {
            value: p,
            onChange: (e) => S(e.target.value),
            disabled: F.length === 0,
            className: "flex-1 min-w-0 px-3 py-2 text-sm bg-[var(--bg-secondary)] border border-[var(--border-color)] rounded-[var(--radius-input)] text-[var(--text-primary)] focus:outline-none focus:ring-2 focus:ring-[var(--color-accent-500)] disabled:opacity-50",
            children: [
              /* @__PURE__ */ t("option", { value: "all", children: n("admin:userRoles.filterByApplication", "Toutes les applications") }),
              F.map((e) => /* @__PURE__ */ t("option", { value: e.id, children: e.name }, e.id))
            ]
          }
        ),
        /* @__PURE__ */ a(
          "select",
          {
            value: u,
            onChange: (e) => U(e.target.value),
            className: "flex-1 min-w-0 px-3 py-2 text-sm bg-[var(--bg-secondary)] border border-[var(--border-color)] rounded-[var(--radius-input)] text-[var(--text-primary)] focus:outline-none focus:ring-2 focus:ring-[var(--color-accent-500)]",
            children: [
              /* @__PURE__ */ t("option", { value: "all", children: n("admin:userRoles.filterByCategory", "Toutes les catégories") }),
              J.map((e) => /* @__PURE__ */ t("option", { value: e, children: e }, e))
            ]
          }
        )
      ] })
    ] }),
    /* @__PURE__ */ t("div", { className: "flex-1 overflow-y-auto", children: /* @__PURE__ */ a("table", { className: "w-full", children: [
      /* @__PURE__ */ t("thead", { className: "bg-[var(--bg-tertiary)] sticky top-0 z-10", children: /* @__PURE__ */ a("tr", { children: [
        /* @__PURE__ */ t("th", { className: "text-left px-6 py-2.5 text-sm font-medium text-[var(--text-secondary)]", children: n("admin:roles.name", "Rôle") }),
        /* @__PURE__ */ t("th", { className: "text-left px-4 py-2.5 text-sm font-medium text-[var(--text-secondary)]", children: n("admin:roles.category", "Catégorie") }),
        /* @__PURE__ */ t("th", { className: "text-right px-6 py-2.5 text-sm font-medium text-[var(--text-secondary)] w-36", children: n("admin:roles.actions", "Actions") })
      ] }) }),
      /* @__PURE__ */ t("tbody", { className: "divide-y divide-[var(--border-color)]", children: j.length === 0 ? /* @__PURE__ */ t("tr", { children: /* @__PURE__ */ a("td", { colSpan: 3, className: "px-6 py-12 text-center", children: [
        /* @__PURE__ */ t(G, { className: "w-8 h-8 mx-auto mb-2 text-[var(--text-tertiary)]" }),
        /* @__PURE__ */ t("p", { className: "text-[var(--text-muted)]", children: n("admin:userRoles.noRoles", "Aucun rôle trouvé") })
      ] }) }) : j.map((e) => {
        const r = V.get(e.id), s = ce[e.category || "Custom"] || de, D = me(e.code), h = X === e.id;
        return /* @__PURE__ */ a(
          "tr",
          {
            className: `transition-colors ${r ? "bg-[var(--accent-bg)]" : "hover:bg-[var(--bg-hover)]"}`,
            children: [
              /* @__PURE__ */ t("td", { className: "px-6 py-3", children: /* @__PURE__ */ a("div", { className: "flex items-center gap-2", children: [
                D && /* @__PURE__ */ t("span", { children: D }),
                /* @__PURE__ */ a("div", { children: [
                  /* @__PURE__ */ t("span", { className: "font-medium text-[var(--text-primary)]", children: e.name }),
                  e.description && /* @__PURE__ */ t("p", { className: "text-xs text-[var(--text-tertiary)] mt-0.5 max-w-sm truncate", children: e.description })
                ] })
              ] }) }),
              /* @__PURE__ */ t("td", { className: "px-4 py-3", children: /* @__PURE__ */ t("span", { className: `px-2 py-1 text-xs rounded-full ${s.bg} ${s.text}`, children: e.category || "Custom" }) }),
              /* @__PURE__ */ t("td", { className: "px-6 py-3 text-right", children: r ? /* @__PURE__ */ a("div", { className: "inline-flex items-center gap-2 justify-end", children: [
                /* @__PURE__ */ t("span", { className: "text-xs text-[var(--text-muted)]", children: n("admin:userRoles.alreadyAssigned", "Déjà assigné") }),
                r.removable ? /* @__PURE__ */ t(
                  "button",
                  {
                    type: "button",
                    onClick: () => B(e.id, "remove"),
                    disabled: h,
                    className: "p-1.5 rounded-[var(--radius-button)] text-[var(--error-text)] hover:bg-[var(--error-bg)] transition-colors disabled:opacity-50",
                    title: n("admin:userRoles.removeRole", "Retirer le rôle"),
                    children: h ? /* @__PURE__ */ t(_, { className: "w-4 h-4 animate-spin" }) : /* @__PURE__ */ t(se, { className: "w-4 h-4" })
                  }
                ) : /* @__PURE__ */ t("span", { title: r.lockedReason, children: /* @__PURE__ */ t(oe, { className: "w-4 h-4 text-[var(--text-muted)]" }) })
              ] }) : /* @__PURE__ */ a(
                "button",
                {
                  type: "button",
                  onClick: () => B(e.id, "assign"),
                  disabled: h,
                  className: "inline-flex items-center gap-1.5 px-3 py-1.5 rounded-[var(--radius-button)] bg-[var(--color-accent-600)] text-white text-xs font-medium hover:bg-[var(--color-accent-700)] transition-colors disabled:opacity-50",
                  children: [
                    h ? /* @__PURE__ */ t(_, { className: "w-3.5 h-3.5 animate-spin" }) : /* @__PURE__ */ t(le, { className: "w-3.5 h-3.5" }),
                    n("admin:userRoles.assign", "Assigner")
                  ]
                }
              ) })
            ]
          },
          e.id
        );
      }) })
    ] }) }),
    /* @__PURE__ */ t("div", { className: "px-6 py-3 border-t border-[var(--border-color)] bg-[var(--bg-tertiary)] flex justify-end", children: /* @__PURE__ */ t(
      "button",
      {
        type: "button",
        onClick: I,
        className: "px-4 py-2 rounded-[var(--radius-button)] bg-[var(--bg-secondary)] text-[var(--text-secondary)] hover:bg-[var(--bg-hover)] transition-colors text-sm font-medium",
        children: n("common:close", "Fermer")
      }
    ) })
  ] }) });
}
export {
  fe as R
};
//# sourceMappingURL=RoleAssignmentDialog-DNhW7ssZ.js.map
