"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.setProducerScore = exports.setProducerClosed = exports.producerConnected = void 0;
const toolkit_1 = require("@reduxjs/toolkit");
const roomSlice_1 = require("./roomSlice");
exports.producerConnected = toolkit_1.createAction('websocket/producer-connect');
exports.setProducerClosed = toolkit_1.createAction('websocket/producer-closed');
exports.setProducerScore = toolkit_1.createAction('websocket/producer-score');
const initialState = {
    activeProducers: [],
};
const producersReducer = toolkit_1.createReducer(initialState, {
    [roomSlice_1.resetRoom.toString()]: (state) => {
        state.activeProducers = [];
    },
    [exports.producerConnected.toString()]: (state, action) => {
        state.activeProducers = state.activeProducers
            .filter((c) => c.id !== action.payload.id)
            .concat([action.payload]);
    },
    [exports.setProducerClosed.toString()]: (state, action) => {
        state.activeProducers = state.activeProducers.filter((c) => c.id !== action.payload.id);
    },
    [exports.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([Object.assign({}, producer)]);
    },
});
exports.default = producersReducer;
//# sourceMappingURL=producerSlice.js.map