"use client";
import { useCallback, useMemo, useState } from "react";
import { create } from "@bufbuild/protobuf";
import { GetArtifactDownloadUrlRequestSchema } from "@stigmer/protos/ai/stigmer/agentic/agentexecution/v1/io_pb";
import { useStigmer } from "../hooks.js";
import { toError } from "../internal/toError.js";
/**
 * Behavior hook that downloads an execution artifact on demand.
 *
 * Mirrors the click → mint fresh URL → browser download flow established by
 * `WorkflowExecutionArtifactPanel`, generalized so every artifact "Download"
 * affordance shares one implementation. The URL is minted at click time from
 * the stable `storageKey` via `getArtifactDownloadUrl`, so it is always valid —
 * unlike the previously persisted, short-lived URL that expired after an hour.
 *
 * Returns an imperative `download(storageKey, fileName?)` action rather than a
 * URL, because a download is a user action, not render data. For rendering an
 * image or showing a link, use {@link useArtifactDownloadUrl} instead.
 *
 * @param executionId - Execution that produced the artifacts, or `null` to
 *   disable (the action becomes a no-op).
 *
 * @example
 * ```tsx
 * const { download, isDownloading } = useArtifactDownload(executionId);
 * <button onClick={() => download(artifact.storageKey, artifact.name)} disabled={isDownloading}>
 *   Download
 * </button>
 * ```
 *
 * @see useArtifactDownloadUrl — declarative URL resolver (image src, links)
 */
export function useArtifactDownload(executionId) {
    const stigmer = useStigmer();
    const [isDownloading, setIsDownloading] = useState(false);
    const [error, setError] = useState(null);
    const download = useCallback(async (storageKey, fileName) => {
        if (!executionId || !storageKey || isDownloading)
            return;
        setIsDownloading(true);
        setError(null);
        try {
            const result = await stigmer.agentExecution.getArtifactDownloadUrl(create(GetArtifactDownloadUrlRequestSchema, {
                executionId,
                storageKey,
                // This is a save-to-disk action, so mint a URL that forces a
                // browser download (Content-Disposition: attachment). Browsers
                // ignore the anchor's `download` attribute cross-origin, so the
                // disposition must ride on the URL itself. Contrast
                // useArtifactDownloadUrl, which stays inline for `<img src>`.
                asAttachment: true,
            }));
            if (result.downloadUrl) {
                triggerBrowserDownload(result.downloadUrl, fileName);
            }
        }
        catch (err) {
            setError(toError(err));
        }
        finally {
            setIsDownloading(false);
        }
    }, [executionId, stigmer, isDownloading]);
    return useMemo(() => ({ download, isDownloading, error }), [download, isDownloading, error]);
}
/**
 * Trigger a browser download for a URL via a transient anchor click.
 *
 * The URL minted for this flow carries `Content-Disposition: attachment`
 * (requested via `asAttachment`), so the browser saves the file even
 * cross-origin — where it would ignore the anchor's `download` attribute. The
 * `download` hint is still set as a same-origin nicety. Falls back to a new tab
 * in non-DOM environments.
 */
function triggerBrowserDownload(url, fileName) {
    if (typeof document === "undefined") {
        if (typeof window !== "undefined") {
            window.open(url, "_blank", "noopener,noreferrer");
        }
        return;
    }
    const anchor = document.createElement("a");
    anchor.href = url;
    if (fileName)
        anchor.download = fileName;
    anchor.rel = "noopener noreferrer";
    anchor.target = "_blank";
    document.body.appendChild(anchor);
    anchor.click();
    document.body.removeChild(anchor);
}
//# sourceMappingURL=useArtifactDownload.js.map