/*! 
- Name HS_CTI
- FileName hs-cti
- Version 1.0.9 
- JS Standard any
- Author platformfe
- Built on 2024/12/14 14:32:24
- GitHub 
- Branch main
- CommitID aafd3d07aeaabaa0ad367764fe9b039205e63e6a
- CommitMessage fix:bug
*/
/**
 * Minimal `EventEmitter` interface that is molded against the Node.js
 * `EventEmitter` interface.
 */
declare class EventEmitter<
  EventTypes extends EventEmitter.ValidEventTypes = string | symbol,
  // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-constraint
  Context extends any = any
> {
  static prefixed: string | boolean

  /**
   * Return an array listing the events for which the emitter has registered
   * listeners.
   */
  eventNames(): Array<EventEmitter.EventNames<EventTypes>>

  /**
   * Return the listeners registered for a given event.
   */
  listeners<T extends EventEmitter.EventNames<EventTypes>>(
    event: T
  ): Array<EventEmitter.EventListener<EventTypes, T>>

  /**
   * Return the number of listeners listening to a given event.
   */
  listenerCount(event: EventEmitter.EventNames<EventTypes>): number

  /**
   * Calls each of the listeners registered for a given event.
   */
  emit<T extends EventEmitter.EventNames<EventTypes>>(
    event: T,
    ...args: EventEmitter.EventArgs<EventTypes, T>
  ): boolean

  /**
   * Add a listener for a given event.
   */
  on<T extends EventEmitter.EventNames<EventTypes>>(
    event: T,
    fn: EventEmitter.EventListener<EventTypes, T>,
    context?: Context
  ): this
  addListener<T extends EventEmitter.EventNames<EventTypes>>(
    event: T,
    fn: EventEmitter.EventListener<EventTypes, T>,
    context?: Context
  ): this

  /**
   * Add a one-time listener for a given event.
   */
  once<T extends EventEmitter.EventNames<EventTypes>>(
    event: T,
    fn: EventEmitter.EventListener<EventTypes, T>,
    context?: Context
  ): this

  /**
   * Remove the listeners of a given event.
   */
  removeListener<T extends EventEmitter.EventNames<EventTypes>>(
    event: T,
    fn?: EventEmitter.EventListener<EventTypes, T>,
    context?: Context,
    once?: boolean
  ): this
  off<T extends EventEmitter.EventNames<EventTypes>>(
    event: T,
    fn?: EventEmitter.EventListener<EventTypes, T>,
    context?: Context,
    once?: boolean
  ): this

  /**
   * Remove all listeners, or those of the specified event.
   */
  removeAllListeners(event?: EventEmitter.EventNames<EventTypes>): this
}

declare namespace EventEmitter {
  export interface ListenerFn<Args extends any[] = any[]> {
    (...args: Args): void
  }

  export interface EventEmitterStatic {
    new <
      EventTypes extends ValidEventTypes = string | symbol,
      Context = any
    >(): EventEmitter<EventTypes, Context>
  }

  /**
   * `object` should be in either of the following forms:
   * ```
   * interface EventTypes {
   *   'event-with-parameters': any[]
   *   'event-with-example-handler': (...args: any[]) => void
   * }
   * ```
   */
  export type ValidEventTypes = string | symbol | object

  export type EventNames<T extends ValidEventTypes> = T extends string | symbol
    ? T
    : keyof T

  export type ArgumentMap<T extends object> = {
    [K in keyof T]: T[K] extends (...args: any[]) => void
      ? Parameters<T[K]>
      : T[K] extends any[]
      ? T[K]
      : any[]
  }

  export type EventListener<
    T extends ValidEventTypes,
    K extends EventNames<T>
  > = T extends string | symbol
    ? (...args: any[]) => void
    : (
        ...args: ArgumentMap<Exclude<T, string | symbol>>[Extract<K, keyof T>]
      ) => void

  export type EventArgs<
    T extends ValidEventTypes,
    K extends EventNames<T>
  > = Parameters<EventListener<T, K>>

  export const EventEmitter: EventEmitterStatic
}

