// WhisperAsrEngine（Phase 1 引擎抽象与 Worker 化）。
// 将现有 OnnxWhisperAudioTranscriber 适配到 LocalAsrEngine 契约，识别行为保持原样：
// pushAudio 非阻塞、内部单条推理链、reset/dispose/metrics 全支持。
// 模型管理（status/inspect/download/remove/availability/dump）仍在 transcriber 上，
// 由 asr-worker-handler 直接调用，不进入引擎会话。
import { OnnxWhisperAudioTranscriber } from "./onnx-transcriber.js";
const SAMPLE_RATE = 16_000;
export class WhisperAsrEngine {
    backend = "whisper-wasm";
    mode = "segmented";
    transcriber;
    emit;
    constructor(transcriber = new OnnxWhisperAudioTranscriber(), emit = () => undefined) {
        this.transcriber = transcriber;
        this.emit = emit;
        this.transcriber.setDiagnostics((line) => this.emit({ type: "diagnostic", line }));
    }
    async init(config) {
        if (config.sourceLanguage === "auto")
            throw new Error("language_unsupported");
        await this.transcriber.start({
            sourceLanguage: config.sourceLanguage,
            onPartial: (text, startMs, endMs) => config.onPartial(text, startMs, endMs),
            onStable: (text, startMs, endMs) => config.onStable(text, startMs, endMs),
            onError: (error) => config.onError({ code: error.code }),
        });
    }
    pushAudio(pcm16, startMs) {
        this.transcriber.push({ pcm16: pcm16.buffer, createdAt: startMs, durationMs: (pcm16.length / SAMPLE_RATE) * 1000 });
    }
    recordDroppedAudio(durationMs) {
        this.transcriber.recordDroppedAudio(durationMs);
    }
    flush(_endMs) {
        this.transcriber.flushPending();
    }
    reset(reason) {
        // Whisper 无流式状态可回退：暂停类重置走 setPaused，其余丢弃当前会话。
        if (reason === "pause") {
            this.transcriber.setPaused(true);
            return;
        }
        this.transcriber.stop();
    }
    setPaused(paused) {
        this.transcriber.setPaused(paused);
    }
    idlePause() {
        this.transcriber.idlePause();
    }
    resumeFromIdle() {
        this.transcriber.resumeFromIdle();
    }
    metrics() {
        return this.transcriber.metrics();
    }
    dispose() {
        this.transcriber.stop();
    }
}
