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);
import { useLoginState, useRoomEngine, useRoomState, useDeviceState, RoomType } from "tuikit-atomicx-vue3/room";
import useRoomLifeCycle from "../hooks/useRoomLifeCycle.mjs";
import { eventCenter } from "../utils/eventCenter.mjs";
class Conference {
  constructor() {
    __publicField(this, "componentConfig", []);
  }
  login(params) {
    const { login } = useLoginState();
    return login({
      userId: params.userId,
      userSig: params.userSig,
      sdkAppId: params.sdkAppId
    });
  }
  async logout() {
    const { logout } = useLoginState();
    return logout();
  }
  getRoomEngine() {
    const roomEngine = useRoomEngine();
    if (!roomEngine) {
      console.warn("getRoomEngine failed, roomEngine is not exist");
    }
    return roomEngine;
  }
  on(eventType, callback) {
    eventCenter.on(eventType, callback);
  }
  off(eventType, callback) {
    eventCenter.off(eventType, callback);
  }
  async start({ roomId, roomType, options }) {
    const { createAndJoinRoom } = useRoomState();
    const {
      openLocalCamera,
      openLocalMicrophone,
      setCurrentCamera,
      setCurrentMicrophone,
      setCurrentSpeaker
    } = useDeviceState();
    const {
      roomName,
      isOpenCamera = false,
      isOpenMicrophone = false,
      defaultCameraId,
      defaultMicrophoneId,
      defaultSpeakerId,
      password
    } = options || {};
    const { isJoiningRoom, handleJoinRoomError } = useRoomLifeCycle();
    isJoiningRoom.value = true;
    try {
      await createAndJoinRoom({
        roomId,
        roomType: roomType || RoomType.Standard,
        options: {
          roomName,
          password
        }
      });
    } catch (error) {
      isJoiningRoom.value = false;
      handleJoinRoomError(error);
      throw error;
    }
    isJoiningRoom.value = false;
    if (defaultCameraId) {
      await setCurrentCamera({ deviceId: defaultCameraId });
    }
    if (defaultMicrophoneId) {
      await setCurrentMicrophone({ deviceId: defaultMicrophoneId });
    }
    if (defaultSpeakerId) {
      await setCurrentSpeaker({ deviceId: defaultSpeakerId });
    }
    if (isOpenCamera) {
      try {
        await openLocalCamera();
      } catch (error) {
        console.warn("Failed to open camera:", error);
      }
    }
    if (isOpenMicrophone) {
      try {
        await openLocalMicrophone();
      } catch (error) {
        console.warn("Failed to open microphone:", error);
      }
    }
  }
  async join({ roomId, roomType, options }) {
    const { joinRoom } = useRoomState();
    const {
      openLocalCamera,
      openLocalMicrophone,
      setCurrentCamera,
      setCurrentMicrophone,
      setCurrentSpeaker
    } = useDeviceState();
    const {
      isOpenCamera = false,
      isOpenMicrophone = false,
      defaultCameraId,
      defaultMicrophoneId,
      defaultSpeakerId,
      password
    } = options || {};
    const { isJoiningRoom, joiningRoomId, handleJoinRoomError } = useRoomLifeCycle();
    isJoiningRoom.value = true;
    joiningRoomId.value = roomId;
    try {
      await joinRoom({
        roomId,
        roomType: roomType || RoomType.Standard,
        password
      });
    } catch (error) {
      isJoiningRoom.value = false;
      handleJoinRoomError(error);
      return;
    }
    isJoiningRoom.value = false;
    joiningRoomId.value = "";
    if (defaultCameraId) {
      await setCurrentCamera({ deviceId: defaultCameraId });
    }
    if (defaultMicrophoneId) {
      await setCurrentMicrophone({ deviceId: defaultMicrophoneId });
    }
    if (defaultSpeakerId) {
      await setCurrentSpeaker({ deviceId: defaultSpeakerId });
    }
    if (isOpenCamera) {
      try {
        await openLocalCamera();
      } catch (error) {
        console.warn("Failed to open camera:", error);
      }
    }
    if (isOpenMicrophone) {
      try {
        await openLocalMicrophone();
      } catch (error) {
        console.warn("Failed to open microphone:", error);
      }
    }
  }
  async leave() {
    const { leaveRoom } = useRoomState();
    await leaveRoom();
  }
  async dismiss() {
    const { endRoom } = useRoomState();
    await endRoom();
  }
  setSelfInfo(options) {
    const { setSelfInfo } = useLoginState();
    return setSelfInfo(options);
  }
  setComponentConfig(config) {
    const index = this.componentConfig.findIndex((item) => item.componentName === config.componentName);
    if (index !== -1) {
      this.componentConfig[index] = config;
    } else {
      this.componentConfig.push(config);
    }
  }
  getComponentConfig(name) {
    return this.componentConfig.find((item) => item.componentName === name);
  }
  // public disableTextMessaging() {
  //   roomService.dataReportManager.reportCount(MetricsKey.disableTextMessaging);
  //   roomService.setComponentConfig({ ChatControl: { visible: false } });
  // }
  // public disableScreenSharing() {
  //   roomService.dataReportManager.reportCount(MetricsKey.disableScreenSharing);
  //   roomService.setComponentConfig({ ScreenShare: { visible: false } });
  // }
  // public enableWatermark() {
  //   roomService.dataReportManager.reportCount(MetricsKey.enableWatermark);
  //   roomService.waterMark.toggleWatermark(true);
  // }
  // public enableVirtualBackground() {
  //   roomService.dataReportManager.reportCount(
  //     MetricsKey.enableVirtualBackground
  //   );
  //   roomService.setComponentConfig({ VirtualBackground: { visible: true } });
  // }
  // public hideFeatureButton(name: FeatureButton) {
  //   roomService.dataReportManager.reportCount(MetricsKey.hideFeatureButton);
  //   roomService.setComponentConfig({ [name]: { visible: false } });
  // }
  // public replaceFriendList(userList: Array<any>) {
  //   return roomService.scheduleConferenceManager.replaceFriendList(userList);
  // }
  // public setUserListSortComparator(comparator: Comparator<UserInfo>) {
  //   roomService.userManager.setUserListSortComparator(comparator);
  // }
  // public setStreamListSortComparator(comparator: Comparator<StreamInfo>) {
  //   roomService.userManager.setStreamListSortComparator(comparator);
  // }
  // public setParticipants(
  //   participants: Array<{
  //     userName: string;
  //     userId: string;
  //     avatarUrl: string;
  //   }>
  // ) {
  //   const list = participants.map(item => {
  //     const { userId, userName, avatarUrl } = item;
  //     return {
  //       userID: userId,
  //       profile: { userID: userId, nick: userName, avatar: avatarUrl },
  //     };
  //   });
  //   roomService.scheduleConferenceManager.replaceFriendList(list);
  // }
}
const conference = new Conference();
export {
  conference
};
