"use client";
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
import { useStigmer } from "../hooks.js";
import { toError } from "../internal/toError.js";
import { fetchAndUnpackArtifact } from "./internal/fetchAndUnpackArtifact.js";
// ---------------------------------------------------------------------------
// Hook implementation
// ---------------------------------------------------------------------------
/**
 * Data hook that fetches and unpacks a skill's ZIP artifact for browsing.
 *
 * Uses `stigmer.skill.getArtifact()` with the skill's storage key,
 * then decompresses the ZIP using `fflate` to expose a file listing
 * and content retrieval.
 *
 * Pass `null` for `artifactStorageKey` to skip fetching (stable no-op).
 *
 * @example
 * ```tsx
 * const { skill } = useSkill(org, slug);
 * const { files, getFileContent, isLoading } = useSkillArtifact(
 *   skill?.status?.artifactStorageKey ?? null
 * );
 *
 * if (files) {
 *   const readme = getFileContent("SKILL.md");
 * }
 * ```
 */
export function useSkillArtifact(artifactStorageKey) {
    const stigmer = useStigmer();
    const [files, setFiles] = useState(null);
    const [isLoading, setIsLoading] = useState(false);
    const [error, setError] = useState(null);
    const contentMapRef = useRef(new Map());
    const fetchIdRef = useRef(0);
    const doFetch = useCallback(async () => {
        if (!artifactStorageKey) {
            setFiles(null);
            setError(null);
            return;
        }
        const fetchId = ++fetchIdRef.current;
        setIsLoading(true);
        setError(null);
        try {
            const result = await fetchAndUnpackArtifact(stigmer, artifactStorageKey);
            if (fetchIdRef.current !== fetchId)
                return;
            contentMapRef.current = new Map(result.contentMap);
            setFiles(result.files);
        }
        catch (err) {
            if (fetchIdRef.current !== fetchId)
                return;
            setError(toError(err));
            setFiles(null);
        }
        finally {
            if (fetchIdRef.current === fetchId) {
                setIsLoading(false);
            }
        }
    }, [artifactStorageKey, stigmer]);
    useEffect(() => {
        doFetch();
    }, [doFetch]);
    const getFileContent = useCallback((path) => {
        return contentMapRef.current.get(path) ?? null;
    }, []);
    const refetch = useCallback(() => {
        doFetch();
    }, [doFetch]);
    return useMemo(() => ({ files, isLoading, error, getFileContent, refetch }), [files, isLoading, error, getFileContent, refetch]);
}
//# sourceMappingURL=useSkillArtifact.js.map