"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.setRoomViewType = exports.requestedJoinToStageSeen = exports.peerRemovedToStage = exports.peerAddedToStage = exports.newRequestedJoinToStage = exports.setCurrentPage = exports.setScreenShareRequested = exports.setShowSetupScreen = exports.setKicked = exports.pinPeer = exports.setRoomGridView = exports.markMessagesRead = exports.setRoomPassword = exports.removeWaitListPeer = exports.newWaitListPeer = exports.waitlistRejected = exports.waitlistAccepted = exports.clearActiveSpeaker = exports.changeActiveSpeaker = exports.setRoomDisplayTitle = exports.setRoomState = exports.setLegacyMode = exports.updatePoll = exports.getPolls = exports.getChatMessages = exports.addChatMessage = exports.resetRoom = exports.setProducersState = exports.setRoomNodeLink = exports.setRoomDisplayTitleState = exports.setRouterFetchState = exports.setPresetFetched = exports.setRoomSocketState = exports.setTransportConnected = exports.setTransport = exports.setRouterCapability = exports.WaitingState = exports.RoomSocketState = exports.MediaStates = void 0;
const toolkit_1 = require("@reduxjs/toolkit");
const peersSlice_1 = require("./peersSlice");
const src_1 = require("../utils/dyte-utils/src");
var MediaStates;
(function (MediaStates) {
    MediaStates["NOT_STARTED"] = "NOT_STARTED";
    MediaStates["CONNECTING"] = "CONNECTING";
    MediaStates["FETCHED"] = "FETCHED";
    MediaStates["LOADED"] = "LOADED";
    MediaStates["STOPPING"] = "STOPPING";
    MediaStates["NOT_APPLICABLE"] = "NOT_APPLICABLE";
})(MediaStates = exports.MediaStates || (exports.MediaStates = {}));
var RoomSocketState;
(function (RoomSocketState) {
    RoomSocketState[RoomSocketState["NOT_STARTED"] = 0] = "NOT_STARTED";
    RoomSocketState[RoomSocketState["CONNECTED"] = 1] = "CONNECTED";
    RoomSocketState[RoomSocketState["DISCONNECTED"] = 2] = "DISCONNECTED";
    RoomSocketState[RoomSocketState["ERRORED"] = 3] = "ERRORED";
})(RoomSocketState = exports.RoomSocketState || (exports.RoomSocketState = {}));
var WaitingState;
(function (WaitingState) {
    WaitingState[WaitingState["NONE"] = 0] = "NONE";
    WaitingState[WaitingState["WAITING"] = 1] = "WAITING";
    WaitingState[WaitingState["ACCEPTED"] = 2] = "ACCEPTED";
    WaitingState[WaitingState["REJECTED"] = 3] = "REJECTED";
})(WaitingState = exports.WaitingState || (exports.WaitingState = {}));
const initialState = {
    routerState: MediaStates.NOT_APPLICABLE,
    producersState: MediaStates.NOT_APPLICABLE,
    roomDisplayTitleState: MediaStates.NOT_STARTED,
    socketState: RoomSocketState.NOT_STARTED,
    polls: {},
    chatMessages: [],
    legacyMode: false,
    joined: false,
    roomDisplayTitle: '',
    activeSpeakerPeerId: '',
    roomUUID: '',
    startedAt: null,
    maxPreferredStreams: 10,
    roomNodeLink: '',
    waitingState: WaitingState.NONE,
    waitlistedPeers: [],
    password: '',
    roomName: '',
    gridView: 'multi',
    gridViewSaved: 'multi',
    pinnedPeerId: null,
    kicked: '',
    presetFetched: false,
    showSetupScreen: null,
    screenShareRequested: false,
    currentPage: 0,
    roomViewType: src_1.DyteViewTypes.groupCall,
    requestToJoinPeersList: [],
};
exports.setRouterCapability = toolkit_1.createAction('websocket/router-capability');
exports.setTransport = toolkit_1.createAction('websocket/create-transport');
exports.setTransportConnected = toolkit_1.createAction('websocket/connect-transport');
exports.setRoomSocketState = toolkit_1.createAction('session/socket-state');
exports.setPresetFetched = toolkit_1.createAction('session/preset-fetched');
exports.setRouterFetchState = toolkit_1.createAction('session/router-state');
exports.setRoomDisplayTitleState = toolkit_1.createAction('session/room-display-title-state');
exports.setRoomNodeLink = toolkit_1.createAction('session/room-node-link');
exports.setProducersState = toolkit_1.createAction('session/producers-state');
exports.resetRoom = toolkit_1.createAction('session/reset-room');
exports.addChatMessage = toolkit_1.createAction('websocket/new-chat-message');
exports.getChatMessages = toolkit_1.createAction('websocket/get-chat-messages');
exports.getPolls = toolkit_1.createAction('websocket/get-polls');
exports.updatePoll = toolkit_1.createAction('websocket/update-poll');
exports.setLegacyMode = toolkit_1.createAction('websocket/room-legacy-mode');
exports.setRoomState = toolkit_1.createAction('websocket/room-state');
exports.setRoomDisplayTitle = toolkit_1.createAction('websocket/room-display-title');
exports.changeActiveSpeaker = toolkit_1.createAction('websocket/active-speaker');
exports.clearActiveSpeaker = toolkit_1.createAction('websocket/no-active-speaker');
exports.waitlistAccepted = toolkit_1.createAction('websocket/waitlist-accepted');
exports.waitlistRejected = toolkit_1.createAction('websocket/waitlist-rejected');
exports.newWaitListPeer = toolkit_1.createAction('websocket/waitlist-peer-added');
exports.removeWaitListPeer = toolkit_1.createAction('websocket/waitlist-peer-closed');
exports.setRoomPassword = toolkit_1.createAction('session/room-password');
exports.markMessagesRead = toolkit_1.createAction('session/mark-messages-read');
exports.setRoomGridView = toolkit_1.createAction('session/room-grid-view');
exports.pinPeer = toolkit_1.createAction('websocket/pin-peer');
exports.setKicked = toolkit_1.createAction('websocket/kicked');
exports.setShowSetupScreen = toolkit_1.createAction('session/show-setup-screen');
exports.setScreenShareRequested = toolkit_1.createAction('session/screenshare-requested');
exports.setCurrentPage = toolkit_1.createAction('session/set-current-page');
exports.newRequestedJoinToStage = toolkit_1.createAction('websocket/request-to-join-stage-peer-added');
exports.peerAddedToStage = toolkit_1.createAction('websocket/peer-added-to-stage');
exports.peerRemovedToStage = toolkit_1.createAction('websocket/peer-rejected-to-join-stage');
exports.requestedJoinToStageSeen = toolkit_1.createAction('room/seen-all-participant');
exports.setRoomViewType = toolkit_1.createAction('websocket/room-view-type');
const roomReducer = toolkit_1.createReducer(initialState, {
    // eslint-disable-next-line @typescript-eslint/no-unused-vars
    [exports.resetRoom.toString()]: (state, action) => {
        const { roomName, showSetupScreen } = action.payload;
        state.roomName = roomName || state.roomName;
        state.showSetupScreen = state.showSetupScreen;
        state.socketState = initialState.socketState;
        state.producersState = initialState.producersState;
    },
    [exports.setRoomSocketState.toString()]: (state, action) => {
        state.socketState = action.payload;
    },
    [exports.setRouterFetchState.toString()]: (state, action) => {
        state.routerState = action.payload;
    },
    [exports.setPresetFetched.toString()]: (state, action) => {
        state.presetFetched = action.payload;
    },
    [exports.setRouterCapability.toString()]: (state) => {
        state.routerState = MediaStates.FETCHED;
    },
    [exports.addChatMessage.toString()]: (state, action) => {
        state.chatMessages = [
            ...state.chatMessages,
            Object.assign(Object.assign({}, action.payload), { read: false }),
        ];
    },
    [exports.getChatMessages.toString()]: (state, action) => {
        state.chatMessages = action.payload.messages.map((obj) => (Object.assign(Object.assign({}, obj), { read: true })));
    },
    [exports.markMessagesRead.toString()]: (state, action) => {
        const messagesRed = action.payload;
        state.chatMessages = state.chatMessages.map((obj) => {
            const messageType = obj.type;
            if (!messageType || messagesRed[messageType]) {
                return Object.assign(Object.assign({}, obj), { read: true });
            }
            return obj;
        });
    },
    [exports.updatePoll.toString()]: (state, action) => {
        const payload = action.payload.poll;
        state.polls[payload.id] = payload;
    },
    [exports.getPolls.toString()]: (state, action) => {
        state.polls = action.payload.polls;
    },
    [exports.setRoomDisplayTitle.toString()]: (state, action) => {
        state.roomDisplayTitle = action.payload.displayTitle;
    },
    [exports.setRoomDisplayTitleState.toString()]: (state, action) => {
        state.roomDisplayTitleState = action.payload;
    },
    [exports.setRoomState.toString()]: (state, action) => {
        const { roomUUID, displayTitle } = action.payload.roomState;
        state.roomDisplayTitle = displayTitle;
        state.roomUUID = roomUUID;
    },
    [peersSlice_1.roomJoined.toString()]: (state, action) => {
        state.legacyMode = action.payload.legacyMode || initialState.legacyMode;
        state.startedAt = action.payload.waitlisted
            ? null
            : action.payload.startedAt;
        state.maxPreferredStreams = action.payload.maxPreferredStreams
            || initialState.maxPreferredStreams;
        state.joined = true;
        state.waitingState = action.payload.waitlisted
            ? WaitingState.WAITING
            : WaitingState.NONE;
        state.pinnedPeerId = action.payload.pinnedPeerId;
        state.gridView = action.payload.pinnedPeerId
            ? 'highlighted'
            : state.gridView;
        if (action.payload.waitlistedPeers) {
            state.waitlistedPeers = action.payload.waitlistedPeers;
        }
    },
    [exports.setLegacyMode.toString()]: (state, action) => {
        state.legacyMode = action.payload.turnOn;
    },
    [exports.setCurrentPage.toString()]: (state, action) => {
        state.currentPage = action.payload;
    },
    [exports.changeActiveSpeaker.toString()]: (state, action) => {
        state.activeSpeakerPeerId = action.payload.peerId;
    },
    [exports.clearActiveSpeaker.toString()]: (state) => {
        state.activeSpeakerPeerId = '';
    },
    [exports.setProducersState.toString()]: (state, action) => {
        state.producersState = action.payload;
    },
    [exports.setRoomNodeLink.toString()]: (state, action) => {
        state.roomNodeLink = action.payload;
    },
    [exports.waitlistAccepted.toString()]: (state) => {
        state.waitingState = WaitingState.ACCEPTED;
    },
    [exports.waitlistRejected.toString()]: (state) => {
        state.waitingState = WaitingState.REJECTED;
    },
    [exports.newWaitListPeer.toString()]: (state, action) => {
        state.waitlistedPeers = [
            ...state.waitlistedPeers.filter((peer) => peer.id !== action.payload.id),
            action.payload,
        ];
    },
    [exports.removeWaitListPeer.toString()]: (state, action) => {
        state.waitlistedPeers = state.waitlistedPeers.filter((peer) => peer.id !== action.payload.id);
    },
    [exports.setRoomPassword.toString()]: (state, action) => {
        state.password = action.payload;
    },
    [exports.setRoomGridView.toString()]: (state, action) => {
        state.gridView = action.payload;
    },
    [exports.pinPeer.toString()]: (state, action) => {
        if (action.payload.peerId) {
            state.gridViewSaved = state.gridViewSaved
                ? state.gridViewSaved
                : state.gridView;
            state.gridView = 'highlighted';
        }
        else {
            state.gridView = state.gridViewSaved;
            state.gridViewSaved = null;
        }
        state.pinnedPeerId = action.payload.peerId;
    },
    [exports.setKicked.toString()]: (state, action) => {
        var _a, _b;
        if (!((_a = action.payload) === null || _a === void 0 ? void 0 : _a.kickType)) {
            state.kicked = 'unknown';
        }
        state.kicked = (_b = action.payload) === null || _b === void 0 ? void 0 : _b.kickType;
    },
    [exports.setShowSetupScreen.toString()]: (state, action) => {
        state.showSetupScreen = action.payload;
    },
    [exports.setScreenShareRequested.toString()]: (state, action) => {
        state.screenShareRequested = action.payload;
    },
    [exports.newRequestedJoinToStage.toString()]: (state, action) => {
        state.requestToJoinPeersList = [
            ...state.requestToJoinPeersList.filter((peer) => peer.id !== action.payload.id),
            Object.assign(Object.assign({}, action.payload), { seen: false }),
        ];
    },
    [exports.peerRemovedToStage.toString()]: (state, action) => {
        // eslint-disable-next-line max-len
        state.requestToJoinPeersList = state.requestToJoinPeersList.filter((peer) => peer.id !== action.payload.id);
    },
    [exports.peerAddedToStage.toString()]: (state, action) => {
        // eslint-disable-next-line max-len
        state.requestToJoinPeersList = state.requestToJoinPeersList.filter((peer) => peer.id !== action.payload.id);
    },
    [exports.requestedJoinToStageSeen.toString()]: (state) => {
        state.requestToJoinPeersList = [
            ...state.requestToJoinPeersList.map((el) => {
                el.seen = true;
                return el;
            }),
        ];
    },
    [exports.setRoomViewType.toString()]: (state, action) => {
        state.roomViewType = action.payload;
    },
});
exports.default = roomReducer;
//# sourceMappingURL=roomSlice.js.map