"use client";
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
import { useCallback, useRef, useState } from "react";
import { ApiKeyListPanel } from "../api-key/ApiKeyListPanel";
import { CreateApiKeyForm } from "../api-key/CreateApiKeyForm";
import { ApiKeyCreatedAlert } from "../api-key/ApiKeyCreatedAlert";
import { useResourceAvailable, ApiResourceKind } from "../deployment-mode";
import { CloudFeatureNotice } from "../internal/CloudFeatureNotice";
import { useActiveOrgSlug } from "../organization/OrgProvider";
/** Settings section for listing and creating organization API keys. */
export function ApiKeysSection() {
    const org = useActiveOrgSlug();
    const apiKeysAvailable = useResourceAvailable(ApiResourceKind.api_key);
    const [flow, setFlow] = useState({ phase: "idle" });
    const listRefetchRef = useRef(null);
    const handleRefetchRef = useCallback((refetch) => {
        listRefetchRef.current = refetch;
    }, []);
    const handleCreated = useCallback((apiKey) => {
        const rawKey = apiKey.spec?.keyHash ?? "";
        const keyName = apiKey.metadata?.name ?? "API key";
        setFlow({ phase: "reveal", rawKey, keyName });
        listRefetchRef.current?.();
    }, []);
    const handleDismissReveal = useCallback(() => {
        setFlow({ phase: "idle" });
    }, []);
    return (_jsxs("section", { "aria-labelledby": "api-keys-heading", children: [_jsxs("div", { className: "mb-3 flex items-center justify-between", children: [_jsx("h2", { id: "api-keys-heading", className: "text-foreground text-sm font-semibold", children: "API Keys" }), apiKeysAvailable && 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 API key" }))] }), _jsx("p", { className: "text-muted-foreground mb-4 text-xs", children: "API keys authenticate CLI sessions and programmatic access to the Stigmer API. Keys are scoped to your identity and work across all your organizations." }), !apiKeysAvailable ? (_jsx(CloudFeatureNotice, { children: "API keys are not available in local mode. When running locally, the CLI authenticates directly without API keys." })) : (_jsxs(_Fragment, { children: [flow.phase === "reveal" && (_jsx(ApiKeyCreatedAlert, { rawKey: flow.rawKey, keyName: flow.keyName, onDismiss: handleDismissReveal, className: "mb-4" })), flow.phase === "creating" && (_jsx("div", { className: "border-border bg-card mb-4 rounded-lg border p-4", children: _jsx(CreateApiKeyForm, { org: org, onCreated: handleCreated, onCancel: () => setFlow({ phase: "idle" }) }) })), _jsx(ApiKeyListPanel, { onRefetchRef: handleRefetchRef })] }))] }));
}
//# sourceMappingURL=ApiKeysSection.js.map