import { formatBytes } from "../../../tty/format.js";
import { withBirdTip } from "../../bird.js";
import { buildSummaryFinishLabel } from "../../finish-line.js";
import { formatOptionalNumber, formatOptionalString } from "../../format.js";
import { writeVerbose } from "../../logging.js";
export async function fetchLinkContentWithBirdTip({ client, url, options, env, }) {
    try {
        return await client.fetchLinkContent(url, options);
    }
    catch (error) {
        throw withBirdTip(error, url, env);
    }
}
export function deriveExtractionUi(extracted) {
    const extractedContentBytes = Buffer.byteLength(extracted.content, "utf8");
    const contentSizeLabel = formatBytes(extractedContentBytes);
    const twitterStrategy = extracted.diagnostics.strategy === "xurl" || extracted.diagnostics.strategy === "bird"
        ? extracted.diagnostics.strategy
        : null;
    const viaSources = [];
    if (twitterStrategy) {
        viaSources.push(twitterStrategy);
    }
    if (extracted.diagnostics.strategy === "nitter") {
        viaSources.push("Nitter");
    }
    if (extracted.diagnostics.firecrawl.used) {
        viaSources.push("Firecrawl");
    }
    const viaSourceLabel = viaSources.length > 0 ? `, ${viaSources.join("+")}` : "";
    const footerParts = [];
    if (extracted.diagnostics.strategy === "html")
        footerParts.push("html");
    if (twitterStrategy)
        footerParts.push(twitterStrategy);
    if (extracted.diagnostics.strategy === "nitter")
        footerParts.push("nitter");
    if (extracted.diagnostics.firecrawl.used)
        footerParts.push("firecrawl");
    if (extracted.diagnostics.markdown.used) {
        if (extracted.diagnostics.markdown.provider === "llm") {
            footerParts.push(extracted.diagnostics.markdown.notes === "transcript" ? "transcript→md llm" : "html→md llm");
        }
        else {
            footerParts.push("markdown");
        }
    }
    if (extracted.diagnostics.transcript.textProvided) {
        footerParts.push(`transcript ${extracted.diagnostics.transcript.provider ?? "unknown"}`);
    }
    if (extracted.isVideoOnly && extracted.video) {
        footerParts.push(extracted.video.kind === "youtube" ? "video youtube" : "video url");
    }
    const finishSourceLabel = buildSummaryFinishLabel({
        extracted: { diagnostics: extracted.diagnostics, wordCount: extracted.wordCount },
    });
    return {
        contentSizeLabel,
        viaSourceLabel,
        footerParts,
        finishSourceLabel,
    };
}
export function logExtractionDiagnostics({ extracted, stderr, verbose, verboseColor, env, }) {
    writeVerbose(stderr, verbose, `extract done strategy=${extracted.diagnostics.strategy} siteName=${formatOptionalString(extracted.siteName)} title=${formatOptionalString(extracted.title)} transcriptSource=${formatOptionalString(extracted.transcriptSource)}`, verboseColor, env);
    writeVerbose(stderr, verbose, `extract stats characters=${extracted.totalCharacters} words=${extracted.wordCount} transcriptCharacters=${formatOptionalNumber(extracted.transcriptCharacters)} transcriptLines=${formatOptionalNumber(extracted.transcriptLines)}`, verboseColor, env);
    writeVerbose(stderr, verbose, `extract firecrawl attempted=${extracted.diagnostics.firecrawl.attempted} used=${extracted.diagnostics.firecrawl.used} notes=${formatOptionalString(extracted.diagnostics.firecrawl.notes ?? null)}`, verboseColor, env);
    writeVerbose(stderr, verbose, `extract markdown requested=${extracted.diagnostics.markdown.requested} used=${extracted.diagnostics.markdown.used} provider=${formatOptionalString(extracted.diagnostics.markdown.provider ?? null)} notes=${formatOptionalString(extracted.diagnostics.markdown.notes ?? null)}`, verboseColor, env);
    writeVerbose(stderr, verbose, `extract transcript textProvided=${extracted.diagnostics.transcript.textProvided} provider=${formatOptionalString(extracted.diagnostics.transcript.provider ?? null)} attemptedProviders=${extracted.diagnostics.transcript.attemptedProviders.length > 0
        ? extracted.diagnostics.transcript.attemptedProviders.join(",")
        : "none"} notes=${formatOptionalString(extracted.diagnostics.transcript.notes ?? null)}`, verboseColor, env);
}
//# sourceMappingURL=extract.js.map