"use strict";

import { cleanFilePath } from "../utils/fileUtils.js";
export class VadService {
  initialized = false;
  constructor(api) {
    this.api = api;
  }
  validateLibrary() {
    return this.api.validateLibraryLoaded();
  }
  async init(config) {
    try {
      const validateResult = await this.validateLibrary();
      if (!validateResult.loaded) {
        return {
          success: false,
          error: validateResult.status || 'Library not loaded'
        };
      }
      const nativeConfig = {
        modelDir: cleanFilePath(config.modelDir),
        modelFile: config.modelFile ?? 'silero_vad_v5.onnx',
        threshold: config.threshold ?? 0.5,
        minSilenceDuration: config.minSilenceDuration ?? 0.25,
        minSpeechDuration: config.minSpeechDuration ?? 0.25,
        windowSize: config.windowSize ?? 512,
        maxSpeechDuration: config.maxSpeechDuration ?? 5.0,
        bufferSizeInSeconds: config.bufferSizeInSeconds ?? 30.0,
        numThreads: config.numThreads ?? 1,
        debug: config.debug ?? false,
        provider: config.provider ?? 'cpu',
        ...(config.modelBaseUrl && {
          modelBaseUrl: config.modelBaseUrl
        }),
        ...(config.onProgress && {
          onProgress: config.onProgress
        })
      };
      const result = await this.api.initVad(nativeConfig);
      if (result.success) {
        this.initialized = true;
      }
      return result;
    } catch (error) {
      return {
        success: false,
        error: error instanceof Error ? error.message : String(error)
      };
    }
  }
  async acceptWaveform(sampleRate, samples) {
    try {
      if (!this.initialized) {
        return {
          success: false,
          isSpeechDetected: false,
          segments: [],
          error: 'VAD is not initialized'
        };
      }
      return await this.api.acceptVadWaveform({
        sampleRate,
        samples
      });
    } catch (error) {
      return {
        success: false,
        isSpeechDetected: false,
        segments: [],
        error: error instanceof Error ? error.message : String(error)
      };
    }
  }
  async reset() {
    try {
      if (!this.initialized) {
        return {
          success: false
        };
      }
      return await this.api.resetVad();
    } catch {
      return {
        success: false
      };
    }
  }
  async release() {
    try {
      const result = await this.api.releaseVad();
      if (result.released) {
        this.initialized = false;
      }
      return result;
    } catch (error) {
      console.error('Error releasing VAD resources:', error);
      return {
        released: false
      };
    }
  }
}
//# sourceMappingURL=VadService.js.map