"use client";
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { useCallback, useRef, useState } from "react";
import { IdentityProviderListPanel } from "../identity-provider/IdentityProviderListPanel";
import { IdentityProviderWizard } from "../identity-provider/IdentityProviderWizard";
import { IdentityProviderDetailPanel } from "../identity-provider/IdentityProviderDetailPanel";
import { useResourceAvailable, ApiResourceKind } from "../deployment-mode";
import { CloudFeatureNotice } from "../internal/CloudFeatureNotice";
import { useOrg } from "../organization/OrgProvider";
/** Settings section for configuring OIDC identity providers. */
export function IdentityProvidersSection({ ssoLoginBaseUrl, } = {}) {
    const { activeOrg } = useOrg();
    const idpAvailable = useResourceAvailable(ApiResourceKind.identity_provider);
    const orgSlug = activeOrg?.metadata?.slug ?? "";
    const [flow, setFlow] = useState({ phase: "idle" });
    const listRefetchRef = useRef(null);
    const handleRefetchRef = useCallback((refetch) => {
        listRefetchRef.current = refetch;
    }, []);
    const handleCreated = useCallback(() => {
        listRefetchRef.current?.();
        setFlow({ phase: "idle" });
    }, []);
    const handleUpdated = useCallback(() => {
        listRefetchRef.current?.();
        setFlow({ phase: "idle" });
    }, []);
    const baseUrl = ssoLoginBaseUrl ??
        (typeof window !== "undefined" ? window.location.origin : "");
    return (_jsxs("section", { "aria-labelledby": "identity-providers-heading", children: [_jsxs("div", { className: "mb-3 flex items-center justify-between", children: [_jsx("h2", { id: "identity-providers-heading", className: "text-foreground text-sm font-semibold", children: "Identity Providers" }), idpAvailable && orgSlug && flow.phase === "idle" && (_jsx("button", { type: "button", onClick: () => setFlow({ phase: "creating" }), className: "text-primary hover:text-foreground text-xs font-medium transition-colors", children: "+ New identity provider" }))] }), _jsx("p", { className: "text-muted-foreground mb-4 text-xs", children: "Identity providers define external OIDC trust relationships for federated authentication. Configure providers for platform-managed organizations or self-managed SSO." }), !idpAvailable ? (_jsx(CloudFeatureNotice, { children: "Identity providers are not available in local mode. Federated authentication requires Stigmer Cloud." })) : !orgSlug ? (_jsx("p", { className: "text-muted-foreground py-4 text-center text-xs", children: "Select an organization to manage identity providers." })) : flow.phase === "creating" ? (_jsx("div", { className: "border-border bg-card rounded-lg border p-4", children: _jsx(IdentityProviderWizard, { org: orgSlug, onCreated: handleCreated, onCancel: () => setFlow({ phase: "idle" }) }) })) : flow.phase === "editing" ? (_jsx("div", { className: "border-border bg-card rounded-lg border p-4", children: _jsx(IdentityProviderDetailPanel, { identityProvider: flow.identityProvider, ssoLoginUrl: flow.identityProvider.spec?.isSsoProvider
                        ? `${baseUrl}/login?org=${orgSlug}`
                        : undefined, onUpdated: handleUpdated, onBack: () => setFlow({ phase: "idle" }) }) })) : (_jsx(IdentityProviderListPanel, { org: orgSlug, onEdit: (idp) => setFlow({ phase: "editing", identityProvider: idp }), onRefetchRef: handleRefetchRef }))] }));
}
//# sourceMappingURL=IdentityProvidersSection.js.map