"use client";
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { useMemo } from "react";
import { StigmerError } from "@stigmer/sdk";
import { cn } from "@stigmer/theme";
const MISSING_ENV_VAR_PATTERN = /MCP server '([^']+)' requires environment variable '([^']+)'/g;
/**
 * Parse a `FAILED_PRECONDITION` error message for missing environment
 * variable declarations. Returns an empty array when the pattern does
 * not match.
 */
function parseMissingVariables(message) {
    const results = [];
    let match;
    while ((match = MISSING_ENV_VAR_PATTERN.exec(message)) !== null) {
        results.push({ serverName: match[1], variableName: match[2] });
    }
    MISSING_ENV_VAR_PATTERN.lastIndex = 0;
    return results;
}
function isFailedPreconditionError(error) {
    return error instanceof StigmerError && error.code === "failed-precondition";
}
/**
 * Contextual recovery guidance for secret-flow errors.
 *
 * Detects `FAILED_PRECONDITION` errors from execution creation that
 * indicate missing MCP server environment variables, and renders
 * actionable guidance alongside the technical error message.
 *
 * When the error does not match a secret-flow pattern, renders nothing.
 * This makes the component composable: try `SecretFlowErrorGuide`
 * first, fall through to {@link ErrorMessage} for everything else.
 *
 * No Console-specific dependencies (no routing, no app-shell imports).
 * Platform builders embedding Stigmer components get the same guidance.
 *
 * @example
 * ```tsx
 * {error && (
 *   <SecretFlowErrorGuide error={error} />
 *   ?? <ErrorMessage error={error} />
 * )}
 *
 * // Or in a single expression:
 * <SecretFlowErrorGuide error={error} />
 * {error && !isSecretFlowError(error) && <ErrorMessage error={error} />}
 * ```
 */
export function SecretFlowErrorGuide({ error, className, }) {
    const parsed = useMemo(() => {
        if (!error || !isFailedPreconditionError(error))
            return null;
        const variables = parseMissingVariables(error.message);
        if (variables.length === 0)
            return null;
        return variables;
    }, [error]);
    if (!parsed)
        return null;
    const grouped = useMemo(() => {
        const map = new Map();
        for (const { serverName, variableName } of parsed) {
            const list = map.get(serverName) ?? [];
            list.push(variableName);
            map.set(serverName, list);
        }
        return map;
    }, [parsed]);
    return (_jsx("div", { role: "alert", className: cn("rounded-lg border border-amber-500/30 bg-amber-500/5 p-4", className), children: _jsxs("div", { className: "flex items-start gap-3", children: [_jsx(KeyIcon, { className: "mt-0.5 size-4 shrink-0 text-amber-600 dark:text-amber-400" }), _jsxs("div", { className: "min-w-0 flex-1 space-y-2", children: [_jsx("p", { className: "text-sm font-medium text-amber-800 dark:text-amber-200", children: "Missing environment variables" }), _jsx("div", { className: "space-y-1.5", children: Array.from(grouped).map(([server, vars]) => (_jsxs("div", { children: [_jsxs("p", { className: "text-xs text-amber-700 dark:text-amber-300", children: [_jsx("span", { className: "font-medium", children: server }), " requires:"] }), _jsx("ul", { className: "mt-0.5 space-y-0.5", children: vars.map((v) => (_jsx("li", { className: "text-xs font-mono text-amber-800/80 dark:text-amber-200/80 pl-3", children: v }, v))) })] }, server))) }), _jsxs("div", { className: "border-t border-amber-500/20 pt-2", children: [_jsx("p", { className: "text-xs text-amber-700/90 dark:text-amber-300/90", children: "You can provide these values in two ways:" }), _jsxs("ul", { className: "mt-1 space-y-0.5 text-xs text-amber-700/80 dark:text-amber-300/80", children: [_jsxs("li", { className: "flex items-start gap-1.5", children: [_jsx("span", { className: "mt-px shrink-0", children: "\u2022" }), _jsxs("span", { children: ["Add them to your", " ", _jsx("strong", { className: "font-medium", children: "personal environment" }), " ", "in Settings for automatic reuse across sessions."] })] }), _jsxs("li", { className: "flex items-start gap-1.5", children: [_jsx("span", { className: "mt-px shrink-0", children: "\u2022" }), _jsxs("span", { children: ["Provide them as", " ", _jsx("strong", { className: "font-medium", children: "session variables" }), " ", "when sending a message."] })] })] })] })] })] }) }));
}
/**
 * Check whether an error matches the secret-flow missing variable pattern.
 * Useful for conditional rendering alongside `ErrorMessage`.
 */
export function isSecretFlowError(error) {
    if (!error || !isFailedPreconditionError(error))
        return false;
    return parseMissingVariables(error.message).length > 0;
}
// ---------------------------------------------------------------------------
// Icons
// ---------------------------------------------------------------------------
function KeyIcon({ className }) {
    return (_jsxs("svg", { width: "16", height: "16", viewBox: "0 0 16 16", fill: "none", stroke: "currentColor", strokeWidth: "1.5", strokeLinecap: "round", strokeLinejoin: "round", className: className, "aria-hidden": "true", children: [_jsx("circle", { cx: "10.5", cy: "5.5", r: "3" }), _jsx("path", { d: "M8.5 7.5L3 13l1.5 1.5" }), _jsx("path", { d: "M5.5 11l1.5 1.5" })] }));
}
//# sourceMappingURL=SecretFlowErrorGuide.js.map