import { createAction, createReducer } from '@reduxjs/toolkit';
import { resetRoom } from '../store/roomSlice';
export const addConsumer = createAction('session/add-consumer');
export const setConsumerRemotelyPaused = createAction('websocket/consumer-paused');
export const setConsumerRemotelyResumed = createAction('websocket/consumer-resumed');
export const setConsumerClosed = createAction('websocket/consumer-closed');
export const setConsumerScore = createAction('websocket/consumer-score');
export const storeVideoTransform = createAction('session/store-video-transforms');
const initialState = {
    activeConsumers: [],
    closedConsumers: [],
    videoTransforms: {},
};
const consumersReducer = createReducer(initialState, {
    [resetRoom.toString()]: (state) => {
        state.activeConsumers = [];
        state.closedConsumers = [];
        state.videoTransforms = {};
    },
    [addConsumer.toString()]: (state, action) => {
        if (state.closedConsumers.includes(action.payload.id)) {
            state.closedConsumers.filter((c) => c !== action.payload.id);
        }
        else {
            state.activeConsumers = state.activeConsumers
                .filter((c) => c.id !== action.payload.id)
                .concat([action.payload]);
        }
    },
    [setConsumerClosed.toString()]: (state, action) => {
        const consumer = state.activeConsumers.find((c) => c.id === action.payload.id);
        if (consumer) {
            state.activeConsumers = state.activeConsumers.filter((c) => c.id !== action.payload.id);
        }
        else {
            state.closedConsumers = state.closedConsumers.concat([action.payload.id]);
        }
    },
    [setConsumerScore.toString()]: (state, action) => {
        const consumer = state.activeConsumers.find((c) => c.id === action.payload.id);
        if (!consumer) {
            return;
        }
        consumer.score = action.payload.score.score;
        consumer.producerScore = action.payload.score.producerScore;
        state.activeConsumers = state.activeConsumers
            .filter((c) => c.id !== action.payload.id)
            .concat([{ ...consumer }]);
    },
    [setConsumerRemotelyPaused.toString()]: (state, action) => {
        const consumer = {
            ...state.activeConsumers.find((c) => c.id === action.payload.id),
        };
        if (!consumer) {
            return;
        }
        consumer.remotelyPaused = true;
        state.activeConsumers = state.activeConsumers
            .filter((c) => c.id !== action.payload.id)
            .concat([{ ...consumer }]);
    },
    [setConsumerRemotelyResumed.toString()]: (state, action) => {
        const consumer = state.activeConsumers.find((c) => c.id === action.payload.id);
        if (!consumer) {
            return;
        }
        consumer.remotelyPaused = false;
        state.activeConsumers = state.activeConsumers
            .filter((c) => c.id !== action.payload.id)
            .concat([{ ...consumer }]);
    },
    [storeVideoTransform.toString()]: (state, action) => {
        state.videoTransforms[action.payload.streamId] = action.payload.transform;
    },
});
export default consumersReducer;
//# sourceMappingURL=consumersSlice.js.map