"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Meeting = exports.Events = exports.defaultConfig = void 0;
const events_1 = require("events");
const DyteControlBar_1 = __importDefault(require("./DyteControlBar"));
const DyteGrid_1 = __importDefault(require("./DyteGrid"));
const DyteParticipant_1 = require("./DyteParticipant");
const config_1 = require("../utils/config");
const defaultConfig_1 = require("../utils/defaultConfig");
const index_1 = require("../utils/dyte-utils/src/index");
const lodash_merge_1 = __importDefault(require("lodash.merge"));
const lodash_clonedeep_1 = __importDefault(require("lodash.clonedeep"));
exports.defaultConfig = {
    theme: Object.assign({}, defaultConfig_1.defaultTheme),
    permissions: Object.assign({}, defaultConfig_1.defaultRolePermissions),
    permissions_fetched: false,
};
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["localMediaConnected"] = "localMediaConnected";
})(Events = exports.Events || (exports.Events = {}));
class Meeting extends events_1.EventEmitter {
    constructor(setupConfig, uiConfig) {
        super();
        this.config = lodash_clonedeep_1.default(exports.defaultConfig);
        this.participants = [];
        this.showSetupScreen = false;
        this._uiConfig = {};
        this.leaveRoom = () => { };
        this.setupConfig = setupConfig;
        this._uiConfig = uiConfig || {};
        this.config.theme = index_1.convertThemeToV2(this.config.theme, uiConfig || {});
        this.controlBar = new DyteControlBar_1.default();
        this.grid = new DyteGrid_1.default();
        this.Events = Events;
        if (setupConfig === null || setupConfig === void 0 ? void 0 : setupConfig.apiBase) {
            config_1.setUrl(setupConfig.apiBase);
        }
    }
    updateUIConfig(config) {
        this._uiConfig = lodash_merge_1.default(this._uiConfig, config);
        this.config.theme = index_1.convertThemeToV2(this.config.theme, config);
        if (this._setUIConfig) {
            this._setUIConfig(this.config);
        }
    }
    addParticipantControl(control) {
        if (this._controlsHook) {
            this._controlsHook(control);
        }
    }
    sendRoomMessage(message) { }
    sendMessage(peerId, message) { }
    createPopup(element) { }
    kickAll() { }
    closePopup() {
        this.createPopup(null);
    }
    onError(_onError) {
        this._onError = _onError;
    }
    onSocketMessage(message) {
        var _a, _b;
        switch (message.type) {
            case 'websocket/room-message':
                this.emit(Events.roomMessage, (_a = message.payload) === null || _a === void 0 ? void 0 : _a.message);
                break;
            case 'websocket/message':
                this.emit(Events.message, (_b = message.payload) === null || _b === void 0 ? void 0 : _b.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 DyteParticipant_1.DyteSelfParticipant(this._overlayHook);
                participant._update(peer, hook);
                this.participants.push(participant);
                this.self = participant;
            }
            else {
                const participant = new DyteParticipant_1.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);
    }
}
exports.Meeting = Meeting;
//# sourceMappingURL=DyteMeeting.js.map