"use client";
import { useMemo } from "react";
import { useFetch } from "../internal/useFetch.js";
const EMPTY_CONTENT = { content: null };
/**
 * Behavior hook that reads a single workspace file's content on demand.
 *
 * The byte-reading peer of {@link useWorkspaceFiles}
 * (which lists the tree), built on the same {@link useFetch} engine as the
 * content-fetch family ({@link useArtifactContent},
 * `useToolOutputContent`, `useFileChangeContent`) so it inherits their
 * stale-while-revalidate, cancellation, and `refetch` semantics for free.
 *
 * Deliberately passes **no** `cacheKey`: file bodies (up to
 * {@link MAX_WORKSPACE_FILE_READ_BYTES})
 * do not belong in the cross-mount metadata cache, which is sized for small,
 * hot resources. Re-opening a file re-reads it — the correct trade for a
 * read-only viewer.
 *
 * Maps the reader's two failure axes onto distinct states:
 * - reader resolves `null` (unsupported substrate) -> {@link UseWorkspaceFileContentReturn.isUnsupported}
 * - reader **throws** (404, network, unreadable) -> {@link UseWorkspaceFileContentReturn.error}
 *
 * Pass `null` for `entry`/`path` (or an `undefined` reader) to stay idle —
 * the established `useFetch` convention.
 *
 * @example
 * ```tsx
 * const { content, isLoading, error, isUnsupported } = useWorkspaceFileContent({
 *   entry,
 *   path: "src/index.ts",
 *   reader: workspaceFileReader,
 * });
 * ```
 */
export function useWorkspaceFileContent({ entry, path, reader, }) {
    // The fetch runs only when a reader, an entry, and a path are all present.
    const enabled = !!(reader && entry && path);
    const { data, isLoading, isRefetching, error, refetch } = useFetch(enabled
        ? () => reader(entry, path).then((c) => ({ content: c }))
        : null, 
    // Identity deps: a change to any of these is a different file to read.
    [entry?.id ?? null, path ?? null, reader], EMPTY_CONTENT);
    // Unsupported = no reader at all, or a completed fetch that resolved to null
    // (the reader's "this substrate isn't mine" signal). In-flight and error
    // states are excluded so the viewer shows loading/error, not "unavailable".
    const isUnsupported = !reader || (enabled && !isLoading && !error && data.content === null);
    return useMemo(() => ({
        content: data.content,
        isLoading,
        isRefetching,
        error,
        isUnsupported,
        refetch,
    }), [data.content, isLoading, isRefetching, error, isUnsupported, refetch]);
}
//# sourceMappingURL=useWorkspaceFileContent.js.map