"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.AsrService = void 0;
var _fileUtils = require("../utils/fileUtils.js");
/**
 * ASR Service for automatic speech recognition functionality
 */

/**
 * Service for Automatic Speech Recognition functionality
 */
class AsrService {
  initialized = false;
  constructor(api) {
    this.api = api;
  }

  /**
   * Initialize the ASR engine
   * @param config The ASR model configuration
   * @returns Promise resolving to initialization result
   */
  async initialize(config) {
    try {
      // First validate library
      const validation = await this.api.validateLibraryLoaded();
      if (!validation.loaded) {
        throw new Error(`Library validation failed: ${validation.status}`);
      }
      const cleanedConfig = {
        ...config,
        modelDir: (0, _fileUtils.cleanFilePath)(config.modelDir)
      };
      // Note: passing a streaming-only model with streaming: false will produce
      // a cryptic native error. See AsrModelConfig.streaming JSDoc for compatible types.
      const result = await this.api.initAsr(cleanedConfig);
      this.initialized = result.success;
      return result;
    } catch (error) {
      this.initialized = false;
      throw error;
    }
  }

  /**
   * Recognize speech from audio samples
   * @param sampleRate The sample rate of the audio
   * @param samples Float array of audio samples
   * @returns Promise resolving to recognition result
   */
  async recognizeFromSamples(sampleRate, samples) {
    if (!this.initialized) {
      throw new Error('ASR is not initialized. Call initialize() first.');
    }
    return this.api.recognizeFromSamples({
      sampleRate,
      samples
    });
  }

  /**
   * Recognize speech from an audio file
   * @param filePath Path to the audio file
   * @returns Promise resolving to recognition result
   */
  async recognizeFromFile(filePath) {
    if (!this.initialized) {
      throw new Error('ASR is not initialized. Call initialize() first.');
    }
    return this.api.recognizeFromFile((0, _fileUtils.cleanFilePath)(filePath));
  }

  /**
   * Create a persistent online stream for live streaming ASR.
   * Requires initAsr with streaming=true to have been called first.
   */
  async createOnlineStream() {
    if (!this.initialized) {
      throw new Error('ASR is not initialized. Call initialize() first.');
    }
    return this.api.createAsrOnlineStream();
  }

  /**
   * Feed audio samples to the online stream and decode ready frames.
   */
  async acceptWaveform(sampleRate, samples) {
    if (!this.initialized) {
      throw new Error('ASR is not initialized. Call initialize() first.');
    }
    return this.api.acceptAsrOnlineWaveform({
      sampleRate,
      samples
    });
  }

  /**
   * Check if an endpoint (sentence boundary) has been detected.
   */
  async isEndpoint() {
    if (!this.initialized) {
      throw new Error('ASR is not initialized. Call initialize() first.');
    }
    return this.api.isAsrOnlineEndpoint();
  }

  /**
   * Get the current recognition result from the online stream.
   */
  async getResult() {
    if (!this.initialized) {
      throw new Error('ASR is not initialized. Call initialize() first.');
    }
    return this.api.getAsrOnlineResult();
  }

  /**
   * Signal end-of-input and drain ready frames before reading the final result.
   */
  async finishInput() {
    if (!this.initialized) {
      throw new Error('ASR is not initialized. Call initialize() first.');
    }
    return this.api.finishAsrOnlineInput();
  }

  /**
   * Reset the online stream for the next utterance (after endpoint).
   */
  async resetStream() {
    if (!this.initialized) {
      throw new Error('ASR is not initialized. Call initialize() first.');
    }
    return this.api.resetAsrOnlineStream();
  }

  /**
   * Release ASR resources
   */
  async release() {
    const result = await this.api.releaseAsr();
    if (result.released) {
      this.initialized = false;
    }
    return result;
  }
}
exports.AsrService = AsrService;
//# sourceMappingURL=AsrService.js.map