"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.removeAccessUser = exports.addAccessUser = exports.removePlugin = exports.addPlugin = void 0;
const toolkit_1 = require("@reduxjs/toolkit");
const config_1 = __importDefault(require("../utils/config"));
const initialState = {
    plugins: [],
    main: [],
    side: [],
    background: [],
};
exports.addPlugin = toolkit_1.createAction('plugins/addView');
exports.removePlugin = toolkit_1.createAction('plugins/removeView');
exports.addAccessUser = toolkit_1.createAction('websocket/plugin-acl-grant');
exports.removeAccessUser = toolkit_1.createAction('websocket/plugin-acl-remove');
const plugins = toolkit_1.createReducer(initialState, {
    [exports.addPlugin.toString()]: (state, action) => {
        const plugin = action.payload;
        state.plugins.push(plugin);
        const viewURL = new URL(plugin.baseURL);
        viewURL.searchParams.append('auth', plugin.authToken);
        viewURL.searchParams.append('parent', '*');
        viewURL.searchParams.append('backend', config_1.default.apiBase);
        viewURL.searchParams.append('pluginId', plugin.id);
        const viewFrame = {
            url: viewURL.href,
            plugin: plugin.id,
            view: '/',
        };
        state.main.push(viewFrame);
        // for (const view of plugin.views) {
        // 	const viewURL = new URL(view.path, plugin.baseURL);
        // 	viewURL.searchParams.append('auth', plugin.authToken);
        // 	const viewFrame = {
        // 		url: viewURL.href,
        // 		plugin: plugin.id,
        // 		view: view.id,
        // 	};
        // 	switch (view.type) {
        // 		case 'SIDEBAR':
        // 			state.side.push(viewFrame);
        // 			break;
        // 		case 'MAIN':
        // 			state.main.push(viewFrame);
        // 			break;
        // 		case 'BACKGROUND':
        // 			state.background.push(viewFrame);
        // 			break;
        // 		default: break;
        // 	}
        // }
    },
    [exports.removePlugin.toString()]: (state, action) => {
        const plugin = action.payload;
        state.main = state.main.filter((frame) => frame.plugin !== plugin.id);
        state.side = state.side.filter((frame) => frame.plugin !== plugin.id);
        state.background = state.background.filter((frame) => frame.plugin !== plugin.id);
        state.plugins = state.plugins.filter((p) => p.id !== plugin.id);
    },
    [exports.addAccessUser.toString()]: (state, action) => {
        const { payload } = action;
        const plugin = state.plugins.find((p) => p.id === payload.pluginId);
        if (plugin) {
            const newPlugin = Object.assign({}, plugin);
            newPlugin.ACLAccessGrantedUsers.push(payload.userId);
            newPlugin.ACLAccessRemovedUsers =
                newPlugin.ACLAccessRemovedUsers.filter((userId) => userId !== payload.userId);
            state.plugins = state.plugins
                .filter((p) => p.id !== plugin.id)
                .concat([newPlugin]);
        }
    },
    [exports.removeAccessUser.toString()]: (state, action) => {
        const { payload } = action;
        const plugin = state.plugins.find((p) => p.id === payload.pluginId);
        if (plugin) {
            const newPlugin = Object.assign({}, plugin);
            newPlugin.ACLAccessRemovedUsers.push(payload.userId);
            newPlugin.ACLAccessGrantedUsers =
                newPlugin.ACLAccessGrantedUsers.filter((userId) => userId !== payload.userId);
            state.plugins = state.plugins
                .filter((p) => p.id !== plugin.id)
                .concat([newPlugin]);
        }
    },
});
exports.default = plugins;
//# sourceMappingURL=pluginSlice.js.map