import { useState as u, useCallback as U, useEffect as g, useRef as B, useMemo as I } from "react";
import { i as D } from "./index-Dh9kUeDn.js";
import { jsxs as r, jsx as t, Fragment as $ } from "react/jsx-runtime";
import { useTranslation as P } from "react-i18next";
import { Globe as S, Building2 as E, ChevronDown as Q, Search as q, Check as L, AlertTriangle as z, User as H } from "lucide-react";
function Y() {
  const [h, x] = u([]), [c, m] = u(!0), [b, f] = u(null), s = U(async () => {
    m(!0), f(null);
    try {
      const l = await D.myTenants.getAll();
      x(l);
    } catch (l) {
      console.error("Failed to load admin tenants:", l), f("Failed to load tenants");
    } finally {
      m(!1);
    }
  }, []);
  return g(() => {
    s();
  }, [s]), { tenants: h, loading: c, error: b, refresh: s };
}
function Z({
  value: h,
  onChange: x,
  adminTenants: c,
  userTenantMemberships: m = [],
  disabled: b = !1,
  showNotMemberWarning: f = !1
}) {
  const { t: s } = P("admin"), [l, p] = u(!1), [v, y] = u(""), [n, N] = u("all"), w = B(null), R = B(null);
  g(() => {
    function e(a) {
      w.current && !w.current.contains(a.target) && p(!1);
    }
    return document.addEventListener("mousedown", e), () => document.removeEventListener("mousedown", e);
  }, []), g(() => {
    function e(a) {
      a.key === "Escape" && p(!1);
    }
    return document.addEventListener("keydown", e), () => document.removeEventListener("keydown", e);
  }, []), g(() => {
    l && R.current && setTimeout(() => R.current?.focus(), 50);
  }, [l]);
  const T = I(() => {
    const e = /* @__PURE__ */ new Map();
    return m.forEach((a) => e.set(a.tenantId, a)), e;
  }, [m]), { b2bTenants: A, b2cTenants: G, filteredTenants: k } = I(() => {
    const e = c.filter((d) => d.tenantType === "Business"), a = c.filter((d) => d.tenantType === "Personal");
    let o = c;
    if (n === "b2b" ? o = e : n === "b2c" && (o = a), v.trim()) {
      const d = v.toLowerCase().trim();
      o = o.filter((O) => O.tenantName.toLowerCase().includes(d));
    }
    return { b2bTenants: e, b2cTenants: a, filteredTenants: o };
  }, [c, n, v]), i = h === "global", C = i ? null : h, M = () => {
    x("global"), p(!1), y("");
  }, j = (e) => {
    const a = T.get(e.tenantId);
    x({
      tenantId: e.tenantId,
      tenantSlug: e.tenantSlug,
      tenantUserId: a?.tenantUserId || "",
      tenantName: e.tenantName
    }), p(!1), y("");
  };
  if (c.length === 0)
    return null;
  const F = (e) => {
    const a = !i && C?.tenantId === e.tenantId, o = e.tenantType === "Business", d = T.has(e.tenantId);
    return /* @__PURE__ */ r(
      "button",
      {
        type: "button",
        onClick: () => j(e),
        className: `w-full flex items-center gap-3 px-3 py-2.5 text-left transition-colors ${a ? "bg-blue-500/10 text-blue-700" : "hover:bg-[var(--bg-tertiary)] text-[var(--text-primary)]"}`,
        role: "option",
        "aria-selected": a,
        children: [
          /* @__PURE__ */ t(
            "div",
            {
              className: `flex-shrink-0 w-8 h-8 flex items-center justify-center ${o ? "bg-blue-500/10" : "bg-violet-500/10"}`,
              style: { borderRadius: "var(--radius-card)" },
              children: /* @__PURE__ */ t(E, { className: `w-4 h-4 ${o ? "text-blue-500" : "text-violet-500"}` })
            }
          ),
          /* @__PURE__ */ r("div", { className: "flex-1 min-w-0", children: [
            /* @__PURE__ */ r("div", { className: "flex items-center gap-2", children: [
              /* @__PURE__ */ t("span", { className: "font-medium truncate", children: e.tenantName }),
              d && /* @__PURE__ */ r("span", { className: "flex-shrink-0 text-xs px-1.5 py-0.5 rounded bg-emerald-500/10 text-emerald-600", children: [
                /* @__PURE__ */ t(H, { className: "w-3 h-3 inline mr-0.5" }),
                s("userRoles.scope.selector.member", "Membre")
              ] })
            ] }),
            /* @__PURE__ */ t("span", { className: "text-xs text-[var(--text-tertiary)]", children: o ? s("userRoles.scope.selector.b2b", "Business") : s("userRoles.scope.selector.b2c", "Personnel") })
          ] }),
          a && /* @__PURE__ */ t(L, { className: "w-4 h-4 text-blue-600 flex-shrink-0" })
        ]
      },
      e.tenantId
    );
  };
  return /* @__PURE__ */ r("div", { ref: w, className: "relative inline-block", children: [
    /* @__PURE__ */ r(
      "button",
      {
        type: "button",
        onClick: () => !b && p(!l),
        disabled: b,
        className: `flex items-center gap-2 px-3 py-2 text-sm font-medium border transition-colors ${i ? "bg-emerald-500/10 border-emerald-500/30 text-emerald-700 hover:bg-emerald-500/20" : "bg-blue-500/10 border-blue-500/30 text-blue-700 hover:bg-blue-500/20"} ${b ? "opacity-50 cursor-not-allowed" : ""}`,
        style: { borderRadius: "var(--radius-button)" },
        "aria-expanded": l,
        "aria-haspopup": "listbox",
        children: [
          i ? /* @__PURE__ */ r($, { children: [
            /* @__PURE__ */ t(S, { className: "w-4 h-4" }),
            /* @__PURE__ */ t("span", { children: s("userRoles.scope.selector.global", "Global (plateforme)") })
          ] }) : /* @__PURE__ */ r($, { children: [
            /* @__PURE__ */ t(E, { className: "w-4 h-4" }),
            /* @__PURE__ */ t("span", { className: "max-w-[150px] truncate", children: C?.tenantName })
          ] }),
          /* @__PURE__ */ t(Q, { className: `w-4 h-4 transition-transform ${l ? "rotate-180" : ""}` })
        ]
      }
    ),
    /* @__PURE__ */ t(
      "div",
      {
        className: `absolute left-0 mt-1 w-80 z-50 transition-all duration-150 ease-out ${l ? "opacity-100 scale-100 translate-y-0" : "opacity-0 scale-95 -translate-y-1 pointer-events-none"}`,
        children: /* @__PURE__ */ r(
          "div",
          {
            className: "bg-[var(--bg-card)] border border-[var(--border-color)] shadow-xl overflow-hidden",
            style: { borderRadius: "var(--radius-card)" },
            children: [
              /* @__PURE__ */ t("div", { className: "px-3 py-2 border-b border-[var(--border-color)] bg-[var(--bg-secondary)]", children: /* @__PURE__ */ r("div", { className: "relative", children: [
                /* @__PURE__ */ t(q, { className: "absolute left-2.5 top-1/2 -translate-y-1/2 w-4 h-4 text-[var(--text-muted)]" }),
                /* @__PURE__ */ t(
                  "input",
                  {
                    ref: R,
                    type: "text",
                    value: v,
                    onChange: (e) => y(e.target.value),
                    placeholder: s("userRoles.scope.selector.search", "Rechercher un tenant..."),
                    className: "w-full pl-8 pr-3 py-1.5 text-sm bg-[var(--bg-primary)] border border-[var(--border-color)] rounded-md text-[var(--text-primary)] placeholder-[var(--text-muted)] focus:outline-none focus:ring-1 focus:ring-[var(--color-accent-500)]"
                  }
                )
              ] }) }),
              /* @__PURE__ */ r("div", { className: "flex items-center gap-1 px-3 py-2 border-b border-[var(--border-color)] bg-[var(--bg-tertiary)]", role: "tablist", children: [
                /* @__PURE__ */ r(
                  "button",
                  {
                    type: "button",
                    onClick: () => N("all"),
                    className: `px-2.5 py-1 text-xs font-medium rounded-md transition-colors ${n === "all" ? "bg-[var(--color-accent-600)] text-white" : "text-[var(--text-secondary)] hover:bg-[var(--bg-hover)]"}`,
                    role: "tab",
                    "aria-selected": n === "all",
                    children: [
                      s("userRoles.scope.selector.filterAll", "Tous"),
                      " (",
                      c.length,
                      ")"
                    ]
                  }
                ),
                /* @__PURE__ */ r(
                  "button",
                  {
                    type: "button",
                    onClick: () => N("b2b"),
                    className: `px-2.5 py-1 text-xs font-medium rounded-md transition-colors ${n === "b2b" ? "bg-blue-600 text-white" : "text-[var(--text-secondary)] hover:bg-[var(--bg-hover)]"}`,
                    role: "tab",
                    "aria-selected": n === "b2b",
                    children: [
                      s("userRoles.scope.selector.filterB2B", "B2B"),
                      " (",
                      A.length,
                      ")"
                    ]
                  }
                ),
                /* @__PURE__ */ r(
                  "button",
                  {
                    type: "button",
                    onClick: () => N("b2c"),
                    className: `px-2.5 py-1 text-xs font-medium rounded-md transition-colors ${n === "b2c" ? "bg-violet-600 text-white" : "text-[var(--text-secondary)] hover:bg-[var(--bg-hover)]"}`,
                    role: "tab",
                    "aria-selected": n === "b2c",
                    children: [
                      s("userRoles.scope.selector.filterB2C", "B2C"),
                      " (",
                      G.length,
                      ")"
                    ]
                  }
                )
              ] }),
              /* @__PURE__ */ r("div", { className: "py-1 max-h-72 overflow-y-auto", role: "listbox", children: [
                /* @__PURE__ */ r(
                  "button",
                  {
                    type: "button",
                    onClick: M,
                    className: `w-full flex items-center gap-3 px-3 py-2.5 text-left transition-colors ${i ? "bg-emerald-500/10 text-emerald-700" : "hover:bg-[var(--bg-tertiary)] text-[var(--text-primary)]"}`,
                    role: "option",
                    "aria-selected": i,
                    children: [
                      /* @__PURE__ */ t(
                        "div",
                        {
                          className: "flex-shrink-0 w-8 h-8 flex items-center justify-center bg-emerald-500/10",
                          style: { borderRadius: "var(--radius-card)" },
                          children: /* @__PURE__ */ t(S, { className: "w-4 h-4 text-emerald-600" })
                        }
                      ),
                      /* @__PURE__ */ r("div", { className: "flex-1 min-w-0", children: [
                        /* @__PURE__ */ t("span", { className: "font-medium block", children: s("userRoles.scope.selector.global", "Global (plateforme)") }),
                        /* @__PURE__ */ t("span", { className: "text-xs text-[var(--text-tertiary)]", children: s("userRoles.scope.selector.globalDesc", "Rôles applicables sur toute la plateforme") })
                      ] }),
                      i && /* @__PURE__ */ t(L, { className: "w-4 h-4 text-emerald-600 flex-shrink-0" })
                    ]
                  }
                ),
                k.length > 0 && /* @__PURE__ */ t("div", { className: "border-t border-[var(--border-color)] my-1" }),
                k.length > 0 ? k.map(F) : /* @__PURE__ */ t("div", { className: "px-3 py-4 text-center text-sm text-[var(--text-muted)]", children: s("userRoles.scope.selector.noResults", "Aucun tenant trouvé") })
              ] })
            ]
          }
        )
      }
    ),
    f && !i && /* @__PURE__ */ r("div", { className: "mt-2 flex items-center gap-2 text-sm text-amber-600", children: [
      /* @__PURE__ */ t(z, { className: "w-4 h-4" }),
      /* @__PURE__ */ t("span", { children: s("userRoles.scope.notMember", "L'utilisateur n'est pas membre de ce tenant") })
    ] })
  ] });
}
export {
  Z as T,
  Y as u
};
//# sourceMappingURL=TenantScopeSelector-ByosotEG.js.map
