"use strict";

import { cleanFilePath } from "../utils/fileUtils.js";
export 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: 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(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
      };
    }
  }
}
//# sourceMappingURL=LanguageIdService.js.map