"use client";
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { useCallback, useMemo, useState } from "react";
import { cn } from "@stigmer/theme";
import { useSessionArtifacts, } from "../session/useSessionArtifacts";
import { ArtifactCard } from "./ArtifactCard";
import { ArtifactPreviewModal } from "./ArtifactPreviewModal";
/**
 * Right-sidebar widget that surfaces all artifacts produced during a
 * session as a unified, alphabetically-sorted file listing.
 *
 * Artifacts from multiple executions are aggregated and deduplicated
 * by `sandbox_path` (latest execution wins), presenting the user with
 * a file-explorer-like view of the conversation's output — no
 * execution/turn concepts are exposed.
 *
 * Composes {@link ArtifactCard} (summary + detection badges) with
 * {@link ArtifactPreviewModal} (full content review + Apply/Push CTA).
 * The card's "Preview" action opens the modal; the modal is the sole
 * location for Apply/Push actions (review-before-apply pattern).
 *
 * Returns `null` when the executions list is empty or no execution
 * has artifacts, matching the conditional-render pattern of
 * {@link ExecutionProgress} and {@link UsageWidget}.
 *
 * Renders without card chrome — each {@link ArtifactCard} provides its
 * own border and padding. The consumer controls the container styling
 * (or places the widget directly in a flex/grid layout).
 *
 * All visual properties flow through `--stgm-*` tokens.
 *
 * @example
 * ```tsx
 * const conv = useSessionConversation(sessionId, org);
 *
 * <ArtifactsWidget
 *   executions={[
 *     ...conv.completedExecutions,
 *     ...(conv.activeStreamExecution ? [conv.activeStreamExecution] : []),
 *   ]}
 *   org={activeOrg}
 *   onApplied={(result) => toast(`${result.kind} applied`)}
 * />
 * ```
 *
 * @see {@link ArtifactCard} — compact summary card per artifact
 * @see {@link ArtifactPreviewModal} — full preview with Apply/Push CTA
 * @see {@link useSessionArtifacts} — headless session-level artifact aggregation hook
 * @see {@link useExecutionArtifacts} — headless single-execution artifact extraction hook
 */
export function ArtifactsWidget({ executions, org, onApplied, className, }) {
    const { artifacts, hasArtifacts, artifactCount } = useSessionArtifacts(executions);
    // Store the dedup key (sandboxPath or name) instead of a snapshot of
    // the full entry.  The actual entry is derived from the live artifacts
    // list on every render, so the preview modal always reflects the
    // latest artifact version — even when a newer execution publishes an
    // updated artifact for the same path while the modal is open.
    const [previewKey, setPreviewKey] = useState(null);
    const previewEntry = useMemo(() => previewKey !== null
        ? artifacts.find((e) => (e.artifact.sandboxPath || e.artifact.name) === previewKey) ?? null
        : null, [previewKey, artifacts]);
    const handlePreview = useCallback((entry) => setPreviewKey(entry.artifact.sandboxPath || entry.artifact.name), []);
    if (!hasArtifacts)
        return null;
    return (_jsxs("section", { "aria-label": "Artifacts", className: cn(className), children: [_jsxs("div", { className: "mb-2 flex items-center gap-2", children: [_jsx("h3", { className: "text-sm font-medium text-foreground", children: "Artifacts" }), _jsx("span", { className: "inline-flex min-w-[1.25rem] items-center justify-center rounded-full bg-muted px-1.5 text-xs tabular-nums text-muted-foreground", children: artifactCount })] }), _jsx("div", { role: "list", className: "space-y-2", children: artifacts.map((entry) => (_jsx("div", { role: "listitem", children: _jsx(ArtifactCard, { artifact: entry.artifact, executionId: entry.executionId, org: org, hasNameCollision: entry.hasNameCollision, onPreview: () => handlePreview(entry) }) }, entry.artifact.storageKey))) }), previewEntry && (_jsx(ArtifactPreviewModal, { artifact: previewEntry.artifact, executionId: previewEntry.executionId, org: org, isTerminal: previewEntry.isTerminal, open: true, onClose: () => setPreviewKey(null), onApplied: onApplied }))] }));
}
//# sourceMappingURL=ArtifactsWidget.js.map