"use client";
import { useCallback } from "react";
import { parseGitUrl } from "./parseGitUrl.js";
const GITHUB_TREES_API = "https://api.github.com/repos";
/**
 * Advisory entry appended to the listing when the GitHub API indicates that the
 * tree was truncated (repository exceeds the API's entry limit). Flagged with
 * `notice: true` so generic consumers treat it as a "results incomplete" banner
 * rather than an openable file (see `workspaceListingCache`) — never a tree leaf,
 * never a search hit.
 */
const TRUNCATION_MARKER = {
    path: "... (tree truncated by GitHub — repository has too many files)",
    isDirectory: false,
    notice: true,
};
/**
 * Creates a {@link WorkspaceFileLister} backed by the GitHub Trees API.
 *
 * Uses the existing client-side OAuth token (same one that powers
 * `GitHubRepoPicker`) to call `GET /repos/{owner}/{repo}/git/trees/{branch}?recursive=1`.
 *
 * Returns `null` for:
 * - Non-git workspace entries
 * - Missing or empty token
 * - Non-GitHub git URLs
 * - Failed API responses (graceful degradation)
 *
 * @example
 * ```tsx
 * const gitHubConnection = useGitHubConnection(org);
 * const fileLister = useGitHubTreeLister(gitHubConnection.token);
 *
 * <SessionViewer workspaceFileLister={fileLister} />
 * ```
 */
export function useGitHubTreeLister(token) {
    const lister = useCallback(async (entry) => {
        if (entry.type !== "git" || !entry.gitUrl || !token)
            return null;
        const parsed = parseGitUrl(entry.gitUrl);
        if (!parsed)
            return null;
        const branch = entry.gitBranch || "main";
        const url = `${GITHUB_TREES_API}/${parsed.owner}/${parsed.repo}/git/trees/${encodeURIComponent(branch)}?recursive=1`;
        const resp = await fetch(url, {
            headers: { Authorization: `Bearer ${token}` },
        });
        if (!resp.ok)
            return null;
        const data = await resp.json();
        const entries = data.tree
            .filter((node) => node.type === "blob" || node.type === "tree")
            .map((node) => ({
            path: node.path,
            isDirectory: node.type === "tree",
        }));
        if (data.truncated) {
            entries.push(TRUNCATION_MARKER);
        }
        return entries;
    }, [token]);
    if (!token)
        return undefined;
    return lister;
}
//# sourceMappingURL=useGitHubTreeLister.js.map