"use client";
import { AdminOrganizationDetail } from './chunk-2E7HYTS7.mjs';
import { UserProfile } from './chunk-WS3IJFPW.mjs';
import { BillingPanel } from './chunk-PD3JLDA5.mjs';
import { useAuth, InlineSpinner, SegmentedControl } from './chunk-ZQRUAXGP.mjs';
import { useTranslations } from './chunk-7VJ7CMMT.mjs';
import { useMemo, useState, useEffect } from 'react';
import { jsx, jsxs } from 'react/jsx-runtime';

var DEFAULT_SECTIONS = ["account", "organization", "billing"];
function PlatformSettings({
  sections = DEFAULT_SECTIONS,
  defaultSection,
  adminClient,
  organizationId,
  billing
}) {
  const t = useTranslations("windsock");
  const { status } = useAuth();
  const visibleSections = useMemo(() => {
    return sections.filter((section) => {
      if (section === "organization") {
        return Boolean(adminClient && organizationId);
      }
      return true;
    });
  }, [sections, adminClient, organizationId]);
  const initialSection = defaultSection && visibleSections.includes(defaultSection) ? defaultSection : visibleSections[0] ?? "account";
  const [activeSection, setActiveSection] = useState(initialSection);
  const [organization, setOrganization] = useState(null);
  const [orgLoadError, setOrgLoadError] = useState(null);
  useEffect(() => {
    if (activeSection !== "organization") return;
    if (!adminClient || !organizationId) return;
    if (organization?.id === organizationId) return;
    let cancelled = false;
    setOrgLoadError(null);
    void adminClient.listOrganizations().then((orgs) => {
      if (cancelled) return;
      const match = orgs.find((org) => org.id === organizationId);
      if (match) {
        setOrganization(match);
      } else {
        setOrgLoadError("Organization not found");
      }
    }).catch((loadError) => {
      if (cancelled) return;
      setOrgLoadError(loadError instanceof Error ? loadError.message : String(loadError));
    });
    return () => {
      cancelled = true;
    };
  }, [activeSection, adminClient, organizationId, organization?.id]);
  if (status === "loading") {
    return /* @__PURE__ */ jsx("div", { className: "flex items-center justify-center py-12", children: /* @__PURE__ */ jsx(InlineSpinner, {}) });
  }
  if (status !== "authenticated") {
    return null;
  }
  if (visibleSections.length === 0) {
    return null;
  }
  const segments = visibleSections.map((section) => ({
    value: section,
    label: t(`platformSettings.sections.${section}`)
  }));
  return /* @__PURE__ */ jsxs("div", { className: "space-y-6", children: [
    visibleSections.length > 1 && /* @__PURE__ */ jsx(
      SegmentedControl,
      {
        segments,
        value: activeSection,
        onChange: (value) => setActiveSection(value),
        fullWidth: true
      }
    ),
    activeSection === "account" && /* @__PURE__ */ jsx(UserProfile, {}),
    activeSection === "organization" && adminClient && organizationId && (organization ? /* @__PURE__ */ jsx(
      AdminOrganizationDetail,
      {
        client: adminClient,
        organization
      }
    ) : orgLoadError ? /* @__PURE__ */ jsx(
      "div",
      {
        role: "alert",
        className: "rounded-xl border border-red-300/40 bg-red-50/80 px-4 py-3 text-sm text-red-700 dark:border-red-500/30 dark:bg-red-500/10 dark:text-red-300",
        children: orgLoadError
      }
    ) : /* @__PURE__ */ jsx("div", { className: "flex items-center justify-center py-12", children: /* @__PURE__ */ jsx(InlineSpinner, {}) })),
    activeSection === "billing" && /* @__PURE__ */ jsx(BillingPanel, { ...billing })
  ] });
}

export { PlatformSettings };
//# sourceMappingURL=chunk-OEZ7GAJY.mjs.map
//# sourceMappingURL=chunk-OEZ7GAJY.mjs.map