/**
 * fetch-url-content.ts
 *
 * Fetches documentation content from a URL. Tries the .md endpoint first
 * (Sanity's agent-friendly format), then content-type negotiation.
 * Does NOT attempt HTML-to-Markdown conversion — if the endpoint
 * doesn't serve markdown, the fetch fails cleanly.
 *
 * Part of Phase 4: Modular doc fetching for baseline mode.
 */
/**
 * Fetch documentation content from a URL.
 *
 * Strategy:
 *   1. Try `.md` endpoint with `Accept: text/markdown`
 *   2. Try content-type negotiation (ask for markdown)
 *   3. Fail cleanly if only HTML is available (no conversion)
 *
 * @param url - The documentation URL to fetch
 * @param headers - Optional custom headers to merge with defaults
 * @returns A `UrlFetchResult` with success or failure metadata
 */
export async function fetchUrlContent(url, headers) {
    const cleanUrl = url.replace(/\/$/, "");
    const mergedHeaders = {
        ...headers,
        "User-Agent": "SanityEvalBot/1.0",
    };
    // Strategy 1: Try .md endpoint
    const mdUrl = cleanUrl.endsWith(".md") ? cleanUrl : `${cleanUrl}.md`;
    try {
        const response = await fetch(mdUrl, {
            headers: { ...mergedHeaders, Accept: "text/markdown, text/plain" },
        });
        if (response.ok) {
            const contentType = response.headers.get("content-type") ?? "";
            const text = await response.text();
            // Accept if content-type says markdown OR the content doesn't look like HTML
            if (contentType.includes("markdown") ||
                !text.trimStart().startsWith("<!DOCTYPE")) {
                return {
                    content: text,
                    method: "md-endpoint",
                    status: response.status,
                    url,
                };
            }
        }
    }
    catch {
        // .md endpoint unavailable — try next strategy
    }
    // Strategy 2: Content-type negotiation (ask for markdown)
    try {
        const response = await fetch(cleanUrl, {
            headers: { ...mergedHeaders, Accept: "text/markdown, text/plain" },
        });
        if (response.ok) {
            const contentType = response.headers.get("content-type") ?? "";
            const text = await response.text();
            if (contentType.includes("markdown") ||
                contentType.includes("text/plain")) {
                return {
                    content: text,
                    method: "content-negotiation",
                    status: response.status,
                    url,
                };
            }
            // Got HTML or something else — don't try to convert it
            return {
                error: `Endpoint returned ${contentType} instead of markdown. ` +
                    "The URL does not appear to support markdown content negotiation.",
                method: "failed",
                status: response.status,
                url,
            };
        }
        return {
            error: `HTTP ${response.status}`,
            method: "failed",
            status: response.status,
            url,
        };
    }
    catch (err) {
        return {
            error: `Network error: ${err.message}`,
            method: "failed",
            url,
        };
    }
}
