import { jsxs as r, jsx as t, Fragment as C } from "react/jsx-runtime";
import { useState as g, useRef as B, useEffect as y, useMemo as I } from "react";
import { useTranslation as F } from "react-i18next";
import { Globe as $, Building2 as S, ChevronDown as P, Search as Q, Check as E, AlertTriangle as q, User as z } from "lucide-react";
function W({
  value: N,
  onChange: w,
  adminTenants: i,
  userTenantMemberships: k = [],
  disabled: b = !1,
  showNotMemberWarning: L = !1,
  align: G = "left"
}) {
  const { t: s } = F("admin"), [d, u] = g(!1), [m, p] = g(""), [o, x] = g("all"), v = B(null), h = B(null);
  y(() => {
    function e(a) {
      v.current && !v.current.contains(a.target) && u(!1);
    }
    return document.addEventListener("mousedown", e), () => document.removeEventListener("mousedown", e);
  }, []), y(() => {
    function e(a) {
      a.key === "Escape" && u(!1);
    }
    return document.addEventListener("keydown", e), () => document.removeEventListener("keydown", e);
  }, []), y(() => {
    d && h.current && setTimeout(() => h.current?.focus(), 50);
  }, [d]);
  const R = I(() => {
    const e = /* @__PURE__ */ new Map();
    return k.forEach((a) => e.set(a.tenantId, a)), e;
  }, [k]), { b2bTenants: M, b2cTenants: j, filteredTenants: f } = I(() => {
    const e = i.filter((c) => c.tenantType === "Business"), a = i.filter((c) => c.tenantType === "Personal");
    let n = i;
    if (o === "b2b" ? n = e : o === "b2c" && (n = a), m.trim()) {
      const c = m.toLowerCase().trim();
      n = n.filter((D) => D.tenantName.toLowerCase().includes(c));
    }
    return { b2bTenants: e, b2cTenants: a, filteredTenants: n };
  }, [i, o, m]), l = N === "global", T = l ? null : N, O = () => {
    w("global"), u(!1), p("");
  }, A = (e) => {
    const a = R.get(e.tenantId);
    w({
      tenantId: e.tenantId,
      tenantSlug: e.tenantSlug,
      tenantUserId: a?.tenantUserId || "",
      tenantName: e.tenantName
    }), u(!1), p("");
  };
  if (i.length === 0)
    return null;
  const U = (e) => {
    const a = !l && T?.tenantId === e.tenantId, n = e.tenantType === "Business", c = R.has(e.tenantId);
    return /* @__PURE__ */ r(
      "button",
      {
        type: "button",
        onClick: () => A(e),
        className: `w-full flex items-center gap-3 px-3 py-2.5 text-left transition-colors ${a ? "bg-[var(--info-bg)] text-[var(--info-text)]" : "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 ${n ? "bg-[var(--info-bg)]" : "bg-[var(--color-accent-50)]"} rounded-[var(--radius-card)]`,
              children: /* @__PURE__ */ t(S, { className: `w-4 h-4 ${n ? "text-[var(--info-text)]" : "text-[var(--color-accent-600)]"}` })
            }
          ),
          /* @__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 }),
              c && /* @__PURE__ */ r("span", { className: "flex-shrink-0 text-xs px-1.5 py-0.5 rounded bg-[var(--success-bg)] text-[var(--success-text)]", children: [
                /* @__PURE__ */ t(z, { 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: n ? s("userRoles.scope.selector.b2b", "Business") : s("userRoles.scope.selector.b2c", "Personnel") })
          ] }),
          a && /* @__PURE__ */ t(E, { className: "w-4 h-4 text-[var(--info-text)] flex-shrink-0" })
        ]
      },
      e.tenantId
    );
  };
  return /* @__PURE__ */ r("div", { ref: v, className: "relative inline-block", children: [
    /* @__PURE__ */ r(
      "button",
      {
        type: "button",
        onClick: () => !b && u(!d),
        disabled: b,
        className: `flex items-center gap-2 px-3 py-2 text-sm font-medium border transition-colors ${l ? "bg-[var(--color-accent-600)]/10 border-[var(--color-accent-600)]/30 text-[var(--color-accent-600)] hover:bg-[var(--color-accent-600)]/15" : "bg-[var(--info-bg)] border-[var(--info-border)] text-[var(--info-text)] hover:opacity-80"} ${b ? "opacity-50 cursor-not-allowed" : ""} rounded-[var(--radius-button)]`,
        "aria-expanded": d,
        "aria-haspopup": "listbox",
        children: [
          l ? /* @__PURE__ */ r(C, { children: [
            /* @__PURE__ */ t($, { className: "w-4 h-4" }),
            /* @__PURE__ */ t("span", { children: s("userRoles.scope.selector.global", "Global (plateforme)") })
          ] }) : /* @__PURE__ */ r(C, { children: [
            /* @__PURE__ */ t(S, { className: "w-4 h-4" }),
            /* @__PURE__ */ t("span", { className: "max-w-[150px] truncate", children: T?.tenantName })
          ] }),
          /* @__PURE__ */ t(P, { className: `w-4 h-4 transition-transform ${d ? "rotate-180" : ""}` })
        ]
      }
    ),
    /* @__PURE__ */ t(
      "div",
      {
        className: `absolute ${G === "right" ? "right-0" : "left-0"} mt-1 w-80 z-50 transition-all duration-150 ease-out ${d ? "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 rounded-[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: h,
                    type: "text",
                    value: m,
                    onChange: (e) => p(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: () => x("all"),
                    className: `px-2.5 py-1 text-xs font-medium rounded-md transition-colors ${o === "all" ? "bg-[var(--color-accent-600)] text-white" : "text-[var(--text-secondary)] hover:bg-[var(--bg-hover)]"}`,
                    role: "tab",
                    "aria-selected": o === "all",
                    children: [
                      s("userRoles.scope.selector.filterAll", "Tous"),
                      " (",
                      i.length,
                      ")"
                    ]
                  }
                ),
                /* @__PURE__ */ r(
                  "button",
                  {
                    type: "button",
                    onClick: () => x("b2b"),
                    className: `px-2.5 py-1 text-xs font-medium rounded-md transition-colors ${o === "b2b" ? "bg-[var(--color-accent-600)] text-white" : "text-[var(--text-secondary)] hover:bg-[var(--bg-hover)]"}`,
                    role: "tab",
                    "aria-selected": o === "b2b",
                    children: [
                      s("userRoles.scope.selector.filterB2B", "B2B"),
                      " (",
                      M.length,
                      ")"
                    ]
                  }
                ),
                /* @__PURE__ */ r(
                  "button",
                  {
                    type: "button",
                    onClick: () => x("b2c"),
                    className: `px-2.5 py-1 text-xs font-medium rounded-md transition-colors ${o === "b2c" ? "bg-violet-600 text-white" : "text-[var(--text-secondary)] hover:bg-[var(--bg-hover)]"}`,
                    role: "tab",
                    "aria-selected": o === "b2c",
                    children: [
                      s("userRoles.scope.selector.filterB2C", "B2C"),
                      " (",
                      j.length,
                      ")"
                    ]
                  }
                )
              ] }),
              /* @__PURE__ */ r("div", { className: "py-1 max-h-72 overflow-y-auto", role: "listbox", children: [
                /* @__PURE__ */ r(
                  "button",
                  {
                    type: "button",
                    onClick: O,
                    className: `w-full flex items-center gap-3 px-3 py-2.5 text-left transition-colors ${l ? "bg-[var(--color-accent-600)]/10 text-[var(--color-accent-600)]" : "hover:bg-[var(--bg-tertiary)] text-[var(--text-primary)]"}`,
                    role: "option",
                    "aria-selected": l,
                    children: [
                      /* @__PURE__ */ t(
                        "div",
                        {
                          className: "flex-shrink-0 w-8 h-8 flex items-center justify-center bg-[var(--color-accent-600)]/10 rounded-[var(--radius-card)]",
                          children: /* @__PURE__ */ t($, { className: "w-4 h-4 text-[var(--color-accent-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") })
                      ] }),
                      l && /* @__PURE__ */ t(E, { className: "w-4 h-4 text-[var(--color-accent-600)] flex-shrink-0" })
                    ]
                  }
                ),
                f.length > 0 && /* @__PURE__ */ t("div", { className: "border-t border-[var(--border-color)] my-1" }),
                f.length > 0 ? f.map(U) : /* @__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é") })
              ] })
            ]
          }
        )
      }
    ),
    L && !l && /* @__PURE__ */ r("div", { className: "mt-2 flex items-center gap-2 text-sm text-[var(--warning-text)]", children: [
      /* @__PURE__ */ t(q, { className: "w-4 h-4" }),
      /* @__PURE__ */ t("span", { children: s("userRoles.scope.notMember", "L'utilisateur n'est pas membre de ce tenant") })
    ] })
  ] });
}
export {
  W as T
};
//# sourceMappingURL=TenantScopeSelector-C_McRF0W.js.map
