"use client";
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { memo, useCallback, useState } from "react";
import { cn } from "@stigmer/theme";
import { useArtifactContent } from "./useArtifactContent";
import { ArtifactContentRenderer } from "./ArtifactContentRenderer";
import { formatArtifactSize } from "./artifact-utils";
/** Plans above this size are not inlined for preview — download instead. */
const MAX_PREVIEW_BYTES = 512 * 1024;
/**
 * Reviewable card shown after a completed Plan-mode execution when the agent
 * published a `plan.md` artifact.
 *
 * Unlike {@link PlanCompletionCard} (a bare Implement CTA), this card makes the
 * plan a first-class, durable object: expand to review the rendered plan, copy
 * it, download the `plan.md` file, or proceed to implement. The plan content is
 * the single source of truth (the published artifact), fetched on demand via
 * {@link useArtifactContent} rather than duplicated into component state.
 *
 * All visual properties flow through `--stgm-*` tokens; the component is
 * self-contained and embeddable.
 */
export const PlanArtifactCard = memo(function PlanArtifactCard({ executionId, artifact, onImplement, disabled, className, }) {
    const [expanded, setExpanded] = useState(false);
    const [copied, setCopied] = useState(false);
    // Plans are small markdown; fetch eagerly (within the size cap) so both the
    // preview and Copy have content ready. contentHash invalidates the cache when
    // the plan is re-published in the same execution.
    const fetchable = Number(artifact.sizeBytes) < MAX_PREVIEW_BYTES;
    const { content, contentType, isTruncated, isLoading } = useArtifactContent(fetchable ? executionId : null, fetchable ? artifact.storageKey : null, undefined, artifact.contentHash || undefined);
    const handleCopy = useCallback(async () => {
        if (!content)
            return;
        try {
            await navigator.clipboard.writeText(content);
            setCopied(true);
            setTimeout(() => setCopied(false), 1500);
        }
        catch {
            // Clipboard can be unavailable (insecure context / denied permission).
            // Silently no-op: Download remains available as the durable fallback.
        }
    }, [content]);
    return (_jsxs("div", { role: "region", "aria-label": "Plan ready to review", className: cn("mx-4 overflow-hidden rounded-md border border-border-muted bg-muted-faint", className), children: [_jsxs("div", { className: "flex items-center gap-3 px-3 py-2.5", children: [_jsx(PlanIcon, {}), _jsxs("div", { className: "min-w-0 flex-1", children: [_jsx("div", { className: "text-xs font-medium text-foreground", children: "Plan ready to review" }), _jsxs("div", { className: "text-xs tabular-nums text-muted-foreground", children: [artifact.name, " \u00B7 ", formatArtifactSize(artifact.sizeBytes)] })] }), onImplement && (_jsxs("button", { type: "button", disabled: disabled, onClick: onImplement, className: cn("inline-flex items-center gap-1.5 rounded-md px-3 py-1.5", "text-xs font-medium transition-colors", "bg-primary text-primary-foreground hover:bg-primary-hover", "focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring", "disabled:pointer-events-none disabled:opacity-50"), children: [_jsx(ImplementIcon, {}), "Implement"] }))] }), _jsxs("div", { className: "flex flex-wrap items-center gap-x-4 gap-y-1 border-t border-border-muted px-3 py-1.5", children: [_jsxs("button", { type: "button", "aria-expanded": expanded, onClick: () => setExpanded((v) => !v), className: cn("inline-flex items-center gap-1 text-xs font-medium text-primary transition-colors hover:text-primary-muted", FOCUS_RING), children: [_jsx(ChevronIcon, { expanded: expanded }), expanded ? "Hide plan" : "Review plan"] }), _jsx("button", { type: "button", onClick: handleCopy, disabled: !content, className: cn("text-xs font-medium text-muted-foreground transition-colors hover:text-foreground", "disabled:pointer-events-none disabled:opacity-50", FOCUS_RING), children: copied ? "Copied" : "Copy" }), _jsxs("a", { href: artifact.downloadUrl, download: artifact.name, className: cn("inline-flex items-center gap-1 text-xs font-medium text-muted-foreground transition-colors hover:text-foreground", FOCUS_RING), children: [_jsx(DownloadIcon, {}), "Download"] })] }), expanded && (_jsx("div", { className: "max-h-96 overflow-auto border-t border-border-muted px-3 py-2", children: !fetchable ? (_jsx("p", { className: "text-xs text-muted-foreground", children: "This plan is large \u2014 use Download to view the full file." })) : isLoading ? (_jsx("div", { className: "h-4 w-40 animate-pulse rounded bg-muted", "aria-hidden": "true" })) : content ? (_jsx(ArtifactContentRenderer, { content: content, fileName: artifact.name, contentType: contentType, isTruncated: isTruncated })) : (_jsx("p", { className: "text-xs text-muted-foreground", children: "Plan content is unavailable. Use Download to retrieve the file." })) }))] }));
});
const FOCUS_RING = "focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:rounded-sm";
function PlanIcon() {
    return (_jsxs("svg", { width: "16", height: "16", viewBox: "0 0 16 16", fill: "none", stroke: "currentColor", strokeWidth: "1.5", strokeLinecap: "round", strokeLinejoin: "round", className: "shrink-0 text-muted-foreground-faint", "aria-hidden": "true", children: [_jsx("path", { d: "M3 4h10M3 8h7M3 12h8" }), _jsx("path", { d: "M12.5 10.5l1.5 1.5-1.5 1.5" })] }));
}
function ImplementIcon() {
    return (_jsx("svg", { width: "12", height: "12", viewBox: "0 0 12 12", fill: "none", stroke: "currentColor", strokeWidth: "1.5", strokeLinecap: "round", strokeLinejoin: "round", "aria-hidden": "true", children: _jsx("path", { d: "M2 6h8M7 3l3 3-3 3" }) }));
}
function DownloadIcon() {
    return (_jsxs("svg", { width: "10", height: "10", viewBox: "0 0 12 12", fill: "none", stroke: "currentColor", strokeWidth: "1.5", strokeLinecap: "round", strokeLinejoin: "round", className: "shrink-0", "aria-hidden": "true", children: [_jsx("path", { d: "M6 1.5V8.5" }), _jsx("path", { d: "M3 6L6 9L9 6" }), _jsx("path", { d: "M2 10.5H10" })] }));
}
function ChevronIcon({ expanded }) {
    return (_jsx("svg", { width: "10", height: "10", viewBox: "0 0 12 12", fill: "none", stroke: "currentColor", strokeWidth: "1.5", strokeLinecap: "round", strokeLinejoin: "round", className: cn("shrink-0 transition-transform", expanded && "rotate-90"), "aria-hidden": "true", children: _jsx("path", { d: "M4 2l4 4-4 4" }) }));
}
//# sourceMappingURL=PlanArtifactCard.js.map