"use client";
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { memo, useState } from "react";
import { cn } from "@stigmer/theme";
import { ArtifactPreviewModal } from "./ArtifactPreviewModal.js";
import { formatArtifactSize } from "./artifact-utils.js";
import { useArtifactCopy } from "./useArtifactCopy.js";
import { useArtifactDownload } from "./useArtifactDownload.js";
import { useBuildFromPlanHotkey } from "./use-build-from-plan-hotkey.js";
/**
 * The compact plan card — a completed Plan turn's sole representation in the
 * thread. The plan document itself lives in the session panel's plan tab
 * (`PlanEditor`), Cursor-style: chat carries a recognizable, actionable card;
 * the panel carries the document.
 *
 * Anatomy: a document-style identity block (plan icon, the plan's title, a
 * `<name> · size` meta line) with the actions trailing — open, copy, and
 * download as icon-only secondaries (labelled via `aria-label`/`title`) and
 * one prominent, themeable primary, **Build** — so the next step is
 * unmistakable.
 *
 * Action-label rule (shared with `PlanStreamingCard`, which keeps its
 * labelled "Open plan"): a lone secondary keeps its text label; a row of
 * secondaries competing with a primary collapses to icons, so the primary
 * carries the card's one visible verb.
 *
 * Only the LATEST plan's card carries `onImplement`; a superseded plan's card
 * is review-only (its `onOpenPlan` opens the document read-only), so a stale
 * plan can never be implemented by accident.
 *
 * All visual properties flow through `--stgm-*` tokens; the component is
 * self-contained and embeddable.
 */
export const PlanArtifactCard = memo(function PlanArtifactCard({ executionId, artifact, title, org, onImplement, onOpenPlan, disabled, buildPending, className, }) {
    const [previewOpen, setPreviewOpen] = useState(false);
    const handleKeyDown = useBuildFromPlanHotkey(onImplement, disabled);
    const { copy, isCopying, copied } = useArtifactCopy(executionId);
    const { download, isDownloading } = useArtifactDownload(executionId);
    return (_jsxs("div", { role: "region", "aria-label": "Plan", onKeyDown: handleKeyDown, className: cn("mx-4 flex flex-wrap items-center gap-x-3 gap-y-2 rounded-lg", "border border-border-muted bg-card px-3 py-2.5", className), children: [_jsx(PlanIcon, {}), _jsxs("div", { className: "min-w-0 flex-1 basis-48", children: [_jsx("div", { className: "truncate text-sm font-medium text-foreground", children: title ?? "Plan" }), _jsxs("div", { className: "text-[0.65rem] text-muted-foreground-faint", children: [artifact.name, _jsx("span", { "aria-hidden": "true", children: " \u00B7 " }), _jsx("span", { className: "tabular-nums", children: formatArtifactSize(artifact.sizeBytes) })] })] }), _jsxs("div", { className: "ml-auto flex flex-wrap items-center gap-x-1.5 gap-y-1.5", children: [onOpenPlan ? (_jsx(IconAction, { label: "Open plan", onClick: onOpenPlan, icon: _jsx(EyeIcon, {}) })) : (org && (_jsx(IconAction, { label: "Open plan", onClick: () => setPreviewOpen(true), icon: _jsx(EyeIcon, {}) }))), _jsx(IconAction, { label: copied ? "Copied" : isCopying ? "Copying…" : "Copy plan", onClick: () => copy(artifact.storageKey), disabled: isCopying, icon: copied ? _jsx(CheckIcon, {}) : _jsx(CopyIcon, {}) }), _jsx(IconAction, { label: isDownloading ? "Preparing…" : `Download ${artifact.name}`, onClick: () => download(artifact.storageKey, artifact.name), disabled: isDownloading, icon: _jsx(DownloadIcon, {}) }), onImplement && (_jsxs("button", { type: "button", disabled: disabled, onClick: onImplement, className: cn("ml-1.5 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, {}), buildPending ? "Starting build…" : "Build"] }))] }), _jsx("div", { role: "status", "aria-live": "polite", "aria-atomic": "true", className: "sr-only", children: copied && "Plan copied to clipboard" }), org && previewOpen && (_jsx(ArtifactPreviewModal, { artifact: artifact, executionId: executionId, org: org, isTerminal: true, open: true, onClose: () => setPreviewOpen(false), onImplement: onImplement }))] }));
});
const FOCUS_RING = "focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:rounded-sm";
/**
 * An icon-only secondary action. The accessible name doubles as the hover
 * tooltip (native `title` — the codebase's tooltip pattern), and the padded
 * hit area keeps the target comfortable despite the compact glyph.
 */
function IconAction({ label, onClick, icon, disabled, }) {
    return (_jsx("button", { type: "button", "aria-label": label, title: label, onClick: onClick, disabled: disabled, className: cn("inline-flex items-center rounded-md p-1.5 text-muted-foreground", "transition-colors hover:bg-muted hover:text-foreground", "disabled:pointer-events-none disabled:opacity-50", FOCUS_RING), children: icon }));
}
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: "13", height: "13", 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 CopyIcon() {
    return (_jsxs("svg", { width: "13", height: "13", viewBox: "0 0 12 12", fill: "none", stroke: "currentColor", strokeWidth: "1.2", strokeLinecap: "round", strokeLinejoin: "round", className: "shrink-0", "aria-hidden": "true", children: [_jsx("rect", { x: "4", y: "4", width: "6.5", height: "6.5", rx: "1" }), _jsx("path", { d: "M8 4V2.5C8 1.95 7.55 1.5 7 1.5H2.5C1.95 1.5 1.5 1.95 1.5 2.5V7C1.5 7.55 1.95 8 2.5 8H4" })] }));
}
/** A check glyph — transient confirmation that the copy succeeded. */
function CheckIcon() {
    return (_jsx("svg", { width: "13", height: "13", viewBox: "0 0 12 12", fill: "none", stroke: "currentColor", strokeWidth: "1.5", strokeLinecap: "round", strokeLinejoin: "round", className: "shrink-0 text-success", "aria-hidden": "true", children: _jsx("path", { d: "M2 6.5L4.5 9L10 3" }) }));
}
/** An eye glyph — "view/open the plan to read it". */
function EyeIcon() {
    return (_jsxs("svg", { width: "13", height: "13", viewBox: "0 0 16 16", fill: "none", stroke: "currentColor", strokeWidth: "1.5", strokeLinecap: "round", strokeLinejoin: "round", className: "shrink-0", "aria-hidden": "true", children: [_jsx("path", { d: "M1.5 8S4 3.5 8 3.5s6.5 4.5 6.5 4.5-2.5 4.5-6.5 4.5S1.5 8 1.5 8Z" }), _jsx("circle", { cx: "8", cy: "8", r: "1.75" })] }));
}
//# sourceMappingURL=PlanArtifactCard.js.map