"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.storeVideoTransform = exports.setConsumerScore = exports.setConsumerClosed = exports.setConsumerRemotelyResumed = exports.setConsumerRemotelyPaused = exports.addConsumer = void 0;
const toolkit_1 = require("@reduxjs/toolkit");
const roomSlice_1 = require("./roomSlice");
exports.addConsumer = toolkit_1.createAction('session/add-consumer');
exports.setConsumerRemotelyPaused = toolkit_1.createAction('websocket/consumer-paused');
exports.setConsumerRemotelyResumed = toolkit_1.createAction('websocket/consumer-resumed');
exports.setConsumerClosed = toolkit_1.createAction('websocket/consumer-closed');
exports.setConsumerScore = toolkit_1.createAction('websocket/consumer-score');
exports.storeVideoTransform = toolkit_1.createAction('session/store-video-transforms');
const initialState = {
    activeConsumers: [],
    closedConsumers: [],
    videoTransforms: {},
};
const consumersReducer = toolkit_1.createReducer(initialState, {
    [roomSlice_1.resetRoom.toString()]: (state) => {
        state.activeConsumers = [];
        state.closedConsumers = [];
        state.videoTransforms = {};
    },
    [exports.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]);
        }
    },
    [exports.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]);
        }
    },
    [exports.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([Object.assign({}, consumer)]);
    },
    [exports.setConsumerRemotelyPaused.toString()]: (state, action) => {
        const consumer = Object.assign({}, 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([Object.assign({}, consumer)]);
    },
    [exports.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([Object.assign({}, consumer)]);
    },
    [exports.storeVideoTransform.toString()]: (state, action) => {
        state.videoTransforms[action.payload.streamId] = action.payload.transform;
    },
});
exports.default = consumersReducer;
//# sourceMappingURL=consumersSlice.js.map