"use client";
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { useCallback, useRef, useState } from "react";
import { PlatformClientListPanel } from "../platform-client/PlatformClientListPanel";
import { CreatePlatformClientForm } from "../platform-client/CreatePlatformClientForm";
import { PlatformClientDetailPanel } from "../platform-client/PlatformClientDetailPanel";
import { PlatformClientSecretAlert } from "../platform-client/PlatformClientSecretAlert";
import { useResourceAvailable, ApiResourceKind } from "../deployment-mode";
import { CloudFeatureNotice } from "../internal/CloudFeatureNotice";
import { useActiveOrgSlug } from "../organization/OrgProvider";
/** Settings section for creating and maintaining platform clients. */
export function PlatformClientsSection() {
    const org = useActiveOrgSlug();
    const pcAvailable = useResourceAvailable(ApiResourceKind.platform_client);
    const [flow, setFlow] = useState({ phase: "idle" });
    const listRefetchRef = useRef(null);
    const handleRefetchRef = useCallback((refetch) => {
        listRefetchRef.current = refetch;
    }, []);
    const handleCreated = useCallback((response) => {
        listRefetchRef.current?.();
        setFlow({
            phase: "revealing",
            clientId: response.platformClient?.spec?.clientId ?? "",
            clientSecret: response.clientSecret,
            context: "created",
        });
    }, []);
    const handleSecretRotated = useCallback((response) => {
        listRefetchRef.current?.();
        setFlow({
            phase: "revealing",
            clientId: response.platformClient?.spec?.clientId ?? "",
            clientSecret: response.clientSecret,
            context: "rotated",
        });
    }, []);
    const handleUpdated = useCallback(() => {
        listRefetchRef.current?.();
        setFlow({ phase: "idle" });
    }, []);
    const handleDeleted = useCallback(() => {
        listRefetchRef.current?.();
        setFlow({ phase: "idle" });
    }, []);
    return (_jsxs("section", { "aria-labelledby": "platform-clients-heading", children: [_jsxs("div", { className: "mb-3 flex items-center justify-between", children: [_jsx("h2", { id: "platform-clients-heading", className: "text-foreground text-sm font-semibold", children: "Platform Clients" }), pcAvailable && org && 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 platform client" }))] }), _jsx("p", { className: "text-muted-foreground mb-4 text-xs", children: "Platform clients let your backend mint Stigmer user tokens so you can embed Stigmer components in your application without requiring OIDC federation." }), !pcAvailable ? (_jsx(CloudFeatureNotice, { children: "Platform clients are not available in local mode. Token minting requires Stigmer Cloud." })) : flow.phase === "creating" ? (_jsx("div", { className: "border-border bg-card rounded-lg border p-4", children: _jsx(CreatePlatformClientForm, { org: org, onCreated: handleCreated, onCancel: () => setFlow({ phase: "idle" }) }) })) : flow.phase === "revealing" ? (_jsxs("div", { className: "space-y-4", children: [_jsx(PlatformClientSecretAlert, { clientId: flow.clientId, clientSecret: flow.clientSecret, context: flow.context, onDismiss: () => setFlow({ phase: "idle" }) }), _jsx(PlatformClientListPanel, { org: org, onEdit: (pc) => setFlow({ phase: "editing", platformClient: pc }), onRefetchRef: handleRefetchRef })] })) : flow.phase === "editing" ? (_jsx("div", { className: "border-border bg-card rounded-lg border p-4", children: _jsx(PlatformClientDetailPanel, { platformClient: flow.platformClient, onUpdated: handleUpdated, onSecretRotated: handleSecretRotated, onDeleted: handleDeleted, onBack: () => setFlow({ phase: "idle" }) }) })) : (_jsx(PlatformClientListPanel, { org: org, onEdit: (pc) => setFlow({ phase: "editing", platformClient: pc }), onRefetchRef: handleRefetchRef }))] }));
}
//# sourceMappingURL=PlatformClientsSection.js.map