// @ts-nocheck
import { SpeechRecognizer } from "microsoft-cognitiveservices-speech-sdk";
import { EventEmitter } from "events";
import { ITranscriptionConfig, WebSocketConnectionOptions } from "./ITranscriptionService";

export { WebSocketConnectionOptions };

export interface ITranscriptionService extends EventEmitter {
  initialize(mediaStream: MediaStream): void;
  startTranscription(): void;
  stopTranscription(): Promise<void>;
  pauseTranscription(): void;
  resumeTranscription(): void;
}

export type SupportedLanguage = "pt-BR" | "en-US" | "es-ES";

export type Providers =
  | "sofya_compliance"
  | "oracle"
  | "sofya_as_service"
  | "sofya_whisper_flow"
  | "stt_wvad";

// Base configuration interface with common properties
export interface BaseConfig {
  language: SupportedLanguage;
}

export interface SofyaComplianceConfig extends BaseConfig {
  token: string;
  compartmentId: string;
  region: string;
}

export interface SofyaSpeechConfig extends BaseConfig {
  /**
   * Opções de conexão customizadas para WebSocket.
   *
   * ⚠️ AVISO DE SEGURANÇA: Query params são visíveis em logs e histórico.
   * Use 'protocols' para dados sensíveis como tokens de autenticação.
   *
   * @example
   * ```typescript
   * websocketOptions: {
   *   queryParams: {
   *     client_id: "app-123",
   *     session_id: "sess-456"
   *   },
   *   protocols: "Bearer eyJhbGciOiJIUzI1NiIs..."
   * }
   * ```
   */
  websocketOptions?: WebSocketConnectionOptions;
}

export type Connection =
  | { apiKey: string; config?: BaseConfig }
  | {
      provider: "sofya_compliance";
      endpoint: string;
      config: SofyaComplianceConfig;
    }
  | {
      provider: "sofya_as_service";
      endpoint: string;
      config: SofyaSpeechConfig;
    }
  | { provider: "stt_wvad"; endpoint: string; config: SofyaSpeechConfig };

export class SofyaTranscriber
  extends EventEmitter
  implements ITranscriptionService
{
  /**
   * Initializes a new instance of the SofyaTranscriber class.
   * @param connection The connection object required for authentication with the transcription service.
   */
  constructor(connection: Connection);

  /**
   * Starts the transcription process. This method listens for recognizing, recognized, and other relevant events.
   * @param mediaStream The MediaStream to be used for transcription.
   * Throws an error if the recognizer has not been initialized.
   */

  startTranscription(mediaStream: MediaStream): void;

  /**
   * Stops the ongoing transcription process.
   */
  stopTranscription(): Promise<void>;

  /**
   * Pauses the ongoing transcription process.
   */
  pauseTranscription(): void;

  /**
   * Resumes the ongoing transcription process.
   */
  resumeTranscription(): void;

  /**
   * Event emitted when speech is being recognized.
   * @event recognizing
   * @param text The text that is currently being recognized.
   */
  on(event: "recognizing", listener: (text: string) => void): this;

  /**
   * Event emitted when speech is successfully recognized.
   * @event recognized
   * @param text The recognized text.
   */
  on(event: "recognized", listener: (text: string) => void): this;

  /**
   * Event emitted when diarization is successfully recognized.
   * @event recognized_diarization
   * @param text The recognized diarization.
   */
  on(
    event: "recognized_diarization",
    listener: (
      diarization: Array<{
        end: number;
        sentence: string;
        speaker: string;
        start: number;
      }>
    ) => void
  ): this;

  /**
   * Event emitted when no match is found for the recognized speech.
   * @event nomatch
   */
  on(event: "nomatch", listener: () => void): this;

  /**
   * Event emitted when an error occurs during the transcription process.
   * @event error
   * @param error The error details.
   */
  on(event: "error", listener: (error: any) => void): this;

  /**
   * Event emitted when an the transcription service is ready to start.
   * @event ready
   */
  on(event: "ready", listener: () => void): this;

  /**
   * Event emitted when the transcription session stops.
   * @event stopped
   */
  on(event: "stopped", listener: () => void): this;
}
