"use client";
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { useMemo } from "react";
import { cn } from "@stigmer/theme";
import { getUserMessage } from "@stigmer/sdk";
import { serializeMcpServerInputYaml } from "../../library/serialize-resource-yaml.js";
/**
 * Wizard step 3: Review and create.
 *
 * Shows a summary card with key configuration details and a full
 * YAML preview of the MCP server that will be created. The "Create"
 * action is in the WizardNav footer, not in this component.
 */
export function ReviewStep({ org, data, isCreating, error, }) {
    const mcpServerInput = useMemo(() => buildMcpServerInput(org, data), [org, data]);
    const yamlPreview = useMemo(() => serializeMcpServerInputYaml(mcpServerInput), [mcpServerInput]);
    const transportSummary = data.transportType === "http"
        ? data.httpUrl || "(no URL)"
        : `${data.stdioCommand}${data.stdioArgs ? ` ${data.stdioArgs}` : ""}` || "(no command)";
    return (_jsxs("div", { className: "flex flex-col gap-6", children: [_jsxs("div", { children: [_jsx("h2", { className: "text-lg font-semibold text-foreground", children: "Review & Create" }), _jsx("p", { className: "mt-1 text-sm text-muted-foreground", children: "Review the MCP server configuration below, then create it." })] }), _jsx("div", { className: "rounded-lg border border-border p-4", children: _jsxs("dl", { className: "grid gap-x-6 gap-y-3 text-sm sm:grid-cols-2", children: [_jsx(SummaryItem, { label: "Name", value: data.name }), _jsx(SummaryItem, { label: "Slug", value: data.slug, mono: true }), _jsx(SummaryItem, { label: "Organization", value: org, mono: true }), _jsx(SummaryItem, { label: "Visibility", value: data.visibility === "public" ? "Public" : "Private" }), _jsx(SummaryItem, { label: "Transport", value: data.transportType === "http" ? "HTTP" : "Stdio" }), _jsx(SummaryItem, { label: data.transportType === "http" ? "URL" : "Command", value: transportSummary, mono: true }), data.description && (_jsx(SummaryItem, { label: "Description", value: data.description, className: "sm:col-span-2" })), data.env.length > 0 && (_jsx(SummaryItem, { label: "Env Variables", value: `${data.env.length} declared` })), data.authEnabled && data.authOAuthAppSlug && (_jsx(SummaryItem, { label: "OAuth", value: data.authOAuthAppOrg
                                ? `${data.authOAuthAppOrg}/${data.authOAuthAppSlug}`
                                : data.authOAuthAppSlug, mono: true }))] }) }), _jsxs("div", { className: "space-y-2", children: [_jsx("h3", { className: "text-xs font-medium uppercase tracking-wider text-muted-foreground", children: "YAML Preview" }), _jsx("div", { className: "max-h-80 overflow-auto rounded-lg border border-border bg-muted-faint", children: _jsx("pre", { className: "p-4 font-mono text-xs leading-relaxed text-foreground", children: yamlPreview }) })] }), error && (_jsxs("div", { className: "rounded-md border border-destructive bg-muted-faint px-4 py-3", role: "alert", children: [_jsx("p", { className: "text-sm font-medium text-destructive", children: "Failed to create MCP server" }), _jsx("p", { className: "mt-1 text-xs text-muted-foreground", children: getUserMessage(error) })] })), isCreating && (_jsx("p", { className: "text-sm text-muted-foreground", "aria-live": "polite", children: "Creating MCP server\u2026" }))] }));
}
// ---------------------------------------------------------------------------
// Summary item
// ---------------------------------------------------------------------------
function SummaryItem({ label, value, mono, className, }) {
    return (_jsxs("div", { className: className, children: [_jsx("dt", { className: "text-xs text-muted-foreground", children: label }), _jsx("dd", { className: cn("mt-0.5 text-sm text-foreground", mono && "font-mono"), children: value })] }));
}
// ---------------------------------------------------------------------------
// buildMcpServerInput
// ---------------------------------------------------------------------------
/**
 * Transforms wizard data into the SDK `McpServerInput` shape for submission.
 * Exported for reuse by `McpServerCreationWizard`.
 */
export function buildMcpServerInput(org, data) {
    const input = {
        name: data.name.trim(),
        org,
        ...(data.slug && { slug: data.slug }),
        ...(data.description && { description: data.description.trim() }),
        ...(data.iconUrl && { iconUrl: data.iconUrl.trim() }),
    };
    if (data.transportType === "http" && data.httpUrl) {
        const http = {
            url: data.httpUrl.trim(),
        };
        const headers = buildHeadersMap(data.httpHeaders);
        if (headers) {
            http.headers = headers;
        }
        if (data.httpTimeoutSeconds > 0) {
            http.timeoutSeconds = data.httpTimeoutSeconds;
        }
        input.http = http;
    }
    if (data.transportType === "stdio" && data.stdioCommand) {
        const stdio = {
            command: data.stdioCommand.trim(),
        };
        const args = parseSpaceSeparated(data.stdioArgs);
        if (args.length > 0) {
            stdio.args = args;
        }
        if (data.stdioWorkingDir.trim()) {
            stdio.workingDir = data.stdioWorkingDir.trim();
        }
        input.stdio = stdio;
    }
    if (data.env.length > 0) {
        const env = {};
        for (const entry of data.env) {
            if (!entry.key)
                continue;
            env[entry.key] = {
                ...(entry.isSecret && { isSecret: true }),
                ...(entry.description && { description: entry.description }),
                ...(entry.optional && { optional: true }),
            };
        }
        if (Object.keys(env).length > 0) {
            input.env = env;
        }
    }
    if (data.authEnabled) {
        const auth = {};
        if (data.authOAuthAppSlug) {
            auth.oauthAppRef = {
                org: data.authOAuthAppOrg,
                slug: data.authOAuthAppSlug,
            };
        }
        if (data.authTargetEnvVar) {
            auth.targetEnvVar = data.authTargetEnvVar;
        }
        if (data.authTokenLifetimeHint) {
            auth.tokenLifetimeHint = data.authTokenLifetimeHint;
        }
        const scopes = parseCommaSeparated(data.authScopeHints);
        if (scopes.length > 0) {
            auth.scopeHints = scopes;
        }
        if (data.authDiscoveryUrl) {
            auth.discoveryUrl = data.authDiscoveryUrl;
        }
        if (Object.keys(auth).length > 0) {
            input.auth = auth;
        }
    }
    return input;
}
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
function buildHeadersMap(entries) {
    const map = {};
    for (const entry of entries) {
        if (entry.key.trim()) {
            map[entry.key.trim()] = entry.value;
        }
    }
    return Object.keys(map).length > 0 ? map : undefined;
}
function parseSpaceSeparated(input) {
    return input
        .trim()
        .split(/\s+/)
        .filter(Boolean);
}
function parseCommaSeparated(input) {
    return input
        .split(",")
        .map((s) => s.trim())
        .filter(Boolean);
}
//# sourceMappingURL=ReviewStep.js.map