import { jsxs as a, jsx as e, Fragment as Ve } from "react/jsx-runtime";
import { useMemo as ce, useState as o, useEffect as P, useCallback as Ge } from "react";
import { useNavigate as ze, useSearchParams as Ke } from "react-router-dom";
import { useTranslation as ge } from "react-i18next";
import { AlertTriangle as We, Plus as qe, Search as q, X as oe, Filter as de, RotateCcw as Je, ChevronDown as I, ArrowUpDown as Xe, Loader2 as $, Cloud as k, Mail as J, Shield as me, Eye as ue, Edit2 as he, UserX as xe, UserCheck as X, Trash2 as pe, Users as He } from "lucide-react";
import { h as fe, e as Qe, j as Ye, k as Ze, i as v, B as _e, S as F, V as et, P as tt, A as L, R, m as at, K as ve, n as be } from "./index-Dh9kUeDn.js";
function rt() {
  const { currentTenant: r, hasGlobalRoles: d, isGlobalView: n } = fe(), { currentModuleCode: y, getCurrentModule: N, isLoading: U, menu: O } = Qe();
  return d && !n && r && y && !U && O ? {
    isRestrictedModule: !N(),
    tenantName: r?.name || null
  } : { isRestrictedModule: !1, tenantName: null };
}
function st() {
  const { t: r } = ge("admin"), { isRestrictedModule: d, tenantName: n } = rt();
  return d ? /* @__PURE__ */ a(
    "div",
    {
      className: "flex items-center gap-3 px-4 py-2.5 border bg-amber-500/10 border-amber-500/20",
      style: { borderRadius: "var(--radius-card)" },
      children: [
        /* @__PURE__ */ e(
          "div",
          {
            className: "flex-shrink-0 w-8 h-8 flex items-center justify-center bg-amber-500/10",
            style: { borderRadius: "var(--radius-card)" },
            children: /* @__PURE__ */ e(We, { className: "w-4 h-4 text-amber-600" })
          }
        ),
        /* @__PURE__ */ a("div", { className: "flex-1 min-w-0", children: [
          /* @__PURE__ */ e("span", { className: "font-medium text-amber-700", children: r("scope.banner.restrictedModule", "Module restreint") }),
          /* @__PURE__ */ e("p", { className: "text-xs text-amber-600/70 mt-0.5", children: r(
            "scope.banner.restrictedModuleDesc",
            `Le tenant "{{tenantName}}" n'a pas accès à ce module. Données affichées grâce à vos privilèges administrateur.`,
            { tenantName: n }
          ) })
        ] })
      ]
    }
  ) : null;
}
function lt(r, d) {
  const [n, y] = o(r);
  return P(() => {
    const N = setTimeout(() => y(r), d);
    return () => clearTimeout(N);
  }, [r, d]), n;
}
const it = {
  search: "",
  status: "all",
  roleId: "",
  source: "all",
  userType: "",
  sortBy: "lastname",
  sortDesc: !1
};
function ht() {
  const { t: r } = ge("admin"), d = ze(), [n, y] = Ke(), N = Ye("users"), { currentTenant: U, isGlobalView: O } = fe(), { hasPermission: D } = Ze(), S = D("administration.users.create"), c = D("administration.users.update"), w = D("administration.users.delete"), ye = ce(() => ({
    search: n.get("search") || "",
    status: n.get("status") || "all",
    roleId: n.get("roleId") || "",
    source: n.get("source") || "all",
    userType: n.get("userType") || "",
    sortBy: n.get("sortBy") || "lastname",
    sortDesc: n.get("sortDesc") === "true"
  }), []), [u, Ne] = o([]), [H, we] = o([]), [b, Q] = o(!0), [l, E] = o(ye), [j, Y] = o(!1), [g, B] = o(parseInt(n.get("page") || "1")), [ke, Ce] = o(1), [V, Ae] = o(0), [Te, C] = o(!1), [h, Z] = o(null), [m, x] = o({
    email: "",
    password: "",
    firstName: "",
    lastName: "",
    roleIds: []
  }), [G, _] = o(!1), [ee, M] = o(null), [A, Ie] = o("table");
  P(() => {
    const t = {
      search: n.get("search") || "",
      status: n.get("status") || "all",
      roleId: n.get("roleId") || "",
      source: n.get("source") || "all",
      userType: n.get("userType") || "",
      sortBy: n.get("sortBy") || "lastname",
      sortDesc: n.get("sortDesc") === "true"
    };
    JSON.stringify(t) !== JSON.stringify(l) && E(t);
    const s = parseInt(n.get("page") || "1");
    s !== g && B(s);
  }, [n]);
  const te = lt(l.search, 300), T = ce(() => {
    let t = 0;
    return l.status !== "all" && t++, l.roleId && t++, l.source !== "all" && t++, l.userType && t++, t;
  }, [l]), f = Ge(async () => {
    try {
      Q(!0);
      const t = await v.users.getAll({
        page: g,
        pageSize: 20,
        search: te || void 0,
        isActive: l.status === "all" ? void 0 : l.status === "active",
        entraOnly: l.source === "entra" ? !0 : void 0,
        roleId: l.roleId || void 0,
        userType: l.userType || void 0,
        sortBy: l.sortBy || void 0,
        sortDesc: l.sortDesc
      });
      Ne(t.items), Ce(t.totalPages), Ae(t.totalCount);
    } catch (t) {
      console.error("Failed to load users:", t);
    } finally {
      Q(!1);
    }
  }, [g, te, l.status, l.source, l.roleId, l.userType, l.sortBy, l.sortDesc, U?.id, O]);
  P(() => {
    const t = new URLSearchParams();
    l.search && t.set("search", l.search), l.status !== "all" && t.set("status", l.status), l.roleId && t.set("roleId", l.roleId), l.source !== "all" && t.set("source", l.source), l.userType && t.set("userType", l.userType), l.sortBy !== "lastname" && t.set("sortBy", l.sortBy), l.sortDesc && t.set("sortDesc", "true"), g > 1 && t.set("page", g.toString()), y(t, { replace: !0 });
  }, [l, g, y]);
  const Pe = async () => {
    try {
      const t = await v.roles.getAll();
      we(t);
    } catch (t) {
      console.error("Failed to load roles:", t);
    }
  };
  P(() => {
    f();
  }, [f]), P(() => {
    Pe();
  }, []);
  const De = () => {
    E(it), B(1);
  }, p = (t, s) => {
    E((i) => ({ ...i, [t]: s })), B(1);
  }, Se = () => {
    Z(null), x({ email: "", password: "", firstName: "", lastName: "", roleIds: [] }), M(null), C(!0);
  }, ae = async (t) => {
    try {
      const s = await v.users.getById(t.id);
      Z(t), x({
        email: s.email,
        password: "",
        firstName: s.firstName,
        lastName: s.lastName,
        roleIds: s.roles.map((i) => i.id)
      }), M(null), C(!0);
    } catch (s) {
      console.error("Failed to load user details:", s);
    }
  }, je = async (t) => {
    t.preventDefault(), _(!0), M(null);
    try {
      if (h) {
        const s = {
          email: m.email !== h.email ? m.email : void 0,
          firstName: m.firstName,
          lastName: m.lastName,
          roleIds: m.roleIds
        };
        await v.users.update(h.id, s);
      } else
        await v.users.create(m);
      C(!1), f();
    } catch (s) {
      const i = s instanceof Error ? s.message : "An error occurred";
      M(i);
    } finally {
      _(!1);
    }
  }, z = async (t, s) => {
    s?.stopPropagation();
    try {
      t.isActive ? await v.users.deactivate(t.id) : await v.users.activate(t.id), f();
    } catch (i) {
      console.error("Failed to toggle user status:", i);
    }
  }, re = async (t, s) => {
    if (s?.stopPropagation(), !!confirm(r("users.confirmDelete", { name: t.fullName })))
      try {
        await v.users.delete(t.id), f();
      } catch (i) {
        console.error("Failed to delete user:", i);
      }
  }, Be = (t) => {
    x((s) => ({
      ...s,
      roleIds: s.roleIds?.includes(t) ? s.roleIds.filter((i) => i !== t) : [...s.roleIds || [], t]
    }));
  }, K = u.filter((t) => t.isActive), W = u.filter((t) => !t.isActive), Me = Math.floor(K.length * 0.3), se = (t, s) => t ? s ? "text-[var(--text-tertiary)] hover:text-[var(--warning-text)] hover:bg-[var(--warning-bg)]" : "text-[var(--text-tertiary)] hover:text-[var(--success-text)] hover:bg-[var(--success-bg)]" : "opacity-50 cursor-not-allowed text-[var(--text-tertiary)]", le = (t, s, i) => t ? i(s ? "users.deactivate" : "users.activate") : i("common.noPermission", "Permission insuffisante"), $e = (t) => t ? "text-[var(--text-tertiary)] hover:text-[var(--info-text)] hover:bg-[var(--info-bg)]" : "opacity-50 cursor-not-allowed text-[var(--text-tertiary)]", Fe = (t, s) => t ? s("common.edit") : s("common.noPermission", "Permission insuffisante"), Le = (t) => t ? "text-[var(--text-tertiary)] hover:text-[var(--error-text)] hover:bg-[var(--error-bg)]" : "opacity-50 cursor-not-allowed text-[var(--text-tertiary)]", Re = (t, s) => t ? s("common.delete") : s("common.noPermission", "Permission insuffisante"), Ue = (t) => {
    const s = {
      Direct: "bg-blue-100 dark:bg-blue-900/30 text-blue-700 dark:text-blue-300",
      Group: "bg-purple-100 dark:bg-purple-900/30 text-purple-700 dark:text-purple-300",
      Both: "bg-green-100 dark:bg-green-900/30 text-green-700 dark:text-green-300"
    }, i = {
      Direct: r("users.membershipDirect", "Direct"),
      Group: r("users.membershipGroup", "Via groupe"),
      Both: r("users.membershipBoth", "Mixte")
    };
    return { color: s[t] || "", label: i[t] || "" };
  }, Oe = () => /* @__PURE__ */ e("div", { className: "card overflow-hidden", children: /* @__PURE__ */ a("table", { className: "w-full", children: [
    /* @__PURE__ */ e("thead", { children: /* @__PURE__ */ a("tr", { className: "bg-[var(--bg-secondary)]", children: [
      /* @__PURE__ */ e("th", { className: "w-12 px-4 py-3", children: /* @__PURE__ */ e("input", { type: "checkbox", className: "w-4 h-4 rounded border-[var(--border-color)]" }) }),
      /* @__PURE__ */ e("th", { className: "text-left px-4 py-3 text-xs font-semibold text-[var(--text-tertiary)] uppercase tracking-wider", children: r("users.columns.name") }),
      /* @__PURE__ */ e("th", { className: "text-left px-4 py-3 text-xs font-semibold text-[var(--text-tertiary)] uppercase tracking-wider hidden md:table-cell", children: r("users.columns.email") }),
      /* @__PURE__ */ e("th", { className: "text-left px-4 py-3 text-xs font-semibold text-[var(--text-tertiary)] uppercase tracking-wider hidden lg:table-cell", children: r("users.columns.role") }),
      /* @__PURE__ */ e("th", { className: "text-left px-4 py-3 text-xs font-semibold text-[var(--text-tertiary)] uppercase tracking-wider", children: r("users.columns.status") }),
      /* @__PURE__ */ e("th", { className: "text-left px-4 py-3 text-xs font-semibold text-[var(--text-tertiary)] uppercase tracking-wider hidden xl:table-cell", children: r("users.columns.source", "Source") }),
      /* @__PURE__ */ e("th", { className: "text-left px-4 py-3 text-xs font-semibold text-[var(--text-tertiary)] uppercase tracking-wider hidden xl:table-cell", children: r("users.columns.group", "Groupe") }),
      N.map((t) => /* @__PURE__ */ e("th", { className: "text-left px-4 py-3 text-xs font-semibold text-[var(--text-tertiary)] uppercase tracking-wider hidden xl:table-cell", style: t.width ? { width: t.width } : void 0, children: t.label }, t.key)),
      /* @__PURE__ */ e("th", { className: "w-32 px-4 py-3" })
    ] }) }),
    /* @__PURE__ */ a("tbody", { className: "divide-y divide-[var(--border-color)]", children: [
      b && /* @__PURE__ */ e("tr", { children: /* @__PURE__ */ e("td", { colSpan: 8, className: "px-6 py-16 text-center", children: /* @__PURE__ */ a("div", { className: "flex flex-col items-center gap-3", children: [
        /* @__PURE__ */ e($, { className: "w-8 h-8 animate-spin text-primary-500" }),
        /* @__PURE__ */ e("span", { className: "text-sm text-[var(--text-secondary)]", children: "Chargement..." })
      ] }) }) }),
      !b && u.length === 0 && /* @__PURE__ */ e("tr", { children: /* @__PURE__ */ e("td", { colSpan: 8, className: "px-6 py-16 text-center", children: /* @__PURE__ */ a("div", { className: "flex flex-col items-center gap-3", children: [
        /* @__PURE__ */ e("div", { className: "w-16 h-16 rounded-full bg-[var(--bg-secondary)] flex items-center justify-center", children: /* @__PURE__ */ e(q, { className: "w-8 h-8 text-[var(--text-tertiary)]" }) }),
        /* @__PURE__ */ e("span", { className: "text-[var(--text-secondary)]", children: r("users.noUsers") })
      ] }) }) }),
      !b && u.length > 0 && u.map((t) => /* @__PURE__ */ a(
        "tr",
        {
          className: "group hover:bg-[var(--bg-hover)] transition-all cursor-pointer",
          onClick: () => d(`/administration/users/${t.id}`),
          children: [
            /* @__PURE__ */ e("td", { className: "px-4 py-3", onClick: (s) => s.stopPropagation(), children: /* @__PURE__ */ e("input", { type: "checkbox", className: "w-4 h-4 rounded border-[var(--border-color)]" }) }),
            /* @__PURE__ */ e("td", { className: "px-4 py-3", children: /* @__PURE__ */ a("div", { className: "flex items-center gap-3", children: [
              /* @__PURE__ */ a("div", { className: "relative", children: [
                /* @__PURE__ */ e(L, { name: t.fullName }),
                t.entraObjectId && /* @__PURE__ */ e("div", { className: "absolute -bottom-0.5 -right-0.5 w-4 h-4 rounded-full bg-blue-500 flex items-center justify-center ring-2 ring-[var(--bg-card)]", title: r("users.syncedWithAad"), children: /* @__PURE__ */ e(k, { className: "w-2.5 h-2.5 text-white" }) })
              ] }),
              /* @__PURE__ */ a("div", { className: "min-w-0", children: [
                /* @__PURE__ */ a("div", { className: "flex items-center gap-2", children: [
                  /* @__PURE__ */ e("p", { className: "font-medium text-[var(--text-primary)] group-hover:text-primary-600 transition-colors truncate", children: t.fullName }),
                  t.entraObjectId && /* @__PURE__ */ a("span", { className: "hidden sm:inline-flex items-center gap-1 px-1.5 py-0.5 rounded text-[10px] font-medium bg-blue-100 dark:bg-blue-900/30 text-blue-600 dark:text-blue-400", children: [
                    /* @__PURE__ */ e(k, { className: "w-2.5 h-2.5" }),
                    "AAD"
                  ] })
                ] }),
                /* @__PURE__ */ a("p", { className: "text-sm text-[var(--text-tertiary)] truncate md:hidden flex items-center gap-1", children: [
                  /* @__PURE__ */ e(J, { className: "w-3 h-3" }),
                  t.email
                ] })
              ] })
            ] }) }),
            /* @__PURE__ */ e("td", { className: "px-4 py-3 hidden md:table-cell", children: /* @__PURE__ */ a("div", { className: "flex items-center gap-2 text-[var(--text-secondary)]", children: [
              /* @__PURE__ */ e(J, { className: "w-4 h-4 text-[var(--text-tertiary)]" }),
              /* @__PURE__ */ e("span", { className: "truncate", children: t.email })
            ] }) }),
            /* @__PURE__ */ e("td", { className: "px-4 py-3 hidden lg:table-cell", children: /* @__PURE__ */ e("div", { className: "flex flex-wrap gap-1.5", children: t.roles.length > 0 ? t.roles.map((s, i) => /* @__PURE__ */ e(
              R,
              {
                role: s,
                variant: s.toLowerCase().includes("admin") ? "accent" : "default",
                icon: /* @__PURE__ */ e(me, { className: "w-3 h-3" })
              },
              `${s}-${i}`
            )) : /* @__PURE__ */ e("span", { className: "text-sm text-[var(--text-tertiary)] italic", children: "Aucun rôle" }) }) }),
            /* @__PURE__ */ e("td", { className: "px-4 py-3", children: /* @__PURE__ */ e(
              at,
              {
                status: t.isActive ? "active" : "inactive",
                label: t.isActive ? r("users.status.active") : r("users.status.inactive")
              }
            ) }),
            /* @__PURE__ */ e("td", { className: "px-4 py-3 hidden xl:table-cell", children: (() => {
              const { color: s, label: i } = Ue(t.membershipSource);
              return /* @__PURE__ */ e("span", { className: `inline-flex items-center gap-1 px-2 py-1 rounded-md text-xs font-medium ${s}`, children: i });
            })() }),
            /* @__PURE__ */ e("td", { className: "px-4 py-3 hidden xl:table-cell", children: t.primaryGroupId ? /* @__PURE__ */ a(
              "button",
              {
                onClick: (s) => {
                  s.stopPropagation(), d(`/administration/groups/${t.primaryGroupId}`);
                },
                className: "inline-flex items-center gap-1.5 px-2 py-1 rounded-md text-xs font-medium bg-[var(--bg-secondary)] hover:bg-[var(--bg-hover)] text-[var(--text-secondary)] hover:text-primary-600 transition-colors",
                children: [
                  /* @__PURE__ */ e(He, { className: "w-3 h-3" }),
                  t.primaryGroupName
                ]
              }
            ) : /* @__PURE__ */ e("span", { className: "text-[var(--text-tertiary)]", children: "—" }) }),
            N.map((s) => /* @__PURE__ */ e("td", { className: "px-4 py-3 hidden xl:table-cell text-[var(--text-secondary)]", children: s.render(t, { tableId: "users", rowIndex: u.indexOf(t) }) }, s.key)),
            /* @__PURE__ */ e("td", { className: "px-4 py-3", children: /* @__PURE__ */ a("div", { className: "flex items-center justify-end gap-1", onClick: (s) => s.stopPropagation(), children: [
              /* @__PURE__ */ e(
                "button",
                {
                  onClick: () => d(`/administration/users/${t.id}`),
                  className: "p-2 rounded-lg text-[var(--text-tertiary)] hover:text-primary-600 hover:bg-[var(--accent-bg)] transition-all",
                  title: r("users.view"),
                  children: /* @__PURE__ */ e(ue, { className: "w-4 h-4" })
                }
              ),
              /* @__PURE__ */ e(
                "button",
                {
                  onClick: (s) => {
                    s.stopPropagation(), ae(t);
                  },
                  disabled: !c,
                  className: `p-2 rounded-lg transition-all ${c ? "text-[var(--text-tertiary)] hover:text-[var(--info-text)] hover:bg-[var(--info-bg)]" : "opacity-50 cursor-not-allowed text-[var(--text-tertiary)]"}`,
                  title: c ? r("common.edit") : r("common.noPermission", "Permission insuffisante"),
                  children: /* @__PURE__ */ e(he, { className: "w-4 h-4" })
                }
              ),
              /* @__PURE__ */ e(
                "button",
                {
                  onClick: (s) => z(t, s),
                  disabled: !c,
                  className: `p-2 rounded-lg transition-all ${se(c, t.isActive)}`,
                  title: le(c, t.isActive, r),
                  children: t.isActive ? /* @__PURE__ */ e(xe, { className: "w-4 h-4" }) : /* @__PURE__ */ e(X, { className: "w-4 h-4" })
                }
              ),
              t.userType !== "System" && t.userType !== "LocalAdmin" && /* @__PURE__ */ e(
                "button",
                {
                  onClick: (s) => re(t, s),
                  disabled: !w,
                  className: `p-2 rounded-lg transition-all ${w ? "text-[var(--text-tertiary)] hover:text-[var(--error-text)] hover:bg-[var(--error-bg)]" : "opacity-50 cursor-not-allowed text-[var(--text-tertiary)]"}`,
                  title: w ? r("common.delete") : r("common.noPermission", "Permission insuffisante"),
                  children: /* @__PURE__ */ e(pe, { className: "w-4 h-4" })
                }
              ),
              /* @__PURE__ */ e(F, { name: "users.table.row.actions", context: { user: t, reload: f } })
            ] }) })
          ]
        },
        t.id
      ))
    ] })
  ] }) }), ie = () => /* @__PURE__ */ a("div", { className: "grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 gap-4", children: [
    b && /* @__PURE__ */ a("div", { className: "col-span-full flex flex-col items-center justify-center py-16", children: [
      /* @__PURE__ */ e($, { className: "w-8 h-8 animate-spin text-primary-500" }),
      /* @__PURE__ */ e("span", { className: "mt-3 text-sm text-[var(--text-secondary)]", children: "Chargement..." })
    ] }),
    !b && u.length === 0 && /* @__PURE__ */ a("div", { className: "col-span-full flex flex-col items-center justify-center py-16", children: [
      /* @__PURE__ */ e("div", { className: "w-16 h-16 rounded-lg bg-[var(--bg-secondary)] flex items-center justify-center mb-3", children: /* @__PURE__ */ e(q, { className: "w-8 h-8 text-[var(--text-tertiary)]" }) }),
      /* @__PURE__ */ e("span", { className: "text-[var(--text-secondary)]", children: r("users.noUsers") })
    ] }),
    !b && u.length > 0 && u.map((t) => /* @__PURE__ */ a(
      "button",
      {
        type: "button",
        onClick: () => d(`/administration/users/${t.id}`),
        className: "bg-[var(--bg-card)] border-2 border-[var(--color-accent-200)] dark:border-[var(--color-accent-800)] rounded-[var(--radius-card)] overflow-hidden transition-all hover:shadow-lg hover:border-[var(--color-accent-400)] cursor-pointer flex flex-col text-left",
        children: [
          /* @__PURE__ */ e("div", { className: "bg-[var(--color-accent-50)] dark:bg-[var(--color-accent-900)]/20 p-4 border-b border-[var(--color-accent-200)] dark:border-[var(--color-accent-800)]", children: /* @__PURE__ */ a("div", { className: "flex items-center justify-between", children: [
            /* @__PURE__ */ a("div", { className: "flex items-center gap-3", children: [
              /* @__PURE__ */ a("div", { className: "relative", children: [
                /* @__PURE__ */ e(L, { name: t.fullName, size: "lg", className: "rounded-lg" }),
                t.entraObjectId && /* @__PURE__ */ e("div", { className: "absolute -bottom-0.5 -right-0.5 w-5 h-5 rounded-full bg-blue-500 flex items-center justify-center ring-2 ring-[var(--bg-card)]", title: r("users.syncedWithAad"), children: /* @__PURE__ */ e(k, { className: "w-3 h-3 text-white" }) })
              ] }),
              /* @__PURE__ */ a("div", { children: [
                /* @__PURE__ */ e("h3", { className: "font-semibold text-[var(--text-primary)] truncate", children: t.fullName }),
                /* @__PURE__ */ a("p", { className: "text-xs text-[var(--text-tertiary)] truncate flex items-center gap-1", children: [
                  /* @__PURE__ */ e(J, { className: "w-3 h-3" }),
                  t.email
                ] })
              ] })
            ] }),
            /* @__PURE__ */ a("div", { className: "flex flex-col items-end gap-1", children: [
              /* @__PURE__ */ e("span", { className: `inline-flex items-center gap-1.5 px-2 py-0.5 text-xs rounded-full font-medium ${t.isActive ? "bg-green-100 dark:bg-green-900/30 text-green-700 dark:text-green-400" : "bg-gray-100 dark:bg-gray-800 text-gray-600 dark:text-gray-400"}`, children: t.isActive ? r("users.status.active") : r("users.status.inactive") }),
              t.entraObjectId && /* @__PURE__ */ a("span", { className: "inline-flex items-center gap-1 px-1.5 py-0.5 rounded text-[10px] font-medium bg-blue-100 dark:bg-blue-900/30 text-blue-600 dark:text-blue-400", children: [
                /* @__PURE__ */ e(k, { className: "w-2.5 h-2.5" }),
                "AAD"
              ] })
            ] })
          ] }) }),
          /* @__PURE__ */ a("div", { className: "p-4 flex flex-col flex-1", children: [
            /* @__PURE__ */ a("div", { className: "flex flex-wrap gap-1 mb-3", children: [
              t.roles.length > 0 ? t.roles.slice(0, 2).map((s) => /* @__PURE__ */ e(
                R,
                {
                  role: s,
                  variant: s.toLowerCase().includes("admin") ? "accent" : "default",
                  icon: /* @__PURE__ */ e(me, { className: "w-3 h-3" })
                },
                s
              )) : /* @__PURE__ */ e("span", { className: "text-xs text-[var(--text-muted)]", children: "Aucun rôle" }),
              t.roles.length > 2 && /* @__PURE__ */ a("span", { className: "text-xs px-2 py-0.5 rounded-md bg-[var(--bg-secondary)] text-[var(--text-muted)]", children: [
                "+",
                t.roles.length - 2
              ] })
            ] }),
            /* @__PURE__ */ e(F, { name: "users.card.content", context: { user: t } }),
            /* @__PURE__ */ e("div", { className: "flex-1" }),
            /* @__PURE__ */ a("div", { className: "flex items-center justify-end gap-1 pt-3 border-t border-[var(--border-color)]", onClick: (s) => s.stopPropagation(), children: [
              /* @__PURE__ */ e(
                "button",
                {
                  onClick: () => d(`/administration/users/${t.id}`),
                  className: "p-2 rounded-lg text-[var(--text-tertiary)] hover:text-primary-600 hover:bg-[var(--accent-bg)] transition-all",
                  title: r("users.view"),
                  children: /* @__PURE__ */ e(ue, { className: "w-4 h-4" })
                }
              ),
              /* @__PURE__ */ e(
                "button",
                {
                  onClick: (s) => {
                    s.stopPropagation(), ae(t);
                  },
                  disabled: !c,
                  className: `p-2 rounded-lg transition-all ${$e(c)}`,
                  title: Fe(c, r),
                  children: /* @__PURE__ */ e(he, { className: "w-4 h-4" })
                }
              ),
              /* @__PURE__ */ e(
                "button",
                {
                  onClick: (s) => z(t, s),
                  disabled: !c,
                  className: `p-2 rounded-lg transition-all ${se(c, t.isActive)}`,
                  title: le(c, t.isActive, r),
                  children: t.isActive ? /* @__PURE__ */ e(xe, { className: "w-4 h-4" }) : /* @__PURE__ */ e(X, { className: "w-4 h-4" })
                }
              ),
              t.userType !== "System" && t.userType !== "LocalAdmin" && /* @__PURE__ */ e(
                "button",
                {
                  onClick: (s) => re(t, s),
                  disabled: !w,
                  className: `p-2 rounded-lg transition-all ${Le(w)}`,
                  title: Re(w, r),
                  children: /* @__PURE__ */ e(pe, { className: "w-4 h-4" })
                }
              )
            ] })
          ] })
        ]
      },
      t.id
    ))
  ] }), Ee = () => /* @__PURE__ */ e("div", { className: "flex gap-6 overflow-x-auto pb-4", children: b ? /* @__PURE__ */ a("div", { className: "flex-1 flex flex-col items-center justify-center py-16", children: [
    /* @__PURE__ */ e($, { className: "w-8 h-8 animate-spin text-primary-500" }),
    /* @__PURE__ */ e("span", { className: "mt-3 text-sm text-[var(--text-secondary)]", children: "Chargement..." })
  ] }) : /* @__PURE__ */ a(Ve, { children: [
    /* @__PURE__ */ e(ve, { title: "Actifs", count: K.length, color: "success", children: K.map((t) => /* @__PURE__ */ a(be, { onClick: () => d(`/administration/users/${t.id}`), children: [
      /* @__PURE__ */ a("div", { className: "flex items-center gap-3 mb-3", children: [
        /* @__PURE__ */ a("div", { className: "relative", children: [
          /* @__PURE__ */ e(L, { name: t.fullName, size: "sm" }),
          t.entraObjectId && /* @__PURE__ */ e("div", { className: "absolute -bottom-0.5 -right-0.5 w-3.5 h-3.5 rounded-full bg-blue-500 flex items-center justify-center ring-2 ring-[var(--bg-card)]", title: r("users.syncedWithAad"), children: /* @__PURE__ */ e(k, { className: "w-2 h-2 text-white" }) })
        ] }),
        /* @__PURE__ */ a("div", { className: "flex-1 min-w-0", children: [
          /* @__PURE__ */ e("p", { className: "font-medium text-sm text-[var(--text-primary)] truncate", children: t.fullName }),
          /* @__PURE__ */ e("p", { className: "text-xs text-[var(--text-tertiary)] truncate", children: t.email })
        ] })
      ] }),
      /* @__PURE__ */ a("div", { className: "flex items-center justify-between", children: [
        t.roles.length > 0 ? /* @__PURE__ */ e(
          R,
          {
            role: t.roles[0],
            variant: t.roles[0].toLowerCase().includes("admin") ? "accent" : "default"
          }
        ) : /* @__PURE__ */ e("span", { className: "text-xs text-[var(--text-muted)]", children: "-" }),
        /* @__PURE__ */ e("span", { className: "text-xs text-[var(--text-muted)]", children: "En ligne" })
      ] })
    ] }, t.id)) }),
    /* @__PURE__ */ a(ve, { title: "Inactifs", count: W.length, color: "error", children: [
      W.map((t) => /* @__PURE__ */ a(be, { onClick: () => d(`/administration/users/${t.id}`), children: [
        /* @__PURE__ */ a("div", { className: "flex items-center gap-3 mb-3", children: [
          /* @__PURE__ */ a("div", { className: "relative", children: [
            /* @__PURE__ */ e(L, { name: t.fullName, size: "sm" }),
            t.entraObjectId && /* @__PURE__ */ e("div", { className: "absolute -bottom-0.5 -right-0.5 w-3.5 h-3.5 rounded-full bg-blue-500 flex items-center justify-center ring-2 ring-[var(--bg-card)]", title: r("users.syncedWithAad"), children: /* @__PURE__ */ e(k, { className: "w-2 h-2 text-white" }) })
          ] }),
          /* @__PURE__ */ a("div", { className: "flex-1 min-w-0", children: [
            /* @__PURE__ */ e("p", { className: "font-medium text-sm text-[var(--text-primary)] truncate", children: t.fullName }),
            /* @__PURE__ */ e("p", { className: "text-xs text-[var(--text-tertiary)] truncate", children: t.email })
          ] })
        ] }),
        /* @__PURE__ */ a("div", { className: "flex items-center justify-between", children: [
          t.roles.length > 0 ? /* @__PURE__ */ e(R, { role: t.roles[0] }) : /* @__PURE__ */ e("span", { className: "text-xs text-[var(--text-muted)]", children: "-" }),
          /* @__PURE__ */ e(
            "button",
            {
              onClick: (s) => z(t, s),
              disabled: !c,
              className: `text-xs px-2 py-1 rounded transition-opacity ${c ? "bg-[var(--success-bg)] text-[var(--success-text)] hover:opacity-80" : "opacity-50 cursor-not-allowed bg-[var(--bg-secondary)] text-[var(--text-tertiary)]"}`,
              title: c ? void 0 : r("common.noPermission", "Permission insuffisante"),
              children: "Réactiver"
            }
          )
        ] })
      ] }, t.id)),
      W.length === 0 && /* @__PURE__ */ a("div", { className: "border border-dashed border-[var(--border-color)] rounded-lg p-6 text-center", children: [
        /* @__PURE__ */ e(X, { className: "w-6 h-6 mx-auto mb-2 text-[var(--text-muted)]" }),
        /* @__PURE__ */ e("p", { className: "text-xs text-[var(--text-muted)]", children: "Aucun utilisateur inactif" })
      ] })
    ] })
  ] }) });
  return /* @__PURE__ */ a("div", { className: "space-y-6", children: [
    /* @__PURE__ */ e(
      _e,
      {
        items: [
          { label: r("header.title"), href: "/administration" },
          { label: r("users.title") }
        ]
      }
    ),
    /* @__PURE__ */ a("div", { className: "flex flex-col sm:flex-row sm:items-center justify-between gap-4", children: [
      /* @__PURE__ */ a("div", { children: [
        /* @__PURE__ */ e("h1", { className: "text-2xl font-bold text-[var(--text-primary)]", children: r("users.title") }),
        /* @__PURE__ */ a("p", { className: "text-sm text-[var(--text-tertiary)] mt-1", children: [
          V,
          " utilisateurs • ",
          Me,
          " en ligne"
        ] })
      ] }),
      /* @__PURE__ */ a("div", { className: "flex items-center gap-2", children: [
        /* @__PURE__ */ e(F, { name: "users.header.actions", context: { users: u, totalCount: V, reload: f } }),
        /* @__PURE__ */ a(
          "button",
          {
            onClick: Se,
            disabled: !S,
            className: `btn btn-primary ${S ? "" : "opacity-50 cursor-not-allowed"}`,
            title: S ? void 0 : r("common.noPermission", "Permission insuffisante"),
            children: [
              /* @__PURE__ */ e(qe, { className: "w-4 h-4" }),
              r("users.create")
            ]
          }
        )
      ] })
    ] }),
    /* @__PURE__ */ e(st, {}),
    /* @__PURE__ */ e("div", { className: "card p-4", children: /* @__PURE__ */ a("div", { className: "flex flex-col gap-4", children: [
      /* @__PURE__ */ a("div", { className: "flex flex-col md:flex-row md:items-center justify-between gap-4", children: [
        /* @__PURE__ */ a("div", { className: "flex items-center gap-3 flex-1", children: [
          /* @__PURE__ */ a("div", { className: "relative flex-1 max-w-md", children: [
            !l.search && /* @__PURE__ */ e(q, { className: "absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-[var(--text-muted)] pointer-events-none" }),
            /* @__PURE__ */ e(
              "input",
              {
                type: "text",
                value: l.search,
                onChange: (t) => p("search", t.target.value),
                placeholder: r("users.filters.searchPlaceholder"),
                className: "input w-full",
                style: l.search ? void 0 : { paddingLeft: "2.5rem" }
              }
            ),
            l.search && /* @__PURE__ */ e(
              "button",
              {
                onClick: () => p("search", ""),
                className: "absolute right-3 top-1/2 -translate-y-1/2 p-1 rounded hover:bg-[var(--bg-secondary)]",
                children: /* @__PURE__ */ e(oe, { className: "w-3 h-3 text-[var(--text-tertiary)]" })
              }
            )
          ] }),
          /* @__PURE__ */ a(
            "button",
            {
              onClick: () => Y(!j),
              className: `btn ${j ? "btn-primary" : "btn-secondary"} relative`,
              children: [
                /* @__PURE__ */ e(de, { className: "w-4 h-4" }),
                /* @__PURE__ */ e("span", { className: "hidden sm:inline", children: r("users.filters.title") }),
                T > 0 && /* @__PURE__ */ e("span", { className: "absolute -top-1.5 -right-1.5 w-5 h-5 rounded-full bg-[var(--color-accent-500)] text-white text-xs flex items-center justify-center", children: T })
              ]
            }
          ),
          T > 0 && /* @__PURE__ */ e(
            "button",
            {
              onClick: De,
              className: "btn btn-ghost text-[var(--text-tertiary)] hover:text-[var(--text-primary)]",
              title: r("users.filters.reset"),
              children: /* @__PURE__ */ e(Je, { className: "w-4 h-4" })
            }
          )
        ] }),
        /* @__PURE__ */ e("div", { className: "hidden md:block", children: /* @__PURE__ */ e(
          et,
          {
            viewMode: A,
            onChange: Ie,
            tableLabel: "Table",
            cardsLabel: "Cards",
            kanbanLabel: "Kanban"
          }
        ) })
      ] }),
      j && /* @__PURE__ */ a("div", { className: "grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-5 gap-4 pt-4 border-t border-[var(--border-color)]", children: [
        /* @__PURE__ */ a("div", { children: [
          /* @__PURE__ */ e("label", { className: "block text-xs font-medium text-[var(--text-tertiary)] mb-1.5", children: r("users.filters.status") }),
          /* @__PURE__ */ a("div", { className: "relative", children: [
            /* @__PURE__ */ a(
              "select",
              {
                value: l.status,
                onChange: (t) => p("status", t.target.value),
                className: "input w-full appearance-none pr-8",
                children: [
                  /* @__PURE__ */ e("option", { value: "all", children: r("users.filters.allStatuses") }),
                  /* @__PURE__ */ e("option", { value: "active", children: r("users.filters.activeOnly") }),
                  /* @__PURE__ */ e("option", { value: "inactive", children: r("users.filters.inactiveOnly") })
                ]
              }
            ),
            /* @__PURE__ */ e(I, { className: "absolute right-2.5 top-1/2 -translate-y-1/2 w-4 h-4 text-[var(--text-tertiary)] pointer-events-none" })
          ] })
        ] }),
        /* @__PURE__ */ a("div", { children: [
          /* @__PURE__ */ e("label", { className: "block text-xs font-medium text-[var(--text-tertiary)] mb-1.5", children: r("users.filters.role") }),
          /* @__PURE__ */ a("div", { className: "relative", children: [
            /* @__PURE__ */ a(
              "select",
              {
                value: l.roleId,
                onChange: (t) => p("roleId", t.target.value),
                className: "input w-full appearance-none pr-8",
                children: [
                  /* @__PURE__ */ e("option", { value: "", children: r("users.filters.allRoles") }),
                  H.map((t) => /* @__PURE__ */ e("option", { value: t.id, children: t.name }, t.id))
                ]
              }
            ),
            /* @__PURE__ */ e(I, { className: "absolute right-2.5 top-1/2 -translate-y-1/2 w-4 h-4 text-[var(--text-tertiary)] pointer-events-none" })
          ] })
        ] }),
        /* @__PURE__ */ a("div", { children: [
          /* @__PURE__ */ e("label", { className: "block text-xs font-medium text-[var(--text-tertiary)] mb-1.5", children: r("users.filters.source") }),
          /* @__PURE__ */ a("div", { className: "relative", children: [
            /* @__PURE__ */ a(
              "select",
              {
                value: l.source,
                onChange: (t) => p("source", t.target.value),
                className: "input w-full appearance-none pr-8",
                children: [
                  /* @__PURE__ */ e("option", { value: "all", children: r("users.filters.allSources") }),
                  /* @__PURE__ */ e("option", { value: "entra", children: r("users.filters.entraOnly") }),
                  /* @__PURE__ */ e("option", { value: "local", children: r("users.filters.localOnly") })
                ]
              }
            ),
            /* @__PURE__ */ e(I, { className: "absolute right-2.5 top-1/2 -translate-y-1/2 w-4 h-4 text-[var(--text-tertiary)] pointer-events-none" })
          ] })
        ] }),
        /* @__PURE__ */ a("div", { children: [
          /* @__PURE__ */ e("label", { className: "block text-xs font-medium text-[var(--text-tertiary)] mb-1.5", children: r("users.filters.userType") }),
          /* @__PURE__ */ a("div", { className: "relative", children: [
            /* @__PURE__ */ a(
              "select",
              {
                value: l.userType,
                onChange: (t) => p("userType", t.target.value),
                className: "input w-full appearance-none pr-8",
                children: [
                  /* @__PURE__ */ e("option", { value: "", children: r("users.filters.allTypes") }),
                  /* @__PURE__ */ e("option", { value: "User", children: r("users.filters.typeUser") }),
                  /* @__PURE__ */ e("option", { value: "System", children: r("users.filters.typeSystem") }),
                  /* @__PURE__ */ e("option", { value: "LocalAdmin", children: r("users.filters.typeLocalAdmin") })
                ]
              }
            ),
            /* @__PURE__ */ e(I, { className: "absolute right-2.5 top-1/2 -translate-y-1/2 w-4 h-4 text-[var(--text-tertiary)] pointer-events-none" })
          ] })
        ] }),
        /* @__PURE__ */ a("div", { children: [
          /* @__PURE__ */ e("label", { className: "block text-xs font-medium text-[var(--text-tertiary)] mb-1.5", children: r("users.filters.sortBy") }),
          /* @__PURE__ */ a("div", { className: "flex gap-2", children: [
            /* @__PURE__ */ a("div", { className: "relative flex-1", children: [
              /* @__PURE__ */ a(
                "select",
                {
                  value: l.sortBy,
                  onChange: (t) => p("sortBy", t.target.value),
                  className: "input w-full appearance-none pr-8",
                  children: [
                    /* @__PURE__ */ e("option", { value: "lastname", children: r("users.filters.sortName") }),
                    /* @__PURE__ */ e("option", { value: "email", children: r("users.filters.sortEmail") }),
                    /* @__PURE__ */ e("option", { value: "lastlogin", children: r("users.filters.sortLastLogin") }),
                    /* @__PURE__ */ e("option", { value: "createdat", children: r("users.filters.sortCreatedAt") }),
                    /* @__PURE__ */ e("option", { value: "status", children: r("users.filters.sortStatus") })
                  ]
                }
              ),
              /* @__PURE__ */ e(I, { className: "absolute right-2.5 top-1/2 -translate-y-1/2 w-4 h-4 text-[var(--text-tertiary)] pointer-events-none" })
            ] }),
            /* @__PURE__ */ e(
              "button",
              {
                onClick: () => p("sortDesc", !l.sortDesc),
                className: `btn ${l.sortDesc ? "btn-primary" : "btn-secondary"} px-2.5`,
                title: l.sortDesc ? r("users.filters.sortDesc") : r("users.filters.sortAsc"),
                children: /* @__PURE__ */ e(Xe, { className: "w-4 h-4" })
              }
            )
          ] })
        ] })
      ] }),
      T > 0 && !j && /* @__PURE__ */ a("div", { className: "flex items-center gap-2 text-sm text-[var(--text-tertiary)]", children: [
        /* @__PURE__ */ e(de, { className: "w-4 h-4" }),
        /* @__PURE__ */ e("span", { children: r("users.filters.activeFilters", { count: T }) }),
        /* @__PURE__ */ e(
          "button",
          {
            onClick: () => Y(!0),
            className: "text-[var(--color-accent-600)] hover:underline",
            children: r("users.filters.title")
          }
        )
      ] })
    ] }) }),
    /* @__PURE__ */ e("div", { className: "md:hidden", children: ie() }),
    /* @__PURE__ */ a("div", { className: "hidden md:block", children: [
      A === "table" && Oe(),
      A === "cards" && ie(),
      A === "kanban" && Ee()
    ] }),
    A !== "kanban" && /* @__PURE__ */ e(
      tt,
      {
        page: g,
        totalPages: ke,
        totalCount: V,
        pageSize: 20,
        onPageChange: B,
        itemLabel: "utilisateurs"
      }
    ),
    Te && /* @__PURE__ */ e("div", { className: "fixed inset-0 bg-black/50 backdrop-blur-sm flex items-center justify-center z-50", children: /* @__PURE__ */ a("div", { className: "card 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__ */ e("h2", { className: "text-xl font-bold text-[var(--text-primary)]", children: r(h ? "users.edit" : "users.create") }),
        /* @__PURE__ */ e("button", { onClick: () => C(!1), className: "btn btn-ghost p-2", children: /* @__PURE__ */ e(oe, { className: "w-5 h-5" }) })
      ] }),
      ee && /* @__PURE__ */ e("div", { className: "mb-4 p-3 rounded-lg bg-[var(--error-bg)] text-[var(--error-text)] text-sm border border-[var(--error-border)]", children: ee }),
      /* @__PURE__ */ a("form", { onSubmit: je, className: "space-y-4", children: [
        /* @__PURE__ */ a("div", { className: "grid grid-cols-2 gap-4", children: [
          /* @__PURE__ */ a("div", { children: [
            /* @__PURE__ */ e("label", { className: "block text-sm font-medium text-[var(--text-secondary)] mb-1", children: r("users.form.firstName") }),
            /* @__PURE__ */ e(
              "input",
              {
                type: "text",
                required: !0,
                value: m.firstName,
                onChange: (t) => x((s) => ({ ...s, firstName: t.target.value })),
                className: "input w-full"
              }
            )
          ] }),
          /* @__PURE__ */ a("div", { children: [
            /* @__PURE__ */ e("label", { className: "block text-sm font-medium text-[var(--text-secondary)] mb-1", children: r("users.form.lastName") }),
            /* @__PURE__ */ e(
              "input",
              {
                type: "text",
                required: !0,
                value: m.lastName,
                onChange: (t) => x((s) => ({ ...s, lastName: t.target.value })),
                className: "input w-full"
              }
            )
          ] })
        ] }),
        /* @__PURE__ */ a("div", { children: [
          /* @__PURE__ */ e("label", { className: "block text-sm font-medium text-[var(--text-secondary)] mb-1", children: r("users.columns.email") }),
          /* @__PURE__ */ e(
            "input",
            {
              type: "email",
              required: !0,
              value: m.email,
              onChange: (t) => x((s) => ({ ...s, email: t.target.value })),
              className: "input w-full"
            }
          )
        ] }),
        !h && /* @__PURE__ */ a("div", { children: [
          /* @__PURE__ */ e("label", { className: "block text-sm font-medium text-[var(--text-secondary)] mb-1", children: r("users.form.password") }),
          /* @__PURE__ */ e(
            "input",
            {
              type: "password",
              required: !h,
              value: m.password,
              onChange: (t) => x((s) => ({ ...s, password: t.target.value })),
              autoComplete: "new-password",
              className: "input w-full"
            }
          )
        ] }),
        /* @__PURE__ */ a("div", { children: [
          /* @__PURE__ */ e("label", { className: "block text-sm font-medium text-[var(--text-secondary)] mb-2", children: r("users.form.roles") }),
          /* @__PURE__ */ e("div", { className: "space-y-2 max-h-48 overflow-y-auto p-3 border border-[var(--border-color)] rounded-lg bg-[var(--bg-secondary)]", children: H.map((t) => /* @__PURE__ */ a("label", { className: "flex items-center gap-3 cursor-pointer p-2 rounded-lg hover:bg-[var(--bg-hover)] transition-colors", children: [
            /* @__PURE__ */ e(
              "input",
              {
                type: "checkbox",
                checked: m.roleIds?.includes(t.id) || !1,
                onChange: () => Be(t.id),
                className: "w-4 h-4 rounded border-[var(--border-color)] text-primary-600 focus:ring-primary-500"
              }
            ),
            /* @__PURE__ */ a("div", { children: [
              /* @__PURE__ */ e("span", { className: "text-[var(--text-primary)] font-medium", children: t.name }),
              t.description && /* @__PURE__ */ e("p", { className: "text-xs text-[var(--text-tertiary)]", children: t.description })
            ] })
          ] }, t.id)) })
        ] }),
        /* @__PURE__ */ e(
          F,
          {
            name: "users.form.fields.after",
            context: {
              formData: m,
              onChange: (t, s) => {
                if (t.startsWith("ext.")) {
                  const i = t.substring(4);
                  x((ne) => ({
                    ...ne,
                    ext: { ...ne.ext || {}, [i]: s }
                  }));
                } else
                  x((i) => ({ ...i, [t]: s }));
              },
              disabled: G,
              isEditing: !!h,
              user: h
            }
          }
        ),
        /* @__PURE__ */ a("div", { className: "flex justify-end gap-2 pt-4", children: [
          /* @__PURE__ */ e(
            "button",
            {
              type: "button",
              onClick: () => C(!1),
              className: "btn btn-secondary",
              children: r("common.cancel")
            }
          ),
          /* @__PURE__ */ a(
            "button",
            {
              type: "submit",
              disabled: G,
              className: "btn btn-primary disabled:opacity-50",
              children: [
                G && /* @__PURE__ */ e($, { className: "w-4 h-4 animate-spin" }),
                r(h ? "common.save" : "common.create")
              ]
            }
          )
        ] })
      ] })
    ] }) })
  ] });
}
export {
  ht as UsersPage
};
//# sourceMappingURL=UsersPage-BsWti8ZO.js.map
