import { applyTranscriptProgressEvent, createTranscriptProgressState, renderTranscriptLine, renderTranscriptSimple, resolveTranscriptOscPayload, } from "./transcript-state.js";
export function createTranscriptProgressRenderer({ spinner, oscProgress, theme, }) {
    const state = createTranscriptProgressState();
    let ticker = null;
    let lastSpinnerUpdateAtMs = 0;
    const updateSpinner = (text, options) => {
        const now = Date.now();
        if (!options?.force && now - lastSpinnerUpdateAtMs < 100)
            return;
        lastSpinnerUpdateAtMs = now;
        spinner.setText(text);
    };
    const refreshSpinner = () => {
        spinner.refresh?.();
    };
    const stopTicker = () => {
        if (!ticker)
            return;
        clearInterval(ticker);
        ticker = null;
    };
    const renderActiveLine = () => renderTranscriptLine(state, {
        nowMs: Date.now(),
        theme,
    }) ?? "";
    const updateOsc = () => {
        if (!oscProgress)
            return;
        const payload = resolveTranscriptOscPayload(state);
        if (!payload)
            return;
        if (typeof payload.percent === "number") {
            oscProgress.setPercent(payload.label, payload.percent);
        }
        else {
            oscProgress.setIndeterminate(payload.label);
        }
        refreshSpinner();
    };
    const startTicker = () => {
        ticker = setInterval(() => {
            updateSpinner(renderActiveLine());
            updateOsc();
        }, 1000);
    };
    const updatePhase = (event, options) => {
        applyTranscriptProgressEvent(state, event, Date.now());
        if (options?.stopTicker)
            stopTicker();
        updateSpinner(renderActiveLine(), { force: options?.force });
        updateOsc();
    };
    return {
        stop: stopTicker,
        onProgress: (event) => {
            if (event.kind === "transcript-media-download-start") {
                applyTranscriptProgressEvent(state, event, Date.now());
                stopTicker();
                startTicker();
                updateSpinner(renderTranscriptSimple(state, theme) ?? "", { force: true });
                updateOsc();
                return;
            }
            if (event.kind === "transcript-media-download-progress") {
                updatePhase(event);
                return;
            }
            if (event.kind === "transcript-media-download-done") {
                updatePhase(event, { force: true, stopTicker: true });
                return;
            }
            if (event.kind === "transcript-whisper-start") {
                applyTranscriptProgressEvent(state, event, Date.now());
                stopTicker();
                startTicker();
                updateSpinner(renderActiveLine(), { force: true });
                updateOsc();
                return;
            }
            if (event.kind === "transcript-whisper-progress") {
                updatePhase(event);
            }
        },
    };
}
//# sourceMappingURL=transcript.js.map