"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.TtsMixin = TtsMixin;
var _wasmLoader = require("../wasmLoader.js");
var _audioUtils = require("../audioUtils.js");
var _mixinUtils = require("./mixinUtils.js");
function TtsMixin(Base) {
  return class extends Base {
    tts = null;
    ttsSampleRate = 0;
    ttsNumSpeakers = 0;
    async initTts(config) {
      try {
        await (0, _wasmLoader.loadCombinedWasm)();
        const debug = config.debug ? 1 : 0;
        const numThreads = 1; // WASM is single-threaded

        console.log(`[TTS] Loading ${config.ttsModelType || 'vits'} model (threads=${numThreads}, debug=${debug})...`);
        // Web-only: config.modelDir is set to '/wasm/tts' by ModelManagement
        // (createWebTtsModelState in constants.ts), pointing to model files
        // pre-served by download-web-models.sh. Native TTS uses the TurboModule.
        const modelDir = config.modelDir || '/wasm/tts';
        const fetchBase = config.modelBaseUrl || modelDir;
        const loadedModel = await (0, _mixinUtils.withDownloadProgress)(config.onProgress, () => window.SherpaOnnx.TTS.loadModel({
          type: config.ttsModelType || 'vits',
          model: `${fetchBase}/model.onnx`,
          tokens: `${fetchBase}/tokens.txt`,
          espeakDataZip: `${fetchBase}/espeak-ng-data.zip`,
          modelDir,
          debug
        }));
        this.tts = window.SherpaOnnx.TTS.createOfflineTts(loadedModel, {
          numThreads,
          debug
        });
        this.ttsSampleRate = this.tts.sampleRate;
        this.ttsNumSpeakers = this.tts.numSpeakers;
        console.log(`[TTS] Initialized: sampleRate=${this.ttsSampleRate}, numSpeakers=${this.ttsNumSpeakers}`);
        return {
          success: true,
          sampleRate: this.ttsSampleRate,
          numSpeakers: this.ttsNumSpeakers
        };
      } catch (error) {
        console.error('[TTS] initTts failed:', error);
        return {
          success: false,
          sampleRate: 0,
          numSpeakers: 0,
          error: error.message
        };
      }
    }
    async generateTts(config) {
      if (!this.tts) {
        return {
          success: false
        };
      }
      try {
        const audio = this.tts.generate({
          text: config.text,
          sid: config.speakerId,
          speed: config.speakingRate
        });
        const samples = new Float32Array(audio.samples);
        const result = {
          success: true
        };
        if (config.playAudio) {
          await (0, _audioUtils.playAudioSamples)(samples, audio.sampleRate);
        }
        if (config.fileNamePrefix) {
          const wav = (0, _audioUtils.samplesToWav)(samples, audio.sampleRate);
          result.filePath = URL.createObjectURL(wav);
        }
        return result;
      } catch (error) {
        console.error('[TTS] generateTts failed:', error);
        return {
          success: false
        };
      }
    }
    async stopTts() {
      (0, _audioUtils.stopAudioPlayback)();
      return {
        stopped: true
      };
    }
    async releaseTts() {
      if (this.tts) {
        try {
          this.tts.free();
        } catch (_) {
          console.error('[TTS] releaseTts failed:', _);
        }
        this.tts = null;
      }
      return {
        released: true
      };
    }
  };
}
//# sourceMappingURL=tts.js.map