"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.LanguageIdService = void 0;
var _fileUtils = require("../utils/fileUtils.js");
class LanguageIdService {
  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: (0, _fileUtils.cleanFilePath)(config.modelDir),
        encoderFile: config.encoderFile,
        decoderFile: config.decoderFile,
        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.initLanguageId(nativeConfig);
      if (result.success) {
        this.initialized = true;
      }
      return result;
    } catch (error) {
      return {
        success: false,
        error: error instanceof Error ? error.message : String(error)
      };
    }
  }
  async detectLanguage(sampleRate, samples) {
    try {
      if (!this.initialized) {
        return {
          success: false,
          language: '',
          durationMs: 0,
          error: 'Language ID is not initialized'
        };
      }
      return await this.api.detectLanguage({
        sampleRate,
        samples
      });
    } catch (error) {
      return {
        success: false,
        language: '',
        durationMs: 0,
        error: error instanceof Error ? error.message : String(error)
      };
    }
  }
  async detectLanguageFromFile(filePath) {
    try {
      if (!this.initialized) {
        return {
          success: false,
          language: '',
          durationMs: 0,
          error: 'Language ID is not initialized'
        };
      }
      return await this.api.detectLanguageFromFile((0, _fileUtils.cleanFilePath)(filePath));
    } catch (error) {
      return {
        success: false,
        language: '',
        durationMs: 0,
        error: error instanceof Error ? error.message : String(error)
      };
    }
  }
  async release() {
    try {
      const result = await this.api.releaseLanguageId();
      if (result.released) {
        this.initialized = false;
      }
      return result;
    } catch (error) {
      console.error('Error releasing Language ID resources:', error);
      return {
        released: false
      };
    }
  }
}
exports.LanguageIdService = LanguageIdService;
//# sourceMappingURL=LanguageIdService.js.map