import { EventEmitter } from 'events';
import DyteControlBar from '../lib/DyteControlBar';
import DyteGrid from '../lib/DyteGrid';
import { DyteParticipant, DyteSelfParticipant } from '../lib/DyteParticipant';
import { setUrl } from '../utils/config';
import { defaultRolePermissions, defaultTheme } from '../utils/defaultConfig';
import { convertThemeToV2 } from '../utils/dyte-utils/src/index';
import { merge } from 'lodash';
export const defaultConfig = {
    theme: { ...defaultTheme }, permissions: { ...defaultRolePermissions },
};
export var Events;
(function (Events) {
    Events["participantJoin"] = "participantJoin";
    Events["participantLeave"] = "participantLeave";
    Events["disconnect"] = "disconnect";
    Events["connect"] = "connect";
    Events["meetingEnded"] = "meetingEnded";
    Events["roomMessage"] = "roomMessage";
    Events["chatMessage"] = "chatMessage";
    Events["message"] = "message";
    Events["meetingJoined"] = "meetingJoined";
    Events["activeSpeaker"] = "activeSpeaker";
    Events["participantUpdate"] = "participantUpdate";
    Events["recordingStarted"] = "recordingStarted";
    Events["recordingStopped"] = "recordingStopped";
})(Events || (Events = {}));
export class Meeting extends EventEmitter {
    constructor(setupConfig, uiConfig) {
        super();
        this.config = defaultConfig;
        this.participants = [];
        this.showSetupScreen = false;
        this._uiConfig = {};
        this.leaveRoom = () => { };
        this.setupConfig = setupConfig;
        this._uiConfig = uiConfig;
        this.config.theme = convertThemeToV2(this.config.theme, uiConfig || {});
        this.controlBar = new DyteControlBar();
        this.grid = new DyteGrid();
        this.Events = Events;
        if (setupConfig?.apiBase) {
            setUrl(setupConfig.apiBase);
        }
    }
    updateUIConfig(config) {
        this._uiConfig = merge(this._uiConfig, config);
        this.config.theme = convertThemeToV2(this.config.theme, config);
        if (this._setUIConfig) {
            this._setUIConfig({});
        }
    }
    addParticipantControl(control) {
        if (this._controlsHook) {
            this._controlsHook(control);
        }
    }
    sendRoomMessage(message) {
    }
    sendMessage(peerId, message) {
    }
    createPopup(element) {
    }
    closePopup() {
        this.createPopup(null);
    }
    onError(_onError) {
        this._onError = _onError;
    }
    onSocketMessage(message) {
        switch (message.type) {
            case 'websocket/room-message':
                this.emit(Events.roomMessage, message.payload?.message);
                break;
            case 'websocket/message':
                this.emit(Events.message, message.payload?.message);
                break;
            case 'websocket/new-chat-message':
                this.emit(Events.chatMessage, message.payload);
                break;
            default:
                break;
        }
    }
    _addUpdateParticipant(peer, hook) {
        if (!peer.id)
            return;
        const index = this.participants.findIndex((p) => p.id === peer.id);
        if (index === -1) {
            if (peer.isMe) {
                const participant = new DyteSelfParticipant(this._overlayHook);
                participant._update(peer, hook);
                this.participants.push(participant);
                this.self = participant;
            }
            else {
                const participant = new DyteParticipant(this._overlayHook);
                participant._update(peer, hook);
                this.participants.push(participant);
                this.emit(Events.participantJoin, participant);
            }
        }
        else {
            this.participants[index]._update(peer, hook);
            this.emit(Events.participantUpdate, this.participants[index]);
        }
    }
    _removeParticipant(peer) {
        const participant = this.participants.find((p) => p.id === peer.id);
        this.participants = this.participants.filter((p) => p.id !== peer.id);
        this.emit(Events.participantLeave, participant);
    }
}
//# sourceMappingURL=DyteMeeting.js.map