import { createAction, createReducer } from '@reduxjs/toolkit';
import config from '../utils/config';
const initialState = {
    plugins: [],
    main: [],
    side: [],
    background: [],
};
export const addPlugin = createAction('plugins/addView');
export const removePlugin = createAction('plugins/removeView');
const plugins = createReducer(initialState, {
    [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.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;
        // 	}
        // }
    },
    [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);
    },
});
export default plugins;
//# sourceMappingURL=pluginSlice.js.map