"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.AudioTaggingMixin = AudioTaggingMixin;
var _wasmLoader = require("../wasmLoader.js");
var _audioUtils = require("../audioUtils.js");
var _mixinUtils = require("./mixinUtils.js");
function AudioTaggingMixin(Base) {
  return class extends Base {
    audioTagger = null;
    async initAudioTagging(config) {
      try {
        await (0, _wasmLoader.loadCombinedWasm)();
        if (!window.SherpaOnnx.AudioTagging) {
          return {
            success: false,
            error: 'AudioTagging module not loaded'
          };
        }
        const debug = config.debug ? 1 : 0;
        const numThreads = 1; // WASM is single-threaded

        console.log(`[AudioTagging] Loading model (threads=${numThreads}, debug=${debug})...`);
        // Web-only: config.modelDir is set to '/wasm/audio-tagging' by ModelManagement
        // (createWebAudioTaggingModelState in constants.ts), pointing to model files
        // pre-served by download-web-models.sh. Native AudioTagging uses the TurboModule.
        const modelDir = config.modelDir || '/wasm/audio-tagging';
        const fetchBase = config.modelBaseUrl || modelDir;
        const loadedModel = await (0, _mixinUtils.withDownloadProgress)(config.onProgress, () => window.SherpaOnnx.AudioTagging.loadModel({
          ced: `${fetchBase}/model.onnx`,
          labels: `${fetchBase}/labels.txt`,
          modelDir,
          debug
        }));
        this.audioTagger = window.SherpaOnnx.AudioTagging.createAudioTagging(loadedModel, {
          topK: config.topK || 5,
          numThreads,
          debug
        });
        console.log('[AudioTagging] Initialized successfully');
        return {
          success: true
        };
      } catch (error) {
        console.error('[AudioTagging] initAudioTagging failed:', error);
        return {
          success: false,
          error: error.message
        };
      }
    }
    async processAndComputeAudioTagging({
      filePath
    }) {
      if (!this.audioTagger) {
        return {
          success: false,
          durationMs: 0,
          events: [],
          error: 'Audio tagging not initialized'
        };
      }
      try {
        const startMs = performance.now();
        const {
          samples,
          sampleRate
        } = await (0, _audioUtils.fetchAndDecodeAudio)(filePath);
        const stream = this.audioTagger.createStream();
        this.audioTagger.acceptWaveform(stream, sampleRate, samples);
        const events = this.audioTagger.compute(stream, -1);
        this.audioTagger.destroyStream?.(stream);
        const durationMs = performance.now() - startMs;
        return {
          success: true,
          durationMs,
          events: events.map(e => ({
            name: e.name,
            prob: e.prob,
            index: e.index
          }))
        };
      } catch (error) {
        console.error('[AudioTagging] processAndComputeAudioTagging failed:', error);
        return {
          success: false,
          durationMs: 0,
          events: [],
          error: error.message
        };
      }
    }
    async processAndComputeAudioSamples({
      sampleRate,
      samples: rawSamples
    }) {
      if (!this.audioTagger) {
        return {
          success: false,
          durationMs: 0,
          events: [],
          error: 'Audio tagging not initialized'
        };
      }
      try {
        const startMs = performance.now();
        const samples = new Float32Array(rawSamples);
        const stream = this.audioTagger.createStream();
        this.audioTagger.acceptWaveform(stream, sampleRate, samples);
        const events = this.audioTagger.compute(stream, -1);
        this.audioTagger.destroyStream?.(stream);
        const durationMs = performance.now() - startMs;
        return {
          success: true,
          durationMs,
          events: events.map(e => ({
            name: e.name,
            prob: e.prob,
            index: e.index
          }))
        };
      } catch (error) {
        console.error('[AudioTagging] processAndComputeAudioSamples failed:', error);
        return {
          success: false,
          durationMs: 0,
          events: [],
          error: error.message
        };
      }
    }
    async releaseAudioTagging() {
      if (this.audioTagger) {
        try {
          this.audioTagger.free();
        } catch (_) {
          console.error('[AudioTagging] releaseAudioTagging failed:', _);
        }
        this.audioTagger = null;
      }
      return {
        released: true
      };
    }
  };
}
//# sourceMappingURL=audioTagging.js.map