"use strict";

import { cleanFilePath } from "../utils/fileUtils.js";
export class DiarizationService {
  initialized = false;
  sampleRate = 0;
  constructor(api) {
    this.api = api;
  }
  getSampleRate() {
    return this.sampleRate;
  }
  validateLibrary() {
    return this.api.validateLibraryLoaded();
  }
  async init(config) {
    try {
      const validateResult = await this.validateLibrary();
      if (!validateResult.loaded) {
        return {
          success: false,
          sampleRate: 0,
          error: validateResult.status || 'Library not loaded'
        };
      }
      const cleanedConfig = {
        ...config,
        segmentationModelDir: cleanFilePath(config.segmentationModelDir),
        segmentationModelFile: config.segmentationModelFile ? cleanFilePath(config.segmentationModelFile) : undefined,
        embeddingModelFile: cleanFilePath(config.embeddingModelFile)
      };
      const result = await this.api.initDiarization(cleanedConfig);
      if (result.success) {
        this.initialized = true;
        this.sampleRate = result.sampleRate;
      }
      return result;
    } catch (error) {
      return {
        success: false,
        sampleRate: 0,
        error: error instanceof Error ? error.message : String(error)
      };
    }
  }
  async processFile(filePath, numClusters = -1, threshold = 0.5) {
    try {
      if (!this.initialized) {
        return {
          success: false,
          segments: [],
          numSpeakers: 0,
          durationMs: 0,
          error: 'Diarization is not initialized'
        };
      }
      return await this.api.processDiarizationFile({
        filePath: cleanFilePath(filePath),
        numClusters,
        threshold
      });
    } catch (error) {
      return {
        success: false,
        segments: [],
        numSpeakers: 0,
        durationMs: 0,
        error: error instanceof Error ? error.message : String(error)
      };
    }
  }
  async processFileWindow(filePath, startTimeMs, durationMs, numClusters = -1, threshold = 0.5) {
    try {
      if (!this.initialized) {
        return {
          success: false,
          segments: [],
          numSpeakers: 0,
          durationMs: 0,
          error: 'Diarization is not initialized'
        };
      }
      return await this.api.processDiarizationFileWindow({
        filePath: cleanFilePath(filePath),
        startTimeMs,
        durationMs,
        numClusters,
        threshold
      });
    } catch (error) {
      return {
        success: false,
        segments: [],
        numSpeakers: 0,
        durationMs: 0,
        error: error instanceof Error ? error.message : String(error)
      };
    }
  }
  async release() {
    try {
      const result = await this.api.releaseDiarization();
      if (result.released) {
        this.initialized = false;
        this.sampleRate = 0;
      }
      return result;
    } catch (error) {
      console.error('Error releasing Diarization resources:', error);
      return {
        released: false
      };
    }
  }
}
//# sourceMappingURL=DiarizationService.js.map