/** @enum LoggerLevels 日志输出等级 */
declare enum LoggerLevels {
    error = 0,
    warn = 1,
    log = 2,
    debug = 3
}
declare global {
    interface Window {
        ctiLoggerLevel?: LoggerLevels;
        HS_CTI_CLIENT_ID?: string;
    }
}
/** @class Logger 通用的日志模块 */
declare class Logger {
    private _level;
    private category;
    private label;
    constructor(level: LoggerLevels, category: string, label?: string);
    error(content: string): void;
    warn(content: string): void;
    log(content: string): void;
    debug(content: string): void;
    genericLog(levelToLog: LoggerLevels, content: string): void;
    private print;
    get level(): LoggerLevels;
    set level(newLevel: LoggerLevels);
}

/**
 * 默认 'test'
 * 'development': 本地(不上报服埋点只打印 log)
 * 'test': 测试
 * 'production': 生产
 */
type ENV = 'development' | 'test' | 'production';
/**
 * @enum {string} CTI 初始化场景
 * Manual: 手动外呼
 * Robot: 机器人外呼
 * Monitor: 监听
 * Predictive: 预测式外呼
 */
declare enum Scene {
    Manual = "manual",
    Robot = "robot",
    Monitor = "monitor",
    Predictive = "predictive",
    Wechat = "wechat"
}
type ExcludeScene = Scene.Manual | Scene.Robot | Scene.Predictive;
/**
 * @enum {string} CTI 监听场景
 * All: 不区分场景
 * Manual: 手动外呼
 * Robot: 机器人外呼
 * Predictive: 预测式外呼
 */
declare enum MonitorScene {
    All = "all",
    Manual = "manual",
    Robot = "robot",
    Predictive = "predictive"
}
/**
 * @interface {} 初始化 SD_CTI 的基本参数
 * agent_id: 坐席 ID
 * saas_id: 主体标识,例如 'hongshan'
 */
interface BaseOptions {
    agent_id: string;
    saas_id: string;
}
interface BaseHsCTIInitOptions extends BaseOptions {
    loggerLevel?: LoggerLevels;
    password: string;
    env: ENV;
}
interface HsCTIInitOptions extends BaseHsCTIInitOptions {
    scene: ExcludeScene;
}
interface RequiredHsCTIInitOptions extends BaseHsCTIInitOptions {
    scene: Scene.Monitor;
    monitorScene: MonitorScene;
}
/**
 * @interface {} 初始化 SD_CTI 需要的参数
 * loggerLevel: 日志等级
 * password: 临时的鉴权字符串,由业务方写死传进来
 * scene: CTI 初始化场景
 * monitorScene: 监听场景
 * env: 环境变量
 */
/**
 * @enum {string} Socket 状态
 * Initial: 初始状态
 * Connecting: Socket 开始建立连接
 * Connected: Socket 建立连接成功
 * Ready: 向 IM 发送第一个上行 login 消息收到成功回调
 * Terminated: Socket 连接断开、各种 Socket 错误流转到本状态
 */
declare enum SocketStatus {
    Initial = "Initial",
    Connecting = "Connecting",
    Connected = "Connected",
    Ready = "Ready",
    ReTry = "ReTry",
    Terminated = "Terminated"
}
/**
 * @enum {string} SIP 状态
 * Initial: 初始状态
 * Started: SIP 的 User Agent 创建成功
 * Connecting: SIP 底层 Socket 传输 TransportState.Connecting
 * Connected: SIP 底层 Socket 传输 TransportState.Connected
 * Ready: SIP Registerer 监听注册状态 RegistererState.Registered
 * Terminated: SIP Socket 断开、注册失败等各种错误、主动断开连接流转到本状态
 */
declare enum SIPStatus {
    Initial = "Initial",
    Started = "Started",
    Connecting = "Connecting",
    Connected = "Connected",
    Ready = "Ready",
    ReTry = "ReTry",
    Terminated = "Terminated"
}
/**
 * @enum {string} CTI 状态
 * Initial: 初始状态
 * Ready: SocketStatus Ready && SIPStatus Ready
 * Terminated: SocketStatus Terminated || SIPStatus Terminated || 正常调用 unInit 方法卸载
 */
declare enum CTIStatus {
    Initial = "Initial",
    Ready = "Ready",
    ReTry = "ReTry",
    Terminated = "Terminated"
}
/**
 * @enum {string} 通话状态
 * 为了防止人工外呼方法被二次调用引发预期以外的问题,增加此状态的流转
 * Started: 外呼已开始,此状态下不允许再次发起外呼
 * Stopped: 外呼已结束,此状态下可以再次发起外呼
 */
