"use strict";

import { loadCombinedWasm } from "../wasmLoader.js";
import { fetchAndDecodeAudio } from "../audioUtils.js";
import { withDownloadProgress } from "./mixinUtils.js";
export function LanguageIdMixin(Base) {
  return class extends Base {
    languageId = null;
    async initLanguageId(config) {
      try {
        await loadCombinedWasm();
        if (!window.SherpaOnnx.LanguageId) {
          return {
            success: false,
            error: 'LanguageId module not loaded'
          };
        }
        const debug = config?.debug ? 1 : 0;
        const numThreads = 1; // WASM is single-threaded

        console.log(`[LanguageId] Loading model (threads=${numThreads}, debug=${debug})...`);
        // Web-only: config.modelDir is set to '/wasm/language-id' by ModelManagement
        // (createWebLanguageIdModelState in constants.ts), pointing to model files
        // pre-served by download-web-models.sh. Native LanguageId uses the TurboModule.
        const modelDir = config?.modelDir || '/wasm/language-id';
        const fetchBase = config?.modelBaseUrl || modelDir;
        const loadedModel = await withDownloadProgress(config?.onProgress, () => window.SherpaOnnx.LanguageId.loadModel({
          encoder: `${fetchBase}/tiny-encoder.onnx`,
          decoder: `${fetchBase}/tiny-decoder.onnx`,
          modelDir,
          debug
        }));
        this.languageId = window.SherpaOnnx.LanguageId.createLanguageId(loadedModel, {
          numThreads,
          debug
        });
        console.log('[LanguageId] Initialized successfully');
        return {
          success: true
        };
      } catch (error) {
        console.error('[LanguageId] initLanguageId failed:', error);
        return {
          success: false,
          error: error.message
        };
      }
    }
    async detectLanguage({
      sampleRate,
      samples: rawSamples
    }) {
      if (!this.languageId) {
        return {
          success: false,
          language: '',
          durationMs: 0,
          error: 'Language ID not initialized'
        };
      }
      try {
        const startMs = performance.now();
        const samples = new Float32Array(rawSamples);
        const stream = this.languageId.createStream();
        this.languageId.acceptWaveform(stream, sampleRate, samples);
        const lang = this.languageId.compute(stream);
        this.languageId.destroyStream?.(stream);
        const durationMs = performance.now() - startMs;
        return {
          success: true,
          language: lang,
          durationMs
        };
      } catch (error) {
        return {
          success: false,
          language: '',
          durationMs: 0,
          error: error.message
        };
      }
    }
    async detectLanguageFromFile(filePath) {
      if (!this.languageId) {
        return {
          success: false,
          language: '',
          durationMs: 0,
          error: 'Language ID not initialized'
        };
      }
      try {
        const startMs = performance.now();
        const {
          samples,
          sampleRate
        } = await fetchAndDecodeAudio(filePath);
        const stream = this.languageId.createStream();
        this.languageId.acceptWaveform(stream, sampleRate, samples);
        const lang = this.languageId.compute(stream);
        this.languageId.destroyStream?.(stream);
        const durationMs = performance.now() - startMs;
        return {
          success: true,
          language: lang,
          durationMs
        };
      } catch (error) {
        return {
          success: false,
          language: '',
          durationMs: 0,
          error: error.message
        };
      }
    }
    async releaseLanguageId() {
      if (this.languageId) {
        try {
          this.languageId.free();
        } catch (_) {
          console.error('[LanguageId] releaseLanguageId failed:', _);
        }
        this.languageId = null;
      }
      return {
        released: true
      };
    }
  };
}
//# sourceMappingURL=languageId.js.map