"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.setPeerLabel = exports.peerMetadataUpdated = exports.switchToWebinar = exports.peerStoppedPresenting = exports.peerStartedPresenting = exports.peerRemovedFromStage = exports.requestToJoinStageRejected = exports.requestToJoinStageAccepted = exports.setPeerVisibility = exports.selectedPeers = exports.getPage = exports.peerUnmuted = exports.peerMuted = exports.resetPeers = exports.peerClosed = exports.peerJoined = exports.setRoomState = exports.roomJoined = void 0;
const toolkit_1 = require("@reduxjs/toolkit");
exports.roomJoined = toolkit_1.createAction('session/join-room');
exports.setRoomState = toolkit_1.createAction('websocket/room-state');
exports.peerJoined = toolkit_1.createAction('websocket/peer-joined');
exports.peerClosed = toolkit_1.createAction('websocket/peer-closed');
exports.resetPeers = toolkit_1.createAction('session/reset-peers');
exports.peerMuted = toolkit_1.createAction('websocket/peer-muted');
exports.peerUnmuted = toolkit_1.createAction('websocket/peer-unmuted');
exports.getPage = toolkit_1.createAction('websocket/get-page');
exports.selectedPeers = toolkit_1.createAction('websocket/selected-peers');
exports.setPeerVisibility = toolkit_1.createAction('websocket/peer-visibility');
exports.requestToJoinStageAccepted = toolkit_1.createAction('websocket/peer-added-to-stage');
exports.requestToJoinStageRejected = toolkit_1.createAction('websocket/peer-rejected-to-join-stage');
exports.peerRemovedFromStage = toolkit_1.createAction('websocket/peer-removed-from-stage');
exports.peerStartedPresenting = toolkit_1.createAction('websocket/peer-started-presenting');
exports.peerStoppedPresenting = toolkit_1.createAction('websocket/peer-stopped-presenting');
exports.switchToWebinar = toolkit_1.createAction('session/switch-webinar');
exports.peerMetadataUpdated = toolkit_1.createAction('websocket/metadata-update');
exports.setPeerLabel = toolkit_1.createAction('session/peer-label');
const initialState = {
    joinedPeers: [],
    currentPagePeerIds: [],
    activeSpeakerPagePeerIds: [],
    recordingPeer: null,
};
const peersReducer = toolkit_1.createReducer(initialState, {
    [exports.resetPeers.toString()]: (state) => {
        state.joinedPeers = [];
        state.recordingPeer = null;
    },
    [exports.getPage.toString()]: (state, action) => {
        state.currentPagePeerIds = [...action.payload.peerIds];
    },
    [exports.selectedPeers.toString()]: (state, action) => {
        state.activeSpeakerPagePeerIds = [...action.payload.peerIds];
    },
    [exports.setPeerVisibility.toString()]: (state, action) => {
        const peer = state.joinedPeers.find(({ id }) => id === action.payload.peerId);
        if (peer) {
            peer.hiddenParticipant = action.payload.visibility;
            peer.flags.hiddenParticipant = action.payload.visibility;
            state.joinedPeers = state.joinedPeers.filter((p) => p.id !== peer.id).concat([Object.assign({}, peer)]);
        }
    },
    [exports.roomJoined.toString()]: (state, action) => {
        const { peers } = action.payload;
        state.recordingPeer = peers
            ? peers.find((p) => { var _a; return (_a = p.flags) === null || _a === void 0 ? void 0 : _a.recorder; })
            : null;
        state.joinedPeers = peers
            ? peers.filter((p) => { var _a; return !((_a = p.flags) === null || _a === void 0 ? void 0 : _a.recorder); })
            : [];
    },
    [exports.setRoomState.toString()]: (state, action) => {
        const { peers } = action.payload.roomState;
        state.recordingPeer = peers
            ? peers.find((p) => { var _a; return (_a = p.flags) === null || _a === void 0 ? void 0 : _a.recorder; })
            : null;
        state.joinedPeers = peers
            ? peers.filter((p) => { var _a; return !((_a = p.flags) === null || _a === void 0 ? void 0 : _a.recorder); })
            : [];
    },
    [exports.peerJoined.toString()]: (state, action) => {
        var _a;
        const peer = action.payload;
        if ((_a = peer.flags) === null || _a === void 0 ? void 0 : _a.recorder) {
            state.recordingPeer = peer;
        }
        else {
            state.joinedPeers = state.joinedPeers
                .filter((p) => p.id !== peer.id)
                .concat(peer);
        }
    },
    [exports.peerClosed.toString()]: (state, action) => {
        var _a;
        const peer = action.payload;
        if (((_a = state.recordingPeer) === null || _a === void 0 ? void 0 : _a.id) === peer.id) {
            state.recordingPeer = null;
        }
        else {
            state.joinedPeers = state.joinedPeers.filter((p) => p.id !== peer.id);
        }
    },
    [exports.peerMuted.toString()]: (state, action) => {
        const peer = state.joinedPeers.find((p) => p.id === action.payload.peerId);
        if (peer) {
            peer.audioMuted = true;
            state.joinedPeers = state.joinedPeers
                .filter((p) => p.id !== peer.id)
                .concat([Object.assign({}, peer)]);
        }
    },
    [exports.peerUnmuted.toString()]: (state, action) => {
        const peer = state.joinedPeers.find((p) => p.id === action.payload.peerId);
        if (peer) {
            peer.audioMuted = false;
            state.joinedPeers = state.joinedPeers
                .filter((p) => p.id !== peer.id)
                .concat([Object.assign({}, peer)]);
        }
    },
    [exports.requestToJoinStageAccepted.toString()]: (state, action) => {
        const peer = state.joinedPeers.find((p) => p.id === action.payload.id);
        if (peer) {
            peer.requestToJoinType = action.payload.requestToJoinType;
            state.joinedPeers = state.joinedPeers.filter((p) => p.id !== peer.id).concat([Object.assign({}, peer)]);
        }
    },
    [exports.requestToJoinStageRejected.toString()]: (state, action) => {
        const peer = state.joinedPeers.find((p) => p.id === action.payload.id);
        if (peer) {
            peer.webinarHiddenParticipant = true;
            state.joinedPeers = state.joinedPeers.filter((p) => p.id !== peer.id).concat([Object.assign({}, peer)]);
        }
    },
    [exports.peerRemovedFromStage.toString()]: (state, action) => {
        const peer = state.joinedPeers.find((p) => p.id === action.payload.id);
        if (peer) {
            delete peer.requestToJoinType;
            peer.webinarHiddenParticipant = true;
            peer.audioMuted = true;
            state.joinedPeers = state.joinedPeers.filter((p) => p.id !== peer.id).concat([Object.assign({}, peer)]);
        }
    },
    [exports.peerStartedPresenting.toString()]: (state, action) => {
        const peer = state.joinedPeers.find((p) => p.id === action.payload.id);
        if (peer) {
            peer.webinarHiddenParticipant = false;
            state.joinedPeers = state.joinedPeers.filter((p) => p.id !== peer.id).concat([Object.assign({}, peer)]);
        }
    },
    [exports.peerStoppedPresenting.toString()]: (state, action) => {
        const peer = state.joinedPeers.find((p) => p.id === action.payload.id);
        if (peer) {
            delete peer.requestToJoinType;
            peer.webinarHiddenParticipant = true;
            peer.audioMuted = true;
            state.joinedPeers = state.joinedPeers.filter((p) => p.id !== peer.id).concat([Object.assign({}, peer)]);
        }
    },
    [exports.switchToWebinar.toString()]: (state, action) => {
        state.joinedPeers = [
            ...state.joinedPeers.map((p) => {
                p.webinarHiddenParticipant = true;
                return p;
            }),
        ];
    },
    [exports.peerMetadataUpdated.toString()]: (state, action) => {
        const peer = state.joinedPeers.find((p) => p.id === action.payload.id);
        if (peer) {
            peer.metadata.preset_name = action.payload.preset_name;
            peer.metadata.view_type = action.payload.view_type;
            state.joinedPeers = state.joinedPeers.filter((p) => p.id !== peer.id).concat([Object.assign({}, peer)]);
        }
    },
    [exports.setPeerLabel.toString()]: (state, action) => {
        const peer = state.joinedPeers.find((p) => p.id === action.payload.id);
        if (peer) {
            peer.label = action.payload.label;
            state.joinedPeers = state.joinedPeers.filter((p) => p.id !== peer.id).concat([Object.assign({}, peer)]);
        }
    },
});
exports.default = peersReducer;
//# sourceMappingURL=peersSlice.js.map