"use client";
'use strict';

var chunkRXZNACMI_js = require('./chunk-RXZNACMI.js');
var chunkAOUUZ52N_js = require('./chunk-AOUUZ52N.js');
var chunkHDCUWUNH_js = require('./chunk-HDCUWUNH.js');
var chunkI2NZGVBG_js = require('./chunk-I2NZGVBG.js');
var chunkYXN2K77G_js = require('./chunk-YXN2K77G.js');
var react = require('react');
var client = require('@datatechsolutions/windsock/client');
var jsxRuntime = require('react/jsx-runtime');

var DEFAULT_SECTIONS = ["account", "organization", "billing"];
function PlatformSettings({
  sections = DEFAULT_SECTIONS,
  defaultSection,
  adminClient,
  organizationId,
  billing
}) {
  const t = chunkYXN2K77G_js.useTranslations("windsock");
  const { status } = client.useAuth();
  const visibleSections = react.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] = react.useState(initialSection);
  const [organization, setOrganization] = react.useState(null);
  const [orgLoadError, setOrgLoadError] = react.useState(null);
  react.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__ */ jsxRuntime.jsx("div", { className: "flex items-center justify-center py-12", children: /* @__PURE__ */ jsxRuntime.jsx(chunkI2NZGVBG_js.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__ */ jsxRuntime.jsxs("div", { className: "space-y-6", children: [
    visibleSections.length > 1 && /* @__PURE__ */ jsxRuntime.jsx(
      chunkI2NZGVBG_js.SegmentedControl,
      {
        segments,
        value: activeSection,
        onChange: (value) => setActiveSection(value),
        fullWidth: true
      }
    ),
    activeSection === "account" && /* @__PURE__ */ jsxRuntime.jsx(chunkAOUUZ52N_js.UserProfile, {}),
    activeSection === "organization" && adminClient && organizationId && (organization ? /* @__PURE__ */ jsxRuntime.jsx(
      chunkRXZNACMI_js.AdminOrganizationDetail,
      {
        client: adminClient,
        organization
      }
    ) : orgLoadError ? /* @__PURE__ */ jsxRuntime.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__ */ jsxRuntime.jsx("div", { className: "flex items-center justify-center py-12", children: /* @__PURE__ */ jsxRuntime.jsx(chunkI2NZGVBG_js.InlineSpinner, {}) })),
    activeSection === "billing" && /* @__PURE__ */ jsxRuntime.jsx(chunkHDCUWUNH_js.BillingPanel, { ...billing })
  ] });
}

exports.PlatformSettings = PlatformSettings;
//# sourceMappingURL=chunk-W5OEBO6E.js.map
//# sourceMappingURL=chunk-W5OEBO6E.js.map