"use strict";
var __defProp = Object.defineProperty;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const TUIRoomEngine = require("@tencentcloud/tuiroom-engine-js");
const types = require("../types.js");
const environment = require("../../utils/environment.js");
class ErrorHandler {
  constructor(service) {
    __publicField(this, "service");
    __publicField(this, "handlerMap", {
      createRoom: this.handleCreateRoomError.bind(this),
      enterRoom: this.handleEnterRoomError.bind(this),
      onError: this.handleOnError.bind(this)
    });
    __publicField(this, "mediaDeviceErrorHandler", null);
    this.service = service;
  }
  handleError(error, functionName) {
    this.handlerMap[functionName] && this.handlerMap[functionName](error);
  }
  handleEnterRoomError(error) {
    if (error.code === TUIRoomEngine.TUIErrorCode.ERR_NEED_PASSWORD) {
      this.service.emit(types.EventType.ROOM_NEED_PASSWORD, error.code);
      return;
    }
    if (error.code === TUIRoomEngine.TUIErrorCode.ERR_WRONG_PASSWORD) {
      this.service.emit(types.EventType.ROOM_NOTICE_MESSAGE, {
        type: "error",
        message: this.service.t("Wrong password, please re-enter")
      });
      return;
    }
    let message = "";
    switch (error.code) {
      case TUIRoomEngine.TUIErrorCode.ERR_ROOM_ID_NOT_EXIST:
        message = "The room does not exist, please confirm the room number or create a room!";
        break;
      default:
        message = "Failed to enter the meeting";
    }
    this.service.emit(types.EventType.ROOM_NOTICE_MESSAGE_BOX, {
      title: this.service.t("Note"),
      message: this.service.t(message),
      confirmButtonText: this.service.t("Sure"),
      callback: async () => {
        this.service.emit(types.EventType.ROOM_ERROR, {
          code: error.code,
          message: error.message
        });
        this.service.roomStore.reset();
      }
    });
  }
  handleCreateRoomError(error) {
    this.service.emit(types.EventType.ROOM_NOTICE_MESSAGE_BOX, {
      title: this.service.t("Note"),
      message: this.service.t("Failed to initiate meeting"),
      confirmButtonText: this.service.t("Sure"),
      callback: async () => {
        this.service.emit(types.EventType.ROOM_ERROR, {
          code: error.code,
          message: error.message
        });
        this.service.resetStore();
      }
    });
  }
  handleOnError(error) {
    if (error.message === "enter trtc room failed , error code : -1") {
      this.service.emit(types.EventType.ROOM_NOTICE_MESSAGE_BOX, {
        type: "warning",
        message: this.service.t("Failed to enter the meeting"),
        callback: () => {
          this.service.emit(types.EventType.ROOM_ERROR, {
            code: error.code,
            message: error.message
          });
          this.service.resetStore();
        }
      });
    }
    if (error.code && String(error.code).indexOf("-11") === 0) {
      if (!this.mediaDeviceErrorHandler) {
        this.mediaDeviceErrorHandler = new MediaDeviceErrorHandler(
          this.service
        );
      }
      this.mediaDeviceErrorHandler.handleError(error);
    }
  }
}
class MediaDeviceErrorHandler {
  constructor(service) {
    __publicField(this, "service");
    this.service = service;
  }
  getMediaDeviceErrorTitle(deviceType) {
    return this.service.t("Unable to use the device", {
      deviceType: this.service.t(deviceType)
    });
  }
  getMediaDeviceErrorMsg(deviceType) {
    return this.service.t(
      "media device failed to open, please check the media device and try again",
      { deviceType: this.service.t(deviceType) }
    );
  }
  getMediaDeviceNotAuthorizedMsg(deviceType) {
    return this.service.t(
      "The current device is not authorized, please allow access to the device permissions",
      { deviceType: this.service.t(deviceType) }
    );
  }
  getMediaDeviceOccupiedMsg(deviceType) {
    return this.service.t(
      "The current device is occupied by other apps, try to close other apps or change the device",
      { deviceType: this.service.t(deviceType) }
    );
  }
  getTurnOnDevicePrivilegesTitle(deviceType) {
    return this.service.t("Turn on device privileges", {
      deviceType: this.service.t(deviceType)
    });
  }
  getTurnOnDevicePrivilegesMsg(deviceType) {
    return this.service.t(
      'You can go to "System Preferences - Security & Privacy - Device" to enable device permissions',
      { deviceType: this.service.t(deviceType) }
    );
  }
  handleError(error) {
    let mediaType;
    let mediaErrorTitle = "";
    let mediaErrorMsg = "";
    let cancelButtonText = "";
    let confirmButtonText = "";
    let callback = null;
    switch (error.code) {
      case TUIRoomEngine.TUIErrorCode.ERR_CAMERA_START_FAILED:
      case TUIRoomEngine.TUIErrorCode.ERR_MICROPHONE_START_FAILED:
        mediaType = error.code === TUIRoomEngine.TUIErrorCode.ERR_CAMERA_START_FAILED ? "camera" : "microphone";
        mediaErrorTitle = this.getMediaDeviceErrorTitle(mediaType);
        mediaErrorMsg = this.getMediaDeviceErrorMsg(mediaType);
        break;
      case TUIRoomEngine.TUIErrorCode.ERR_CAMERA_NOT_AUTHORIZED:
      case TUIRoomEngine.TUIErrorCode.ERR_MICROPHONE_NOT_AUTHORIZED:
        mediaType = error.code === TUIRoomEngine.TUIErrorCode.ERR_CAMERA_NOT_AUTHORIZED ? "camera" : "microphone";
        if (environment.isElectron) {
          mediaErrorTitle = this.getTurnOnDevicePrivilegesTitle(mediaType);
          mediaErrorMsg = this.getTurnOnDevicePrivilegesMsg(mediaType);
          cancelButtonText = this.service.t("Cancel");
          confirmButtonText = this.service.t("Go to Settings");
          callback = (action) => {
            if (action === "confirm") {
              this.goToPermissionSettings(mediaType);
            }
          };
        } else {
          mediaErrorTitle = this.getMediaDeviceErrorTitle(mediaType);
          mediaErrorMsg = this.getMediaDeviceNotAuthorizedMsg(mediaType);
        }
        break;
      case TUIRoomEngine.TUIErrorCode.ERR_CAMERA_OCCUPIED:
      case TUIRoomEngine.TUIErrorCode.ERR_MICROPHONE_OCCUPIED:
        mediaType = error.code === TUIRoomEngine.TUIErrorCode.ERR_CAMERA_OCCUPIED ? "camera" : "microphone";
        mediaErrorTitle = this.getMediaDeviceErrorTitle(mediaType);
        mediaErrorMsg = this.getMediaDeviceOccupiedMsg(mediaType);
        break;
    }
    if (mediaErrorMsg) {
      this.service.emit(types.EventType.ROOM_NOTICE_MESSAGE_BOX, {
        type: "error",
        title: mediaErrorTitle,
        message: mediaErrorMsg,
        cancelButtonText,
        confirmButtonText: confirmButtonText || this.service.t("I got it"),
        callback
      });
    }
  }
  goToPermissionSettings(deviceType) {
    const shell = { openExternal: Function };
    const privacyTypeObj = {
      camera: "Privacy_Camera",
      microphone: "Privacy_Microphone",
      screenShare: "Privacy_ScreenCapture"
    };
    shell.openExternal(
      `x-apple.systempreferences:com.apple.preference.security?${privacyTypeObj[deviceType]}`
    );
  }
}
exports.ErrorHandler = ErrorHandler;