declare enum CallStatus {
    Started = "Started",
    Stopped = "Stopped"
}
/**
 * @enum {string} Session 状态
 * CTI 目前只有 Invitation(接受会话)的场景,Inviter (主动发起会话)暂时没有
 */
declare enum SessionStatus {
    /**
     * If `Inviter`, INVITE not sent yet.
     * If `Invitation`, SDK 收到 INVITE 通话请求,但尚未处理.
     */
    Initial = "Initial",
    /**
     * If `Inviter`, sent INVITE and waiting for a final response.
     * If `Invitation`, received INVITE and attempting to send 200 final response (but has not sent it yet).
     */
    Establishing = "Establishing",
    /**
     * If `Inviter`, sent INVITE and received 200 final response and sent ACK.
     * If `Invitation`, SDK 完成接受 INVITE 并发送 200 OK 确认接起,同时接通本地语音流.
     */
    Established = "Established",
    /**
     * If `Inviter`, sent INVITE, sent CANCEL and now waiting for 487 final response to ACK (or 200 to ACK & BYE).
     * If `Invitation`, received INVITE, sent 200 final response and now waiting on ACK and upon receipt will attempt BYE
     * (as the protocol specification requires, before sending a BYE we must receive the ACK - so we are waiting).
     */
    Terminating = "Terminating",
    /**
     * If `Inviter`, sent INVITE and received non-200 final response (or sent/received BYE after receiving 200).
     * If `Invitation`, SDK 收到 BYE 信令,发送 200 OK 挂断确认,会话结束.
     */
    Terminated = "Terminated"
}
/**
 * @enum {string} CTI 所有错误的分类
 * SdkTerminated: SDK 不可用错误,需要重新初始化
 * SdkError: SDK 状态可用,其他普通错误
 * ServerTerminated: 服务端不可用错误,需要重新初始化,透传服务端 code,msg
 * ServerError: 服务端可用,普通错误,透传服务端 code,msg
 */
declare enum CTIErrorType {
    SdkTerminated = "SdkTerminated",
    SdkError = "SdkError",
    ServerTerminated = "ServerTerminated",
    ServerError = "ServerError"
}
/**
 * @enum {string} SdkTerminated 类型错误的 code 枚举
 * CTITerminated: SDK 状态不可用,CTIStatus 的状态为 Terminated
 * GetUserMedia: 获取坐席媒体权限失败
 * GetInitConfig: 调接口获取 CTI 初始化配置失败
 * SocketOnError: 监听 socket.io 的 error 事件
 * SocketOnConnectError: 监听 socket.io 的 connect_error 事件
 * SocketOnDisconnect: 监听 socket.io 的 disconnect 事件
 * SocketRepeatLogin: 多页面重复登录,IM 互踢事件
 * SIPInitUserAgent: SIP UserAgent 初始化时启动失败
 * SIPInitRegister: SIP Register 初始化时注册失败
 * SIPUserAgentStateStopped: 监听 SIP UserAgent stateChange 事件状态变更为 Stopped
 * SIPTransportStateDisconnected: 监听 SIP Transport StateChange 事件状态变更为 Disconnect
 * SIPRegistererStateTerminated: 监听 SIP Registerer StateChange 事件状态变更为 Terminated
 * SIPOnDisconnect: 监听 SIP OnDisconnect 事件收到异常退出 error
 * SIPInitTransport: SIP Transport 初始化时连接失败
 * SipHeartBeatErr: SIP发送心跳OPTIONS事件时收到异常结果
 * SIPUnRegistered: 注册SIP时失败
 * SocketOnReconnectFailed: Socket重连超过阈值且依然重连失败
 */
