import { createAction, createReducer } from '@reduxjs/toolkit';
export const roomJoined = createAction('session/join-room');
export const setRoomState = createAction('websocket/room-state');
export const peerJoined = createAction('websocket/peer-joined');
export const peerClosed = createAction('websocket/peer-closed');
export const resetPeers = createAction('session/reset-peers');
export const peerMuted = createAction('websocket/peer-muted');
export const peerUnmuted = createAction('websocket/peer-unmuted');
export const getPage = createAction('websocket/get-page');
export const selectedPeers = createAction('websocket/selected-peers');
export const setPeerVisibility = createAction('websocket/peer-visibility');
export const requestToJoinStageAccepted = createAction('websocket/peer-added-to-stage');
export const requestToJoinStageRejected = createAction('websocket/peer-rejected-to-join-stage');
export const peerRemovedFromStage = createAction('websocket/peer-removed-from-stage');
export const peerStartedPresenting = createAction('websocket/peer-started-presenting');
export const peerStoppedPresenting = createAction('websocket/peer-stopped-presenting');
const initialState = {
    joinedPeers: [],
    currentPagePeerIds: [],
    activeSpeakerPagePeerIds: [],
    recordingPeer: null,
};
const peersReducer = createReducer(initialState, {
    [resetPeers.toString()]: (state) => {
        state.joinedPeers = [];
        state.recordingPeer = null;
    },
    [getPage.toString()]: (state, action) => {
        state.currentPagePeerIds = [...action.payload.peerIds];
    },
    [selectedPeers.toString()]: (state, action) => {
        state.activeSpeakerPagePeerIds = [...action.payload.peerIds];
    },
    [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([{ ...peer }]);
        }
    },
    [roomJoined.toString()]: (state, action) => {
        const { peers } = action.payload;
        state.recordingPeer = peers
            ? peers.find((p) => p.flags?.recorder)
            : null;
        state.joinedPeers = peers
            ? peers.filter((p) => !p.flags?.recorder)
            : [];
    },
    [setRoomState.toString()]: (state, action) => {
        const { peers } = action.payload.roomState;
        state.recordingPeer = peers
            ? peers.find((p) => p.flags?.recorder)
            : null;
        state.joinedPeers = peers
            ? peers.filter((p) => !p.flags?.recorder)
            : [];
    },
    [peerJoined.toString()]: (state, action) => {
        const peer = action.payload;
        if (peer.flags?.recorder) {
            state.recordingPeer = peer;
        }
        else {
            state.joinedPeers = state.joinedPeers
                .filter((p) => p.id !== peer.id)
                .concat(peer);
        }
    },
    [peerClosed.toString()]: (state, action) => {
        const peer = action.payload;
        if (peer.flags?.recorder) {
            state.recordingPeer = null;
        }
        else {
            state.joinedPeers = state.joinedPeers.filter((p) => p.id !== peer.id);
        }
    },
    [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([{ ...peer }]);
        }
    },
    [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([{ ...peer }]);
        }
    },
    [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([{ ...peer }]);
        }
    },
    [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([{ ...peer }]);
        }
    },
    [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([{ ...peer }]);
        }
    },
    [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([{ ...peer }]);
        }
    },
    [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([{ ...peer }]);
        }
    },
});
export default peersReducer;
//# sourceMappingURL=peersSlice.js.map