import { createReducer, createAction } from '@reduxjs/toolkit';
import { resetRoom } from '../store/roomSlice';
export const producerConnected = createAction('websocket/producer-connect');
export const setProducerClosed = createAction('websocket/producer-closed');
export const setProducerScore = createAction('websocket/producer-score');
const initialState = {
    activeProducers: [],
};
const producersReducer = createReducer(initialState, {
    [resetRoom.toString()]: (state) => {
        state.activeProducers = [];
    },
    [producerConnected.toString()]: (state, action) => {
        state.activeProducers = state.activeProducers
            .filter((c) => c.id !== action.payload.id)
            .concat([action.payload]);
    },
    [setProducerClosed.toString()]: (state, action) => {
        state.activeProducers = state.activeProducers.filter((c) => c.id !== action.payload.id);
    },
    [setProducerScore.toString()]: (state, action) => {
        const producer = state.activeProducers.find((c) => c.id === action.payload.id);
        if (!producer) {
            return;
        }
        producer.score = action.payload.score[0].score;
        state.activeProducers = state.activeProducers
            .filter((c) => c.id !== action.payload.id)
            .concat([{ ...producer }]);
    },
});
export default producersReducer;
//# sourceMappingURL=producerSlice.js.map