import { createAction, createReducer } from '@reduxjs/toolkit';
import { roomJoined } from '../store/peersSlice';
import { DyteViewTypes } from '../utils/dyte-utils/src';
export 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 || (MediaStates = {}));
export 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 || (RoomSocketState = {}));
export 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 || (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: DyteViewTypes.groupCall,
    requestToJoinPeersList: [],
};
export const setRouterCapability = createAction('websocket/router-capability');
export const setTransport = createAction('websocket/create-transport');
export const setTransportConnected = createAction('websocket/connect-transport');
export const setRoomSocketState = createAction('session/socket-state');
export const setPresetFetched = createAction('session/preset-fetched');
export const setRouterFetchState = createAction('session/router-state');
export const setRoomDisplayTitleState = createAction('session/room-display-title-state');
export const setRoomNodeLink = createAction('session/room-node-link');
export const setProducersState = createAction('session/producers-state');
export const resetRoom = createAction('session/reset-room');
export const addChatMessage = createAction('websocket/new-chat-message');
export const getChatMessages = createAction('websocket/get-chat-messages');
export const getPolls = createAction('websocket/get-polls');
export const updatePoll = createAction('websocket/update-poll');
export const setLegacyMode = createAction('websocket/room-legacy-mode');
export const setRoomState = createAction('websocket/room-state');
export const setRoomDisplayTitle = createAction('websocket/room-display-title');
export const changeActiveSpeaker = createAction('websocket/active-speaker');
export const clearActiveSpeaker = createAction('websocket/no-active-speaker');
export const waitlistAccepted = createAction('websocket/waitlist-accepted');
export const waitlistRejected = createAction('websocket/waitlist-rejected');
export const newWaitListPeer = createAction('websocket/waitlist-peer-added');
export const removeWaitListPeer = createAction('websocket/waitlist-peer-closed');
export const setRoomPassword = createAction('session/room-password');
export const markMessagesRead = createAction('session/mark-messages-read');
export const setRoomGridView = createAction('session/room-grid-view');
export const pinPeer = createAction('websocket/pin-peer');
export const setKicked = createAction('websocket/kicked');
export const setShowSetupScreen = createAction('session/show-setup-screen');
export const setScreenShareRequested = createAction('session/screenshare-requested');
export const setCurrentPage = createAction('session/set-current-page');
export const newRequestedJoinToStage = createAction('websocket/request-to-join-stage-peer-added');
export const peerAddedToStage = createAction('websocket/peer-added-to-stage');
export const peerRemovedToStage = createAction('websocket/peer-rejected-to-join-stage');
export const requestedJoinToStageSeen = createAction('room/seen-all-participant');
export const setRoomViewType = createAction('websocket/room-view-type');
const roomReducer = createReducer(initialState, {
    // eslint-disable-next-line @typescript-eslint/no-unused-vars
    [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;
    },
    [setRoomSocketState.toString()]: (state, action) => {
        state.socketState = action.payload;
    },
    [setRouterFetchState.toString()]: (state, action) => {
        state.routerState = action.payload;
    },
    [setPresetFetched.toString()]: (state, action) => {
        state.presetFetched = action.payload;
    },
    [setRouterCapability.toString()]: (state) => {
        state.routerState = MediaStates.FETCHED;
    },
    [addChatMessage.toString()]: (state, action) => {
        state.chatMessages = [
            ...state.chatMessages,
            { ...action.payload, read: false },
        ];
    },
    [getChatMessages.toString()]: (state, action) => {
        state.chatMessages = action.payload.messages.map((obj) => ({
            ...obj,
            read: true,
        }));
    },
    [markMessagesRead.toString()]: (state, action) => {
        const messagesRed = action.payload;
        state.chatMessages = state.chatMessages.map((obj) => {
            const messageType = obj.type;
            if (!messageType || messagesRed[messageType]) {
                return { ...obj, read: true };
            }
            return obj;
        });
    },
    [updatePoll.toString()]: (state, action) => {
        const payload = action.payload.poll;
        state.polls[payload.id] = payload;
    },
    [getPolls.toString()]: (state, action) => {
        state.polls = action.payload.polls;
    },
    [setRoomDisplayTitle.toString()]: (state, action) => {
        state.roomDisplayTitle = action.payload.displayTitle;
    },
    [setRoomDisplayTitleState.toString()]: (state, action) => {
        state.roomDisplayTitleState = action.payload;
    },
    [setRoomState.toString()]: (state, action) => {
        const { roomUUID, displayTitle } = action.payload.roomState;
        state.roomDisplayTitle = displayTitle;
        state.roomUUID = roomUUID;
    },
    [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;
        }
    },
    [setLegacyMode.toString()]: (state, action) => {
        state.legacyMode = action.payload.turnOn;
    },
    [setCurrentPage.toString()]: (state, action) => {
        state.currentPage = action.payload;
    },
    [changeActiveSpeaker.toString()]: (state, action) => {
        state.activeSpeakerPeerId = action.payload.peerId;
    },
    [clearActiveSpeaker.toString()]: (state) => {
        state.activeSpeakerPeerId = '';
    },
    [setProducersState.toString()]: (state, action) => {
        state.producersState = action.payload;
    },
    [setRoomNodeLink.toString()]: (state, action) => {
        state.roomNodeLink = action.payload;
    },
    [waitlistAccepted.toString()]: (state) => {
        state.waitingState = WaitingState.ACCEPTED;
    },
    [waitlistRejected.toString()]: (state) => {
        state.waitingState = WaitingState.REJECTED;
    },
    [newWaitListPeer.toString()]: (state, action) => {
        state.waitlistedPeers = [
            ...state.waitlistedPeers.filter((peer) => peer.id !== action.payload.id),
            action.payload,
        ];
    },
    [removeWaitListPeer.toString()]: (state, action) => {
        state.waitlistedPeers = state.waitlistedPeers.filter((peer) => peer.id !== action.payload.id);
    },
    [setRoomPassword.toString()]: (state, action) => {
        state.password = action.payload;
    },
    [setRoomGridView.toString()]: (state, action) => {
        state.gridView = action.payload;
    },
    [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;
    },
    [setKicked.toString()]: (state, action) => {
        if (!action.payload?.kickType) {
            state.kicked = 'unknown';
        }
        state.kicked = action.payload?.kickType;
    },
    [setShowSetupScreen.toString()]: (state, action) => {
        state.showSetupScreen = action.payload;
    },
    [setScreenShareRequested.toString()]: (state, action) => {
        state.screenShareRequested = action.payload;
    },
    [newRequestedJoinToStage.toString()]: (state, action) => {
        state.requestToJoinPeersList = [
            ...state.requestToJoinPeersList.filter((peer) => peer.id !== action.payload.id),
            { ...action.payload, seen: false },
        ];
    },
    [peerRemovedToStage.toString()]: (state, action) => {
        // eslint-disable-next-line max-len
        state.requestToJoinPeersList = state.requestToJoinPeersList.filter((peer) => peer.id !== action.payload.id);
    },
    [peerAddedToStage.toString()]: (state, action) => {
        // eslint-disable-next-line max-len
        state.requestToJoinPeersList = state.requestToJoinPeersList.filter((peer) => peer.id !== action.payload.id);
    },
    [requestedJoinToStageSeen.toString()]: (state) => {
        state.requestToJoinPeersList = [
            ...state.requestToJoinPeersList.map((el) => {
                el.seen = true;
                return el;
            }),
        ];
    },
    [setRoomViewType.toString()]: (state, action) => {
        state.roomViewType = action.payload;
    },
});
export default roomReducer;
//# sourceMappingURL=roomSlice.js.map