declare enum HskTerminatedCode {
    CTITerminated = "100001",
    GetUserMedia = "100002",
    GetInitConfig = "100003",
    SocketOnError = "110001",
    SocketOnConnectError = "110002",
    SocketOnDisconnect = "110003",
    SocketRepeatLogin = "110004",
    SocketOnReconnectFailed = "110007",
    SIPInitUserAgent = "120001",
    SIPInitRegister = "120002",
    SIPUserAgentStateStopped = "120003",
    SIPTransportStateDisconnected = "120004",
    SIPRegistererStateTerminated = "120005",
    SIPOnDisconnect = "120006",
    SIPInitTransport = "120007",
    SipHeartBeatErr = "120008",
    SIPUnRegistered = "120009"
}
/**
 * @enum {string} SdkError 类型错误的 code 枚举
 * Answer: SIP accept 接起失败
 * Bye: SIP bye 挂断失败
 * InvitationCancel: SIP Invitation 会话请求被取消
 * AssignStream: 播放语音流失败
 * FetchError: 修饰器handleApiRes当进入到catch时上报此code
 */
declare enum SdkErrorCode {
    Answer = "200001",
    Bye = "200002",
    InvitationCancel = "200003",
    AssignStream = "200004",
    FetchError = "200005"
}
/**
 * @enum {string} CTI 事件推送
 * OnCtiError: CTI 错误事件,含前后端所有错误,SDK 推送
 * OnSessionStatusChange: 坐席侧 SIP 会话状态变更事件,SDK 推送
 * OnInitalSuccess: CTI 初始化成功事件,SDK 推送
 * OnAgentWorkReport: 坐席&用户状态变更事件,Server 推送
 * OnRingStart: 手动外呼用户未接听时,开始播放回铃音,Server 推送
 * OnRingEnd: 手动外呼用户未接听时,播放回铃音结束,Server 推送
 * OnAgentReport: 坐席状态变更事件,Server 推送
 * OnCallReportInfo: 通话时长及通话次数等信息,Server 推送
 * OnDetectedTone: 服务端收到音频信号后推送
 */
declare enum CTIEvent {
    OnCtiError = "OnCtiError",
    OnSessionStatusChange = "OnSessionStatusChange",
    OnInitalSuccess = "OnInitalSuccess",
    OnAgentWorkReport = "OnAgentWorkReport",
    OnRingStart = "OnRingStart",
    OnRingEnd = "OnRingEnd",
    OnDetectedTone = "OnDetectedTone",
    OnAgentReport = "OnAgentReport",
    OnCallReportInfo = "OnCallReportInfo",
    OnCallRing = "OnCallRing",
    OnCallEnd = "OnCallEnd",
    OnCallAnswer = "OnCallAnswer",
    OnAgentGroupQuery = "OnAgentGroupQuery",
    OnMethodResponseEvent = "OnMethodResponseEvent",
    OnEventPrompt = "OnEventPrompt",
    OnPrompt = "OnPrompt"
}
/** @interface CTIRes CTI 对外方法暴露 promise 结果 */
interface CTIRes {
    code: number;
    data: any;
    msg: string;
}

interface CTIManualCallOptions {
    called: string;
    caller: string;
    ext?: object;
}

