"use client";
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
import { memo, useCallback, useState } from "react";
import { cn } from "@stigmer/theme";
import { useStigmer } from "../hooks.js";
import { formatBytes } from "./format-utils.js";
/**
 * Sidebar panel listing artifacts produced by the workflow execution.
 * Each artifact shows its display name, content type, size, and a
 * download button that fetches a pre-signed URL via `getDownloadUrl`.
 */
export const WorkflowExecutionArtifactPanel = memo(function WorkflowExecutionArtifactPanel({ artifacts, className, }) {
    if (artifacts.length === 0)
        return null;
    return (_jsxs("div", { className: cn("flex flex-col", className), children: [_jsx("div", { className: "px-3 py-2", children: _jsx("h3", { className: "text-xs font-semibold uppercase tracking-wider text-muted-foreground", children: "Artifacts" }) }), _jsx("div", { className: "flex flex-col", children: artifacts.map((artifact) => (_jsx(ArtifactRow, { artifact: artifact }, artifact.metadata?.id ?? artifact.metadata?.name))) })] }));
});
function ArtifactRow({ artifact }) {
    const stigmer = useStigmer();
    const [isDownloading, setIsDownloading] = useState(false);
    const id = artifact.metadata?.id ?? "";
    const displayName = artifact.spec?.displayName ?? artifact.metadata?.name ?? "Unnamed";
    const contentType = artifact.spec?.contentType ?? "";
    const sizeBytes = artifact.status?.sizeBytes ?? BigInt(0);
    const handleDownload = useCallback(async () => {
        if (!id || isDownloading)
            return;
        setIsDownloading(true);
        try {
            const downloadUrl = await stigmer.artifact.getDownloadUrl(id);
            if (downloadUrl.url) {
                window.open(downloadUrl.url, "_blank", "noopener,noreferrer");
            }
        }
        catch {
            // Graceful degradation — download not available
        }
        finally {
            setIsDownloading(false);
        }
    }, [id, stigmer, isDownloading]);
    return (_jsxs("div", { className: "flex items-center gap-2 px-3 py-1.5", children: [_jsx("span", { className: "shrink-0 text-xs text-muted-foreground", "aria-hidden": "true", children: _jsx(FileIcon, {}) }), _jsxs("div", { className: "min-w-0 flex-1", children: [_jsx("p", { className: "truncate text-xs text-foreground", children: displayName }), _jsxs("p", { className: "text-[10px] text-muted-foreground", children: [contentType, sizeBytes > BigInt(0) && _jsxs(_Fragment, { children: [" \u00B7 ", formatBytes(sizeBytes)] })] })] }), _jsx("button", { type: "button", onClick: handleDownload, disabled: isDownloading || !id, className: cn("shrink-0 rounded px-1.5 py-0.5 text-[10px] font-medium", "border border-border text-muted-foreground", "hover:bg-muted hover:text-foreground", "disabled:pointer-events-none disabled:opacity-50"), "aria-label": `Download ${displayName}`, children: isDownloading ? "…" : "↓" })] }));
}
function FileIcon() {
    return (_jsxs("svg", { width: "12", height: "12", viewBox: "0 0 12 12", fill: "none", stroke: "currentColor", strokeWidth: "1.5", strokeLinecap: "round", children: [_jsx("path", { d: "M7 1H3a1 1 0 00-1 1v8a1 1 0 001 1h6a1 1 0 001-1V4L7 1z" }), _jsx("path", { d: "M7 1v3h3" })] }));
}
//# sourceMappingURL=WorkflowExecutionArtifactPanel.js.map