import { jsx as e, jsxs as t } from "react/jsx-runtime";
import { useCallback as l } from "react";
import { useNavigate as B } from "react-router-dom";
import { useTranslation as S } from "react-i18next";
import { Star as u, Globe as M, Building2 as o, User as k, AlertCircle as W, Layers as j } from "lucide-react";
import { a as E, aN as F, P as G } from "./index-CeEMms9z.js";
import { T as v } from "./TenantCard-B2SOvtZs.js";
function q() {
  const { t: r } = S("navigation"), n = B(), {
    currentTenant: d,
    userTenants: m,
    b2bTenants: p,
    personalTenant: s,
    switchTenant: b,
    setDefaultTenant: y,
    clearDefaultTenant: N,
    isLoading: T,
    hasGlobalRoles: D
  } = E(), { enableB2C: P } = F(), c = !m.some((a) => a.isDefault), f = l(async (a) => {
    try {
      await b(a), n("/myspace");
    } catch (g) {
      console.error("Failed to switch tenant:", g);
    }
  }, [b, n]), x = l(async (a) => {
    try {
      await y(a);
    } catch (g) {
      console.error("Failed to set default tenant:", g);
    }
  }, [y]), C = l(async () => {
    try {
      await N();
    } catch (a) {
      console.error("Failed to clear default tenant:", a);
    }
  }, [N]), w = l((a) => {
    n(`/administration/tenants/${a}`);
  }, [n]);
  if (T)
    return /* @__PURE__ */ e("div", { className: "flex items-center justify-center min-h-[400px]", children: /* @__PURE__ */ e("div", { className: "animate-spin rounded-full h-8 w-8 border-b-2 border-[var(--color-accent-500)]" }) });
  const i = m.find((a) => a.isDefault), h = m.filter((a) => a.type !== "Business" && a.type !== "Personal");
  return /* @__PURE__ */ t(
    G,
    {
      title: r("myspace.tenants.title", "My Workspaces"),
      subtitle: r("myspace.tenants.description", "Manage your workspace access and set your default workspace"),
      icon: /* @__PURE__ */ e(o, { className: "w-6 h-6" }),
      children: [
        /* @__PURE__ */ t("section", { children: [
          /* @__PURE__ */ t("div", { className: "flex items-center gap-2 mb-4", children: [
            /* @__PURE__ */ e(u, { className: "w-5 h-5 text-[var(--warning-dot)]" }),
            /* @__PURE__ */ e("h2", { className: "text-lg font-semibold text-[var(--text-primary)]", children: r("myspace.tenants.defaultPreference.title", "Default Preference") })
          ] }),
          /* @__PURE__ */ t("div", { className: "grid grid-cols-1 sm:grid-cols-2 gap-4 max-w-2xl", children: [
            D && /* @__PURE__ */ e(
              "button",
              {
                onClick: c ? void 0 : C,
                disabled: c,
                className: `relative text-left p-5 bg-[var(--bg-card)] border overflow-hidden transition-all duration-200 ${c ? "border-[var(--color-accent-500)] ring-2 ring-[var(--color-accent-500)]/20" : "border-[var(--border-color)] hover:border-[var(--color-accent-300)] cursor-pointer"} rounded-[var(--radius-card)]`,
                children: /* @__PURE__ */ t("div", { className: "flex items-start gap-3", children: [
                  /* @__PURE__ */ e("div", { className: "w-12 h-12 rounded-xl flex items-center justify-center bg-gradient-to-br from-[var(--success-bg)]/20 to-[var(--success-bg)]/30", children: /* @__PURE__ */ e(M, { className: "w-6 h-6 text-[var(--success-dot)]" }) }),
                  /* @__PURE__ */ t("div", { className: "flex-1 min-w-0", children: [
                    /* @__PURE__ */ t("div", { className: "flex items-center gap-2", children: [
                      /* @__PURE__ */ e("h3", { className: "font-semibold text-[var(--text-primary)]", children: r("myspace.tenants.defaultPreference.global", "Global") }),
                      c && /* @__PURE__ */ t("span", { className: "inline-flex items-center gap-1 px-2 py-0.5 text-xs font-medium rounded-full bg-[var(--color-accent-500)]/10 text-[var(--color-accent-600)]", children: [
                        /* @__PURE__ */ e(u, { className: "w-3 h-3 fill-current" }),
                        r("tenants.default", "Default")
                      ] })
                    ] }),
                    /* @__PURE__ */ e("p", { className: "text-sm text-[var(--text-secondary)] mt-1", children: r("myspace.tenants.defaultPreference.globalDescription", "No specific workspace on login — choose each time") })
                  ] })
                ] })
              }
            ),
            i && /* @__PURE__ */ e(
              "div",
              {
                className: "relative p-5 bg-[var(--bg-card)] border border-[var(--color-accent-500)] ring-2 ring-[var(--color-accent-500)]/20 overflow-hidden rounded-[var(--radius-card)]",
                children: /* @__PURE__ */ t("div", { className: "flex items-start gap-3", children: [
                  /* @__PURE__ */ e("div", { className: `w-12 h-12 rounded-xl flex items-center justify-center ${i.type === "Business", "bg-gradient-to-br from-[var(--info-bg)]/20 to-[var(--info-bg)]/30"}`, children: i.type === "Business" ? /* @__PURE__ */ e(o, { className: "w-6 h-6 text-[var(--info-dot)]" }) : /* @__PURE__ */ e(k, { className: "w-6 h-6 text-[var(--info-dot)]" }) }),
                  /* @__PURE__ */ t("div", { className: "flex-1 min-w-0", children: [
                    /* @__PURE__ */ t("div", { className: "flex items-center gap-2", children: [
                      /* @__PURE__ */ e("h3", { className: "font-semibold text-[var(--text-primary)] truncate", children: i.name }),
                      /* @__PURE__ */ t("span", { className: "inline-flex items-center gap-1 px-2 py-0.5 text-xs font-medium rounded-full bg-[var(--color-accent-500)]/10 text-[var(--color-accent-600)]", children: [
                        /* @__PURE__ */ e(u, { className: "w-3 h-3 fill-current" }),
                        r("tenants.default", "Default")
                      ] })
                    ] }),
                    /* @__PURE__ */ e("p", { className: "text-sm text-[var(--text-secondary)] mt-1", children: r("myspace.tenants.defaultPreference.specificDescription", "Auto-connect to this workspace on login") })
                  ] })
                ] })
              }
            )
          ] })
        ] }),
        p.length > 0 && /* @__PURE__ */ t("section", { children: [
          /* @__PURE__ */ t("div", { className: "flex items-center gap-2 mb-4", children: [
            /* @__PURE__ */ e(o, { className: "w-5 h-5 text-[var(--info-dot)]" }),
            /* @__PURE__ */ e("h2", { className: "text-lg font-semibold text-[var(--text-primary)]", children: r("myspace.tenants.b2b.title", "Professional Workspaces") }),
            /* @__PURE__ */ e("span", { className: "px-2 py-0.5 text-xs font-medium rounded-full bg-[var(--info-bg)] text-[var(--info-text)]", children: "B2B" })
          ] }),
          /* @__PURE__ */ e("div", { className: "grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-4", children: p.map((a) => /* @__PURE__ */ e(
            v,
            {
              tenant: a,
              isSelected: d?.id === a.id,
              onSetDefault: () => x(a.id),
              onEnter: () => f(a.id),
              onManage: a.isOwner ? () => w(a.id) : void 0
            },
            a.id
          )) })
        ] }),
        P && /* @__PURE__ */ t("section", { children: [
          /* @__PURE__ */ t("div", { className: "flex items-center gap-2 mb-4", children: [
            /* @__PURE__ */ e(k, { className: "w-5 h-5 text-[var(--info-dot)]" }),
            /* @__PURE__ */ e("h2", { className: "text-lg font-semibold text-[var(--text-primary)]", children: r("myspace.tenants.personal.title", "Personal Workspace") }),
            /* @__PURE__ */ e("span", { className: "px-2 py-0.5 text-xs font-medium rounded-full bg-[var(--info-bg)] text-[var(--info-text)]", children: "B2C" })
          ] }),
          s ? /* @__PURE__ */ e("div", { className: "max-w-md", children: /* @__PURE__ */ e(
            v,
            {
              tenant: s,
              isSelected: d?.id === s.id,
              onSetDefault: () => x(s.id),
              onEnter: () => f(s.id)
            }
          ) }) : /* @__PURE__ */ t(
            "div",
            {
              className: "flex items-start gap-3 p-4 bg-[var(--warning-bg)] border border-[var(--warning-border)] rounded-[var(--radius-card)]",
              children: [
                /* @__PURE__ */ e(W, { className: "w-5 h-5 text-[var(--warning-text)] flex-shrink-0 mt-0.5" }),
                /* @__PURE__ */ t("div", { children: [
                  /* @__PURE__ */ e("p", { className: "font-medium text-[var(--warning-text)]", children: r("myspace.tenants.personal.noWorkspace", "No personal workspace") }),
                  /* @__PURE__ */ e("p", { className: "text-sm text-[var(--warning-text)] opacity-80 mt-1", children: r(
                    "myspace.tenants.personal.noWorkspaceDescription",
                    "Your personal workspace will be created automatically when you first access the platform."
                  ) })
                ] })
              ]
            }
          )
        ] }),
        h.length > 0 && /* @__PURE__ */ t("section", { children: [
          /* @__PURE__ */ t("div", { className: "flex items-center gap-2 mb-4", children: [
            /* @__PURE__ */ e(j, { className: "w-5 h-5 text-[var(--text-secondary)]" }),
            /* @__PURE__ */ e("h2", { className: "text-lg font-semibold text-[var(--text-primary)]", children: r("myspace.tenants.other.title", "Other Workspaces") })
          ] }),
          /* @__PURE__ */ e("div", { className: "grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-4", children: h.map((a) => /* @__PURE__ */ e(
            v,
            {
              tenant: a,
              isSelected: d?.id === a.id,
              onSetDefault: () => x(a.id),
              onEnter: () => f(a.id),
              onManage: a.isOwner ? () => w(a.id) : void 0
            },
            a.id
          )) })
        ] }),
        p.length === 0 && !s && h.length === 0 && /* @__PURE__ */ t(
          "div",
          {
            className: "text-center py-12 px-6 bg-[var(--bg-card)] border border-[var(--border-color)] rounded-[var(--radius-card)]",
            children: [
              /* @__PURE__ */ e(o, { className: "w-12 h-12 text-[var(--text-tertiary)] mx-auto mb-4" }),
              /* @__PURE__ */ e("h3", { className: "text-lg font-medium text-[var(--text-primary)] mb-2", children: r("myspace.tenants.empty.title", "No workspaces available") }),
              /* @__PURE__ */ e("p", { className: "text-[var(--text-secondary)] max-w-md mx-auto", children: r(
                "myspace.tenants.empty.description",
                "You don't have access to any workspace yet. Contact your administrator to get access."
              ) })
            ]
          }
        )
      ]
    }
  );
}
export {
  q as MyTenantsPage,
  q as default
};
//# sourceMappingURL=MyTenantsPage-cVwUtXjL.js.map
