import { formatBytes } from "./format.js";
import { createFetchHtmlProgressRenderer } from "./progress/fetch-html.js";
import { createTranscriptProgressRenderer } from "./progress/transcript.js";
export function createWebsiteProgress({ enabled, spinner, oscProgress, theme, }) {
    if (!enabled)
        return null;
    const fetchRenderer = createFetchHtmlProgressRenderer({ spinner, oscProgress, theme });
    const transcriptRenderer = createTranscriptProgressRenderer({ spinner, oscProgress, theme });
    const styleLabel = (text) => (theme ? theme.label(text) : text);
    const styleDim = (text) => (theme ? theme.dim(text) : text);
    const renderStatus = (label, detail) => theme ? `${styleLabel(label)}${styleDim(detail)}` : `${label}${detail}`;
    const renderTweetCliLabel = (client) => client === "xurl" ? "Xurl" : client === "bird" ? "Bird" : "X";
    const stopAll = () => {
        fetchRenderer.stop();
        transcriptRenderer.stop();
    };
    const formatFirecrawlReason = (reason) => {
        const lower = reason.toLowerCase();
        if (lower.includes("forced"))
            return "forced";
        if (lower.includes("html fetch failed"))
            return "fallback: HTML fetch failed";
        if (lower.includes("blocked") || lower.includes("thin"))
            return "fallback: blocked/thin HTML";
        return reason;
    };
    return {
        stop: stopAll,
        onProgress: (event) => {
            fetchRenderer.onProgress(event);
            transcriptRenderer.onProgress(event);
            if (event.kind === "bird-start") {
                stopAll();
                spinner.setText(renderStatus(renderTweetCliLabel(event.client), ": reading tweet…"));
                return;
            }
            if (event.kind === "bird-done") {
                stopAll();
                const label = renderTweetCliLabel(event.client);
                if (event.ok && typeof event.textBytes === "number") {
                    spinner.setText(renderStatus(label, `: got ${formatBytes(event.textBytes)}…`));
                    return;
                }
                spinner.setText(renderStatus(label, ": failed; fallback…"));
                return;
            }
            if (event.kind === "nitter-start") {
                stopAll();
                spinner.setText(renderStatus("Nitter", ": fetching…"));
                return;
            }
            if (event.kind === "nitter-done") {
                stopAll();
                if (event.ok && typeof event.textBytes === "number") {
                    spinner.setText(renderStatus("Nitter", `: got ${formatBytes(event.textBytes)}…`));
                    return;
                }
                spinner.setText(renderStatus("Nitter", ": failed; fallback…"));
                return;
            }
            if (event.kind === "firecrawl-start") {
                stopAll();
                const reason = event.reason ? formatFirecrawlReason(event.reason) : "";
                const suffix = reason ? ` (${reason})` : "";
                spinner.setText(renderStatus("Firecrawl", `: scraping${suffix}…`));
                return;
            }
            if (event.kind === "firecrawl-done") {
                stopAll();
                if (event.ok && typeof event.markdownBytes === "number") {
                    spinner.setText(renderStatus("Firecrawl", `: got ${formatBytes(event.markdownBytes)}…`));
                    return;
                }
                spinner.setText(renderStatus("Firecrawl", ": no content; fallback…"));
                return;
            }
            if (event.kind === "transcript-start") {
                stopAll();
                const label = event.hint?.trim();
                const text = label && label.length > 0 ? label : "Transcribing";
                spinner.setText(theme ? `${styleLabel(text)}${styleDim("…")}` : `${text}…`);
                return;
            }
            if (event.kind === "transcript-done") {
                stopAll();
                if (event.ok) {
                    spinner.setText(theme ? `${styleLabel("Transcribed")}${styleDim("…")}` : "Transcribed…");
                    return;
                }
                spinner.setText(theme
                    ? `${styleLabel("Transcript unavailable")}${styleDim("…")}`
                    : "Transcript unavailable…");
            }
        },
    };
}
//# sourceMappingURL=website-progress.js.map