/** @class HsCTI 红杉外呼类 */
declare class HsCTI extends EventEmitter {
    private logger;
    loggerLevel: LoggerLevels;
    /** HsCTI 实例 */
    static instance: HsCTI | undefined;
    scene: Scene;
    agent_id: string;
    saas_id: string;
    /** 接口返回的初始化配置 */
    private _initOptions;
    /** IM socket的实例 */
    private _socket;
    /** sip.js UA实例 */
    private _sipUserAgent;
    private _callId;
    private _ctiFlowIdList;
    /** 基本参数 */
    private _baseParams;
    /** 等待提示音播放器 */
    private _waitAudio;
    /** 振铃提示音播放器 */
    private _ringAudio;
    /** 结束通话提示音 */
    private _byeAudio;
    /** 远端音频流播放器 */
    private _remoteAudio;
    /** CTI状态 */
    private _ctiStatus;
    private _ctiStatusList;
    /** sip状态 */
    private _sipStatus;
    private _sipStatusList;
    /** socket状态 */
    private _socketStatus;
    private _socketStatusList;
    private _callStatus;
    private _terminatedStatusList;
    private constructor();
    get getCTIStatus(): CTIStatus;
    get getSocketStatus(): SocketStatus;
    get getSIPStatus(): SIPStatus;
    static getInstance(hsCTIInitOptions: HsCTIInitOptions | RequiredHsCTIInitOptions): HsCTI;
    /** @private 重置实例 */
    private initInstanceOptions;
    /** @private 优雅关闭 SIP 和 socket */
    private clearSocketAndSip;
    /**
     * @private 设置等待音 src
     * @param {AudioName} audioName
     * @param {boolean} loop
     */
    private setAudioSrc;
    private playAudio;
    private stopLocalAudio;
    private stopAudio;
    init(): void;
    private getInitConfig;
    private initSocket;
    private handleSocketDownEvent;
    /** @private serverEventEmit 统一处理服务端推送的事件 */
    private serverEventEmit;
    private initSip;
    private sipDelegate;
    private sessionStateChangeAndTrack;
    /**
     * @private setCTIStatus CTI 状态流转
     * @param {CTIStatus} ctiStatus
     */
    private setCTIStatus;
    /**
     * @private setSocketStatus Socket 状态流转
     * @param SocketStatusChangeParams Socket 状态流转参数及错误详情等
     */
    private setSocketStatus;
    /**
     * @private setSipStatus SIP 状态流转
     * @param SIPStatusChangeParams SIP 状态流转参数及错误详情等
     */
    private setSipStatus;
    /**
     * @private socketOrSipStatusChange Socket 或 SIP 状态变化可能引起 CTI 状态变化
     * @param {SocketStatus} socketStatus
     * @param {SIPStatus} sipStatus
     */
    private socketOrSipStatusChange;
    /**
     * @private eventEmitAndTrack SDK 对外抛出事件并统一埋点
     * @param {CTIEvent} eventName 事件名称
     * @param {object} ext 事件参数
     * @param { error } error 错误详情或错误辅助信息
     */
    private eventEmitAndTrack;
    /** @private checkIn 服务端签入,CTIStatus Ready 时自动调用,坐席状态变更 */
    private checkIn;
    /** @public checkOut 服务端签出, unInit 时自动调用,坐席状态变更 */
    checkOut(): Promise<CTIRes>;
    /** @private _getCtiFlowId 获取手动外呼场景需要的 ctiFlowId */
    private getCtiFlowId;
    /** @public setIdle 服务端置闲,坐席状态变更 */
    setIdle(): Promise<CTIRes>;
    /** @public setBusy 服务端置忙,坐席状态变更 */
    setBusy(): Promise<CTIRes>;
    makeCall(params: CTIManualCallOptions): Promise<CTIRes | undefined>;
    private serverCall;
    /** @public answer SDK SIP 接起 */
    answer(): Promise<CTIRes>;
    /** @public bye SDK SIP 挂断 */
    bye(): Promise<CTIRes>;
    /** @public serverBye 挂断且流转坐席状态 */
    serverBye(): Promise<CTIRes>;
    /** @public turnHang 流转坐席状态-通话结束 */
    private turnHang;
    /** @public getAgentStatus 获取坐席状态 */
    getAgentStatus(): Promise<CTIRes>;
    /**
     * @public loadAgentGroupData 监听-根据监听组 ID 获取监听组成员
     * @param {string[]} monitorIds
     */
    loadAgentGroupData(monitorIds: string[]): Promise<CTIRes>;
    /**
     * @public listen 监听-服务端发起监听
     * @param {string} monitoredAgNo
     */
    listen(monitoredAgNo: string): Promise<CTIRes>;
    /**
     * @public setActiveService 机器人外呼-签入人工组
     * @param {string} serviceId
     */
    setActiveService(serviceId: string): Promise<CTIRes>;
    /** @public unInit 卸载 SDK,checkOut 成功后断开 socket 和 sip 连接,并销毁 SdCTI 实例 */
    unInit(): Promise<void>;
}

/**
 * @function getInstance 获取 HsCTI 的实例
 * @param HsCTIInitOptions 初始化 HsCTI 的配置
 */
declare const getInstance: (HsCTIInitOptions: HsCTIInitOptions | RequiredHsCTIInitOptions) => HsCTI;

export { BaseHsCTIInitOptions, BaseOptions, CTIErrorType, CTIEvent, CTIRes, CTIStatus, CallStatus, ENV, ExcludeScene, HsCTI, HsCTIInitOptions, HskTerminatedCode, Logger, LoggerLevels, MonitorScene, RequiredHsCTIInitOptions, SIPStatus, Scene, SdkErrorCode, SessionStatus, SocketStatus, getInstance };