import { jsx as e, jsxs as n } from "react/jsx-runtime";
import { useState as y, useCallback as C, useMemo as A, useEffect as B } from "react";
import { useNavigate as ne, useSearchParams as le } from "react-router-dom";
import { useTranslation as j } from "react-i18next";
import { Check as oe, RefreshCw as ce, Search as ie, Loader2 as U, Building2 as D, User as H, AlertCircle as K, Crown as de, ChevronDown as me, ChevronRight as ue, Globe2 as pe, Package as xe, Shield as he, UserPlus as be } from "lucide-react";
import { v as q, P as fe } from "./index-CeEMms9z.js";
function ve({
  steps: t,
  currentStepId: x,
  onStepChange: u,
  allowJumpForward: h = !0,
  className: r = "",
  ariaLabel: c = "Steps"
}) {
  const l = t.findIndex((i) => i.id === x), a = (i) => {
    if (i < 0 || i >= t.length) return !1;
    if (i <= l) return !0;
    if (!h) return !1;
    for (let o = 0; o < i; o++)
      if (!t[o].isValid) return !1;
    return !0;
  }, p = (i, o) => o.isComplete !== void 0 ? o.isComplete : i < l && o.isValid;
  return /* @__PURE__ */ e("nav", { "aria-label": c, className: `w-full ${r}`, children: /* @__PURE__ */ e("ol", { className: "flex items-start justify-between gap-2", children: t.map((i, o) => {
    const m = i.id === x, w = p(o, i), k = a(o);
    return /* @__PURE__ */ e("li", { className: "flex-1 min-w-0", children: /* @__PURE__ */ n(
      "button",
      {
        type: "button",
        onClick: () => k && u(i.id),
        disabled: !k,
        "aria-current": m ? "step" : void 0,
        className: `w-full flex items-start gap-3 text-left p-3 rounded-[var(--radius-card)] transition-colors
                  ${k ? "cursor-pointer hover:bg-[var(--bg-hover)]" : "cursor-not-allowed opacity-60"}
                  ${m ? "bg-[var(--bg-hover)]" : ""}`,
        children: [
          /* @__PURE__ */ e(
            "span",
            {
              className: `flex-shrink-0 inline-flex items-center justify-center w-8 h-8 rounded-full text-sm font-semibold border-2 transition-colors
                    ${w ? "bg-[var(--success-bg)] border-[var(--success-border)] text-[var(--success-text)]" : m ? "bg-[var(--color-accent-500)] border-[var(--color-accent-500)] text-white" : "bg-[var(--bg-secondary)] border-[var(--border-color)] text-[var(--text-muted)]"}`,
              "aria-hidden": "true",
              children: w ? /* @__PURE__ */ e(oe, { className: "w-4 h-4" }) : o + 1
            }
          ),
          /* @__PURE__ */ n("span", { className: "flex flex-col min-w-0", children: [
            /* @__PURE__ */ e(
              "span",
              {
                className: `text-sm font-semibold truncate
                      ${m || w ? "text-[var(--text-primary)]" : "text-[var(--text-secondary)]"}`,
                children: i.label
              }
            ),
            i.description && /* @__PURE__ */ e("span", { className: "text-xs text-[var(--text-muted)] truncate", children: i.description })
          ] })
        ]
      }
    ) }, i.id);
  }) }) });
}
function ge({
  canGoBack: t,
  isLastStep: x,
  isCurrentStepValid: u,
  isSubmitting: h = !1,
  onBack: r,
  onNext: c,
  onSubmit: l,
  onCancel: a,
  labels: p,
  className: i = ""
}) {
  return /* @__PURE__ */ n("div", { className: `flex items-center justify-between gap-3 pt-4 border-t border-[var(--border-color)] ${i}`, children: [
    /* @__PURE__ */ e(
      "button",
      {
        type: "button",
        onClick: a,
        className: "px-4 py-2 text-sm font-medium text-[var(--text-secondary)] hover:text-[var(--text-primary)] transition-colors",
        children: p.cancel
      }
    ),
    /* @__PURE__ */ n("div", { className: "flex items-center gap-2", children: [
      /* @__PURE__ */ e(
        "button",
        {
          type: "button",
          onClick: r,
          disabled: !t || h,
          className: `px-4 py-2 text-sm font-medium rounded-[var(--radius-button)] border border-[var(--border-color)]\r
                     text-[var(--text-primary)] bg-[var(--bg-secondary)] hover:bg-[var(--bg-hover)]\r
                     disabled:opacity-50 disabled:cursor-not-allowed transition-colors`,
          children: p.back
        }
      ),
      x ? /* @__PURE__ */ e(
        "button",
        {
          type: "button",
          onClick: l,
          disabled: !u || h,
          className: `px-4 py-2 text-sm font-medium rounded-[var(--radius-button)] bg-[var(--color-accent-600)] text-white\r
                       hover:bg-[var(--color-accent-700)] disabled:opacity-50 disabled:cursor-not-allowed transition-colors`,
          children: h ? "…" : p.submit
        }
      ) : /* @__PURE__ */ e(
        "button",
        {
          type: "button",
          onClick: c,
          disabled: !u,
          className: `px-4 py-2 text-sm font-medium rounded-[var(--radius-button)] bg-[var(--color-accent-600)] text-white\r
                       hover:bg-[var(--color-accent-700)] disabled:opacity-50 disabled:cursor-not-allowed transition-colors`,
          children: p.next
        }
      )
    ] })
  ] });
}
const Ne = /^[^\s@]+@[^\s@]+\.[^\s@]+$/, we = 8, ye = {
  firstName: "",
  lastName: "",
  email: "",
  password: "",
  sendWelcomeEmail: !0,
  requirePasswordReset: !0
};
function Re() {
  const [t, x] = y(ye), [u, h] = y([]), [r, c] = y(!1), [l, a] = y(null), [p, i] = y([]), [o, m] = y(/* @__PURE__ */ new Map()), [w, k] = y(/* @__PURE__ */ new Map()), [v, S] = y([]), [R, N] = y(!1), [P, V] = y(null), [$, E] = y(!1), [z, L] = y(!1), [I, f] = y(null), M = C((s, d) => {
    x((b) => ({ ...b, [s]: d }));
  }, []), X = C(async (s) => {
    c(!0), a(null);
    try {
      const d = await q.tenants.getAll({
        page: 1,
        pageSize: 100,
        search: s || void 0,
        status: "Active",
        excludeTypes: ["Personal"]
      });
      h(d.items);
    } catch (d) {
      a(d instanceof Error ? d.message : "Failed to load tenants");
    } finally {
      c(!1);
    }
  }, []), Y = C((s) => {
    i((d) => d.includes(s) ? d.filter((b) => b !== s) : [...d, s]), m((d) => {
      const b = new Map(d);
      return b.has(s) ? b.delete(s) : b.set(s, { roleIds: /* @__PURE__ */ new Set(), isOwner: !1 }), b;
    });
  }, []), J = C((s, d) => {
    m((b) => {
      const g = new Map(b), T = g.get(s) ?? { roleIds: /* @__PURE__ */ new Set(), isOwner: !1 };
      return g.set(s, { ...T, roleIds: d }), g;
    });
  }, []), Q = C((s, d) => {
    m((b) => {
      const g = new Map(b), T = g.get(s) ?? { roleIds: /* @__PURE__ */ new Set(), isOwner: !1 }, F = new Set(T.roleIds);
      return F.has(d) ? F.delete(d) : F.add(d), g.set(s, { ...T, roleIds: F }), g;
    });
  }, []), Z = C((s, d) => {
    m((b) => {
      const g = new Map(b), T = g.get(s) ?? { roleIds: /* @__PURE__ */ new Set(), isOwner: !1 };
      return g.set(s, { ...T, isOwner: d }), g;
    });
  }, []), G = C(async () => {
    if (!($ || R)) {
      N(!0), V(null);
      try {
        const s = await q.roles.getAssignable();
        S(s), E(!0);
      } catch (s) {
        V(s instanceof Error ? s.message : "Failed to load roles");
      } finally {
        N(!1);
      }
    }
  }, [$, R]), ee = C(async (s) => {
    G(), k((d) => {
      if (d.has(s)) return d;
      const b = new Map(d);
      return b.set(s, { loading: !0, error: null }), b;
    });
    try {
      const d = await q.tenants.getApplications(s);
      k((b) => {
        const g = new Map(b);
        return g.set(s, { apps: d, loading: !1, error: null }), g;
      });
    } catch (d) {
      const b = d instanceof Error ? d.message : "Failed to load tenant data";
      k((g) => {
        const T = new Map(g);
        return T.set(s, { loading: !1, error: b }), T;
      });
    }
  }, [G]), W = A(() => {
    const s = {};
    return t.firstName.trim() || (s.firstName = "firstNameRequired"), t.lastName.trim() || (s.lastName = "lastNameRequired"), t.email.trim() ? Ne.test(t.email.trim()) || (s.email = "emailInvalid") : s.email = "emailRequired", t.password.length < we && (s.password = "passwordTooShort"), s;
  }, [t]), te = A(() => Object.keys(W).length === 0, [W]), re = !0, ae = A(() => p.length === 0 ? !0 : p.every((s) => (o.get(s)?.roleIds.size ?? 0) > 0), [p, o]), se = C(async () => {
    f(null), L(!0);
    try {
      const s = p.map((g) => {
        const T = o.get(g);
        return {
          tenantId: g,
          roleIds: T ? Array.from(T.roleIds) : [],
          isOwner: T?.isOwner ?? !1
        };
      }), d = {
        email: t.email.trim(),
        password: t.password,
        firstName: t.firstName.trim(),
        lastName: t.lastName.trim(),
        tenantAccess: s.length > 0 ? s : void 0,
        sendWelcomeEmail: t.sendWelcomeEmail,
        requirePasswordReset: t.requirePasswordReset
      };
      return (await q.users.create(d)).id;
    } catch (s) {
      const d = s instanceof Error ? s.message : "Failed to create user";
      return f(d), null;
    } finally {
      L(!1);
    }
  }, [t, p, o]);
  return {
    profile: t,
    setProfileField: M,
    availableTenants: u,
    tenantsLoading: r,
    tenantsError: l,
    reloadTenants: X,
    selectedTenantIds: p,
    toggleTenant: Y,
    tenantConfig: o,
    setTenantRoles: J,
    toggleTenantRole: Q,
    setTenantIsOwner: Z,
    tenantCache: w,
    ensureTenantDataLoaded: ee,
    assignableRoles: v,
    rolesLoading: R,
    rolesError: P,
    isProfileValid: te,
    isTenantsValid: re,
    isAppsRolesValid: ae,
    profileErrors: W,
    submitting: z,
    submitError: I,
    submit: se
  };
}
const Ce = () => {
  const t = "ABCDEFGHJKMNPQRSTUVWXYZ", x = "abcdefghjkmnpqrstuvwxyz", u = "23456789", h = "!@#$%^&*", r = t + x + u + h, c = (a) => a[Math.floor(Math.random() * a.length)], l = [c(t), c(x), c(u), c(h)];
  for (let a = 0; a < 12; a++) l.push(c(r));
  for (let a = l.length - 1; a > 0; a--) {
    const p = Math.floor(Math.random() * (a + 1));
    [l[a], l[p]] = [l[p], l[a]];
  }
  return l.join("");
};
function Te({ profile: t, errors: x, onChange: u, showErrors: h }) {
  const { t: r } = j("admin"), c = (a) => h && x[a] ? r(`users.create.validation.${x[a]}`) : null, l = (a) => {
    const p = "w-full px-3 py-2 text-sm rounded-[var(--radius-input)] border bg-[var(--bg-primary)] text-[var(--text-primary)] focus:outline-none focus:ring-2 focus:ring-[var(--color-accent-500)]", i = c(a) ? "border-[var(--error-border)]" : "border-[var(--border-color)]";
    return `${p} ${i}`;
  };
  return /* @__PURE__ */ n("div", { className: "card p-6 space-y-4", children: [
    /* @__PURE__ */ e("h2", { className: "text-lg font-semibold text-[var(--text-primary)]", children: r("users.create.profile.sectionTitle") }),
    /* @__PURE__ */ e("p", { className: "text-sm text-[var(--text-secondary)]", children: r("users.create.profile.sectionHelp") }),
    /* @__PURE__ */ n("div", { className: "grid grid-cols-1 md:grid-cols-2 gap-4", children: [
      /* @__PURE__ */ n("div", { children: [
        /* @__PURE__ */ n("label", { htmlFor: "user-firstName", className: "block text-sm font-medium text-[var(--text-primary)] mb-1", children: [
          r("users.create.profile.firstName"),
          " ",
          /* @__PURE__ */ e("span", { className: "text-[var(--error-text)]", children: "*" })
        ] }),
        /* @__PURE__ */ e(
          "input",
          {
            id: "user-firstName",
            type: "text",
            value: t.firstName,
            onChange: (a) => u("firstName", a.target.value),
            className: l("firstName"),
            autoComplete: "given-name"
          }
        ),
        c("firstName") && /* @__PURE__ */ e("p", { className: "mt-1 text-xs text-[var(--error-text)]", children: c("firstName") })
      ] }),
      /* @__PURE__ */ n("div", { children: [
        /* @__PURE__ */ n("label", { htmlFor: "user-lastName", className: "block text-sm font-medium text-[var(--text-primary)] mb-1", children: [
          r("users.create.profile.lastName"),
          " ",
          /* @__PURE__ */ e("span", { className: "text-[var(--error-text)]", children: "*" })
        ] }),
        /* @__PURE__ */ e(
          "input",
          {
            id: "user-lastName",
            type: "text",
            value: t.lastName,
            onChange: (a) => u("lastName", a.target.value),
            className: l("lastName"),
            autoComplete: "family-name"
          }
        ),
        c("lastName") && /* @__PURE__ */ e("p", { className: "mt-1 text-xs text-[var(--error-text)]", children: c("lastName") })
      ] })
    ] }),
    /* @__PURE__ */ n("div", { children: [
      /* @__PURE__ */ n("label", { htmlFor: "user-email", className: "block text-sm font-medium text-[var(--text-primary)] mb-1", children: [
        r("users.create.profile.email"),
        " ",
        /* @__PURE__ */ e("span", { className: "text-[var(--error-text)]", children: "*" })
      ] }),
      /* @__PURE__ */ e(
        "input",
        {
          id: "user-email",
          type: "email",
          value: t.email,
          onChange: (a) => u("email", a.target.value),
          className: l("email"),
          autoComplete: "email"
        }
      ),
      c("email") && /* @__PURE__ */ e("p", { className: "mt-1 text-xs text-[var(--error-text)]", children: c("email") })
    ] }),
    /* @__PURE__ */ n("div", { children: [
      /* @__PURE__ */ n("label", { htmlFor: "user-password", className: "block text-sm font-medium text-[var(--text-primary)] mb-1", children: [
        r("users.create.profile.password"),
        " ",
        /* @__PURE__ */ e("span", { className: "text-[var(--error-text)]", children: "*" })
      ] }),
      /* @__PURE__ */ n("div", { className: "flex gap-2", children: [
        /* @__PURE__ */ e(
          "input",
          {
            id: "user-password",
            type: "text",
            value: t.password,
            onChange: (a) => u("password", a.target.value),
            className: l("password"),
            autoComplete: "new-password"
          }
        ),
        /* @__PURE__ */ n(
          "button",
          {
            type: "button",
            onClick: () => u("password", Ce()),
            className: "px-3 py-2 text-sm font-medium rounded-[var(--radius-button)] border border-[var(--border-color)] bg-[var(--bg-secondary)] hover:bg-[var(--bg-hover)] text-[var(--text-primary)] flex items-center gap-2",
            title: r("users.create.profile.generatePassword"),
            children: [
              /* @__PURE__ */ e(ce, { className: "w-4 h-4" }),
              r("users.create.profile.generatePassword")
            ]
          }
        )
      ] }),
      c("password") && /* @__PURE__ */ e("p", { className: "mt-1 text-xs text-[var(--error-text)]", children: c("password") }),
      /* @__PURE__ */ e("p", { className: "mt-1 text-xs text-[var(--text-muted)]", children: r("users.create.profile.passwordHelp") })
    ] }),
    /* @__PURE__ */ n("div", { className: "pt-2 border-t border-[var(--border-color)] space-y-2", children: [
      /* @__PURE__ */ n("label", { className: "flex items-center gap-2 text-sm text-[var(--text-primary)] cursor-pointer", children: [
        /* @__PURE__ */ e(
          "input",
          {
            type: "checkbox",
            checked: t.sendWelcomeEmail,
            onChange: (a) => u("sendWelcomeEmail", a.target.checked),
            className: "rounded border-[var(--border-color)]"
          }
        ),
        /* @__PURE__ */ e("span", { children: r("users.create.profile.sendWelcomeEmail") })
      ] }),
      /* @__PURE__ */ n("label", { className: "flex items-center gap-2 text-sm text-[var(--text-primary)] cursor-pointer", children: [
        /* @__PURE__ */ e(
          "input",
          {
            type: "checkbox",
            checked: t.requirePasswordReset,
            onChange: (a) => u("requirePasswordReset", a.target.checked),
            className: "rounded border-[var(--border-color)]"
          }
        ),
        /* @__PURE__ */ e("span", { children: r("users.create.profile.requirePasswordReset") })
      ] })
    ] })
  ] });
}
function ke({
  availableTenants: t,
  loading: x,
  error: u,
  selectedTenantIds: h,
  onToggle: r,
  onReload: c
}) {
  const { t: l } = j("admin"), [a, p] = y("");
  B(() => {
    c();
  }, []);
  const i = A(() => {
    const m = a.trim().toLowerCase();
    return m ? t.filter(
      (w) => w.name.toLowerCase().includes(m) || w.slug.toLowerCase().includes(m)
    ) : t;
  }, [t, a]), o = h.length;
  return /* @__PURE__ */ n("div", { className: "card p-6 space-y-4", children: [
    /* @__PURE__ */ n("div", { className: "flex items-start justify-between gap-3", children: [
      /* @__PURE__ */ n("div", { children: [
        /* @__PURE__ */ e("h2", { className: "text-lg font-semibold text-[var(--text-primary)]", children: l("users.create.tenants.title") }),
        /* @__PURE__ */ e("p", { className: "text-sm text-[var(--text-secondary)] mt-1", children: l("users.create.tenants.help") })
      ] }),
      /* @__PURE__ */ e("span", { className: "flex-shrink-0 px-3 py-1 rounded-full text-xs font-medium bg-[var(--accent-bg)] text-[var(--color-accent-500)] border border-[var(--color-accent-500)]/30", children: l("users.create.tenants.selectedCount", { count: o }) })
    ] }),
    /* @__PURE__ */ n("div", { className: "relative", children: [
      /* @__PURE__ */ e(ie, { className: "absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-[var(--text-muted)]" }),
      /* @__PURE__ */ e(
        "input",
        {
          type: "search",
          value: a,
          onChange: (m) => p(m.target.value),
          placeholder: l("users.create.tenants.searchPlaceholder"),
          className: "w-full pl-10 pr-3 py-2 text-sm rounded-[var(--radius-input)] border border-[var(--border-color)] bg-[var(--bg-primary)] text-[var(--text-primary)] focus:outline-none focus:ring-2 focus:ring-[var(--color-accent-500)]"
        }
      )
    ] }),
    u && /* @__PURE__ */ e("div", { className: "p-3 rounded-[var(--radius-card)] bg-[var(--error-bg)] text-[var(--error-text)] text-sm", children: u }),
    x ? /* @__PURE__ */ n("div", { className: "flex items-center justify-center py-8 text-[var(--text-muted)]", children: [
      /* @__PURE__ */ e(U, { className: "w-5 h-5 animate-spin mr-2" }),
      l("users.create.tenants.loading")
    ] }) : i.length === 0 ? /* @__PURE__ */ e("div", { className: "p-6 text-center text-sm text-[var(--text-muted)]", children: t.length === 0 ? l("users.create.tenants.empty") : l("users.create.tenants.noMatch") }) : /* @__PURE__ */ e("ul", { className: "space-y-2 max-h-[480px] overflow-y-auto", children: i.map((m) => {
      const w = h.includes(m.id);
      return /* @__PURE__ */ e("li", { children: /* @__PURE__ */ n(
        "label",
        {
          className: `flex items-start gap-3 p-3 rounded-[var(--radius-card)] border cursor-pointer transition-colors
                    ${w ? "border-[var(--color-accent-500)] bg-[var(--accent-bg)]" : "border-[var(--border-color)] bg-[var(--bg-primary)] hover:bg-[var(--bg-hover)]"}`,
          children: [
            /* @__PURE__ */ e(
              "input",
              {
                type: "checkbox",
                checked: w,
                onChange: () => r(m.id),
                className: "mt-1 rounded border-[var(--border-color)]"
              }
            ),
            /* @__PURE__ */ e("span", { className: "flex-shrink-0 w-10 h-10 rounded-full bg-[var(--bg-secondary)] flex items-center justify-center", children: m.type === "Business" ? /* @__PURE__ */ e(D, { className: "w-5 h-5 text-[var(--text-secondary)]" }) : /* @__PURE__ */ e(H, { className: "w-5 h-5 text-[var(--text-secondary)]" }) }),
            /* @__PURE__ */ n("span", { className: "flex-1 min-w-0", children: [
              /* @__PURE__ */ n("span", { className: "flex items-center gap-2 flex-wrap", children: [
                /* @__PURE__ */ e("span", { className: "font-medium text-[var(--text-primary)] truncate", children: m.name }),
                /* @__PURE__ */ n("span", { className: "text-xs text-[var(--text-muted)]", children: [
                  "/",
                  m.slug
                ] })
              ] }),
              /* @__PURE__ */ e("span", { className: "block mt-0.5 text-xs text-[var(--text-secondary)]", children: l("users.create.tenants.tenantMeta", {
                members: m.memberCount,
                apps: m.applicationCount
              }) })
            ] })
          ]
        }
      ) }, m.id);
    }) })
  ] });
}
const _ = "__global__";
function Se({
  selectedTenantIds: t,
  availableTenants: x,
  tenantConfig: u,
  tenantCache: h,
  assignableRoles: r,
  rolesLoading: c,
  rolesError: l,
  onEnsureLoaded: a,
  onToggleRole: p,
  onSetIsOwner: i
}) {
  const { t: o } = j("admin"), [m, w] = y(/* @__PURE__ */ new Set());
  B(() => {
    t.forEach((v) => {
      a(v);
    });
  }, [t]);
  const k = (v) => {
    w((S) => {
      const R = new Set(S);
      return R.has(v) ? R.delete(v) : R.add(v), R;
    });
  };
  return t.length === 0 ? /* @__PURE__ */ n("div", { className: "card p-8 text-center", children: [
    /* @__PURE__ */ e(K, { className: "w-10 h-10 text-[var(--text-muted)] mx-auto mb-3" }),
    /* @__PURE__ */ e("h2", { className: "text-lg font-semibold text-[var(--text-primary)] mb-1", children: o("users.create.appsRoles.noTenantsTitle") }),
    /* @__PURE__ */ e("p", { className: "text-sm text-[var(--text-secondary)]", children: o("users.create.appsRoles.noTenantsHelp") })
  ] }) : /* @__PURE__ */ n("div", { className: "space-y-4", children: [
    /* @__PURE__ */ n("div", { className: "card p-4", children: [
      /* @__PURE__ */ e("h2", { className: "text-lg font-semibold text-[var(--text-primary)]", children: o("users.create.appsRoles.title") }),
      /* @__PURE__ */ e("p", { className: "text-sm text-[var(--text-secondary)] mt-1", children: o("users.create.appsRoles.help") }),
      l && /* @__PURE__ */ e("div", { className: "mt-3 p-3 rounded-[var(--radius-card)] bg-[var(--error-bg)] text-[var(--error-text)] text-sm", children: l })
    ] }),
    t.map((v) => {
      const S = x.find((f) => f.id === v), R = h.get(v), N = u.get(v), P = S?.name ?? v, V = (R?.loading ?? !0) || c, $ = R?.apps ?? [];
      new Set($.map((f) => f.applicationId));
      const E = $.map((f) => ({
        appId: f.applicationId,
        appLabel: f.label,
        roles: r.filter((M) => M.applicationId === f.applicationId)
      })), z = r.filter((f) => f.applicationId === null);
      z.length > 0 && E.push({ appId: null, appLabel: o("users.create.appsRoles.globalRoles"), roles: z });
      const L = N?.roleIds.size ?? 0, I = E.reduce((f, M) => f + M.roles.length, 0);
      return /* @__PURE__ */ n("div", { className: "card p-5 space-y-4", children: [
        /* @__PURE__ */ n("header", { className: "flex items-center justify-between gap-3 flex-wrap", children: [
          /* @__PURE__ */ n("div", { className: "flex items-center gap-3", children: [
            /* @__PURE__ */ n("div", { children: [
              /* @__PURE__ */ e("h3", { className: "font-semibold text-[var(--text-primary)]", children: P }),
              S?.slug && /* @__PURE__ */ n("span", { className: "text-xs text-[var(--text-muted)]", children: [
                "/",
                S.slug
              ] })
            ] }),
            /* @__PURE__ */ e("span", { className: `px-2 py-0.5 rounded-full text-xs font-medium border
                  ${L > 0 ? "bg-[var(--accent-bg)] text-[var(--color-accent-500)] border-[var(--color-accent-500)]/30" : "bg-[var(--warning-bg)] text-[var(--warning-text)] border-[var(--warning-border)]"}`, children: o("users.create.appsRoles.assignedRoles", { count: L }) })
          ] }),
          /* @__PURE__ */ n("label", { className: "flex items-center gap-2 text-sm text-[var(--text-primary)] cursor-pointer", children: [
            /* @__PURE__ */ e(
              "input",
              {
                type: "checkbox",
                checked: N?.isOwner ?? !1,
                onChange: (f) => i(v, f.target.checked),
                className: "rounded border-[var(--border-color)]"
              }
            ),
            /* @__PURE__ */ e(de, { className: "w-4 h-4 text-[var(--warning-text)]" }),
            /* @__PURE__ */ e("span", { children: o("users.create.appsRoles.isOwner") })
          ] })
        ] }),
        V ? /* @__PURE__ */ n("div", { className: "flex items-center justify-center py-6 text-[var(--text-muted)]", children: [
          /* @__PURE__ */ e(U, { className: "w-5 h-5 animate-spin mr-2" }),
          o("users.create.appsRoles.loading")
        ] }) : R?.error ? /* @__PURE__ */ e("div", { className: "p-3 rounded-[var(--radius-card)] bg-[var(--error-bg)] text-[var(--error-text)] text-sm", children: R.error }) : E.length === 0 ? /* @__PURE__ */ e("div", { className: "p-4 rounded-[var(--radius-card)] bg-[var(--bg-secondary)] text-sm text-[var(--text-secondary)]", children: o("users.create.appsRoles.noAppsOrRoles") }) : /* @__PURE__ */ e("ul", { className: "space-y-2", children: E.map((f) => /* @__PURE__ */ e(
          Pe,
          {
            group: f,
            tenantId: v,
            isExpanded: m.has(`${v}:${f.appId ?? _}`),
            onToggle: () => k(`${v}:${f.appId ?? _}`),
            assignedRoleIds: N?.roleIds ?? /* @__PURE__ */ new Set(),
            onRoleToggle: p
          },
          f.appId ?? _
        )) }),
        !V && L === 0 && I > 0 && /* @__PURE__ */ e("p", { className: "text-xs text-[var(--warning-text)]", children: o("users.create.appsRoles.noRolesSelected") })
      ] }, v);
    })
  ] });
}
function Pe({
  group: t,
  tenantId: x,
  isExpanded: u,
  onToggle: h,
  assignedRoleIds: r,
  onRoleToggle: c
}) {
  const { t: l } = j("admin"), a = t.appId === null, p = A(
    () => t.roles.filter((o) => r.has(o.id)).length,
    [t.roles, r]
  );
  return /* @__PURE__ */ n("li", { className: "border border-[var(--border-color)] rounded-[var(--radius-card)] overflow-hidden", children: [
    /* @__PURE__ */ n(
      "button",
      {
        type: "button",
        onClick: h,
        className: "w-full flex items-center gap-3 p-3 hover:bg-[var(--bg-hover)] transition-colors text-left",
        "aria-expanded": u,
        children: [
          u ? /* @__PURE__ */ e(me, { className: "w-4 h-4 text-[var(--text-muted)] flex-shrink-0" }) : /* @__PURE__ */ e(ue, { className: "w-4 h-4 text-[var(--text-muted)] flex-shrink-0" }),
          /* @__PURE__ */ e(a ? pe : xe, { className: `w-4 h-4 flex-shrink-0 ${a ? "text-[var(--color-accent-500)]" : "text-[var(--text-secondary)]"}` }),
          /* @__PURE__ */ e("span", { className: "flex-1 font-medium text-[var(--text-primary)] truncate", children: t.appLabel }),
          /* @__PURE__ */ e("span", { className: `px-2 py-0.5 rounded-full text-xs font-medium
          ${p > 0 ? "bg-[var(--accent-bg)] text-[var(--color-accent-500)]" : "bg-[var(--bg-secondary)] text-[var(--text-muted)]"}`, children: l("users.create.appsRoles.appCount", {
            assigned: p,
            total: t.roles.length
          }) })
        ]
      }
    ),
    u && /* @__PURE__ */ e("div", { className: "border-t border-[var(--border-color)] p-3 bg-[var(--bg-secondary)]", children: t.roles.length === 0 ? /* @__PURE__ */ e("p", { className: "text-sm text-[var(--text-muted)] py-2", children: l("users.create.appsRoles.noRolesAvailable") }) : /* @__PURE__ */ e("ul", { className: "grid grid-cols-1 md:grid-cols-2 gap-2", children: t.roles.map((o) => {
      const m = r.has(o.id);
      return /* @__PURE__ */ e("li", { children: /* @__PURE__ */ n(
        "label",
        {
          className: `flex items-start gap-3 p-2 rounded-[var(--radius-card)] border cursor-pointer transition-colors
                        ${m ? "border-[var(--color-accent-500)] bg-[var(--accent-bg)]" : "border-[var(--border-color)] bg-[var(--bg-primary)] hover:bg-[var(--bg-hover)]"}`,
          children: [
            /* @__PURE__ */ e(
              "input",
              {
                type: "checkbox",
                checked: m,
                onChange: () => c(x, o.id),
                className: "mt-1 rounded border-[var(--border-color)]"
              }
            ),
            /* @__PURE__ */ n("span", { className: "flex-1 min-w-0", children: [
              /* @__PURE__ */ e("span", { className: "block text-sm font-medium text-[var(--text-primary)] truncate", children: o.name }),
              o.description && /* @__PURE__ */ e("span", { className: "block text-xs text-[var(--text-muted)] truncate", children: o.description }),
              /* @__PURE__ */ e("span", { className: "block text-xs text-[var(--text-muted)] mt-0.5", children: l("users.create.appsRoles.permissionsCount", { count: o.permissionsCount }) })
            ] })
          ]
        }
      ) }, o.id);
    }) }) })
  ] });
}
const O = ["profile", "tenants", "appsRoles"], Ee = (t) => O.includes(t ?? "") ? t : "profile";
function je() {
  const { t } = j("admin"), x = ne(), [u, h] = le(), r = Re(), c = Ee(u.get("step")), l = O.indexOf(c), [a, p] = y(!1), i = C((N) => {
    const P = new URLSearchParams(u);
    N === "profile" ? P.delete("step") : P.set("step", N), h(P, { replace: !0 });
  }, [u, h]), o = A(() => [
    {
      id: "profile",
      label: t("users.create.wizard.step1.label"),
      description: t("users.create.wizard.step1.description"),
      icon: /* @__PURE__ */ e(H, { className: "w-4 h-4" }),
      isValid: r.isProfileValid
    },
    {
      id: "tenants",
      label: t("users.create.wizard.step2.label"),
      description: t("users.create.wizard.step2.description"),
      icon: /* @__PURE__ */ e(D, { className: "w-4 h-4" }),
      isValid: r.isTenantsValid
    },
    {
      id: "appsRoles",
      label: t("users.create.wizard.step3.label"),
      description: t("users.create.wizard.step3.description"),
      icon: /* @__PURE__ */ e(he, { className: "w-4 h-4" }),
      isValid: r.isAppsRolesValid
    }
  ], [t, r.isProfileValid, r.isTenantsValid, r.isAppsRolesValid]), m = l === O.length - 1, w = c === "profile" ? r.isProfileValid : c === "tenants" ? r.isTenantsValid : r.isAppsRolesValid, k = C(() => {
    if (c === "profile" && p(!0), !w) return;
    const N = O[l + 1];
    N && i(N);
  }, [c, l, w, i]), v = C(() => {
    const N = O[l - 1];
    N && i(N);
  }, [l, i]), S = C(() => {
    x("/administration/users");
  }, [x]), R = C(async () => {
    if (!r.isProfileValid) {
      p(!0), i("profile");
      return;
    }
    if (!r.isAppsRolesValid) {
      i("appsRoles");
      return;
    }
    const N = await r.submit();
    N && x(`/administration/users/${N}`);
  }, [r, i, x]);
  return /* @__PURE__ */ n(
    fe,
    {
      title: t("users.create.pageTitle"),
      subtitle: t("users.create.pageSubtitle"),
      icon: /* @__PURE__ */ e(be, { className: "w-6 h-6 text-[var(--color-accent-500)]" }),
      breadcrumbs: [
        { label: t("navigation:administration", { defaultValue: "Administration" }), href: "/administration" },
        { label: t("users.title"), href: "/administration/users" },
        { label: t("users.create.pageTitle") }
      ],
      children: [
        /* @__PURE__ */ e("div", { className: "card p-4", children: /* @__PURE__ */ e(
          ve,
          {
            steps: o,
            currentStepId: c,
            onStepChange: i,
            ariaLabel: t("users.create.wizard.ariaLabel")
          }
        ) }),
        r.submitError && /* @__PURE__ */ n("div", { className: "card p-3 flex items-start gap-2 bg-[var(--error-bg)] text-[var(--error-text)]", children: [
          /* @__PURE__ */ e(K, { className: "w-4 h-4 mt-0.5 flex-shrink-0" }),
          /* @__PURE__ */ e("span", { className: "text-sm", children: r.submitError })
        ] }),
        c === "profile" && /* @__PURE__ */ e(
          Te,
          {
            profile: r.profile,
            errors: r.profileErrors,
            onChange: r.setProfileField,
            showErrors: a
          }
        ),
        c === "tenants" && /* @__PURE__ */ e(
          ke,
          {
            availableTenants: r.availableTenants,
            loading: r.tenantsLoading,
            error: r.tenantsError,
            selectedTenantIds: r.selectedTenantIds,
            onToggle: r.toggleTenant,
            onReload: r.reloadTenants
          }
        ),
        c === "appsRoles" && /* @__PURE__ */ e(
          Se,
          {
            selectedTenantIds: r.selectedTenantIds,
            availableTenants: r.availableTenants,
            tenantConfig: r.tenantConfig,
            tenantCache: r.tenantCache,
            assignableRoles: r.assignableRoles,
            rolesLoading: r.rolesLoading,
            rolesError: r.rolesError,
            onEnsureLoaded: r.ensureTenantDataLoaded,
            onToggleRole: r.toggleTenantRole,
            onSetIsOwner: r.setTenantIsOwner
          }
        ),
        /* @__PURE__ */ e("div", { className: "card p-4", children: /* @__PURE__ */ e(
          ge,
          {
            canGoBack: l > 0,
            isLastStep: m,
            isCurrentStepValid: w,
            isSubmitting: r.submitting,
            onBack: v,
            onNext: k,
            onSubmit: R,
            onCancel: S,
            labels: {
              back: t("users.create.buttons.back"),
              next: t("users.create.buttons.next"),
              submit: t("users.create.buttons.submit"),
              cancel: t("users.create.buttons.cancel")
            }
          }
        ) })
      ]
    }
  );
}
export {
  je as UserCreatePage
};
//# sourceMappingURL=UserCreatePage-DBJitbqi.js.map
