/* eslint-disable fp/no-delete -- immer */
/* eslint-disable fp/no-let */
import { nanoid } from "nanoid";
import { createContext } from "./react-immersive-history";
import { initialApp } from "./InitialData";
import { normalizeApp } from "./utils/normalizeSchema";
export const RuntimeContext = createContext(initialApp, (modify) => {
    try {
        return {
            setTheme: (theme) => {
                modify((draft) => {
                    draft.theme = theme;
                });
            },
            setApp: (app) => {
                modify(() => {
                    return normalizeApp(app);
                });
            },
            setIcon: (url) => {
                modify((draft) => {
                    draft.icon = url;
                }, false);
            },
            setInitialPage: (entryPage) => {
                modify((draft) => {
                    draft.config.initialPage = Object.assign(Object.assign({}, draft.config.initialPage), { entryPage });
                }, false);
            },
            setLoggedInPage: (loggedInPage) => {
                modify((draft) => {
                    draft.config.initialPage = Object.assign(Object.assign({}, draft.config.initialPage), { loggedInPage });
                }, false);
            },
            addNpmComponent: (name, version) => {
                modify((draft) => {
                    if (draft.components.find((c) => c.name === name && c.version === version)) {
                        return;
                    }
                    draft.components.push({
                        name,
                        version,
                        enabled: true,
                    });
                });
            },
            removeNpmComponent: (name) => {
                modify((draft) => {
                    const index = draft.components.findIndex((c) => c.name === name);
                    if (index !== -1) {
                        draft.components.splice(index, 1);
                    }
                });
            },
            setVersionNpmComponent: (name, version) => {
                modify((draft) => {
                    const index = draft.components.findIndex((c) => c.name === name);
                    if (index !== -1) {
                        draft.components[index].version = version;
                    }
                });
            },
            setEnableNpmComponent: (name, enabled) => {
                modify((draft) => {
                    const index = draft.components.findIndex((c) => c.name === name);
                    if (index !== -1) {
                        draft.components[index].enabled = enabled;
                    }
                });
            },
            addPage(page) {
                modify((draft) => {
                    draft.maps.page[page.id] = page;
                });
            },
            addIntegration(integration) {
                modify((draft) => {
                    draft.integrations[integration.name] = integration;
                });
            },
            setIntegrationProperty: function (namespace, field, value) {
                modify((draft) => {
                    draft.integrations[namespace].properties.values[field] = value;
                });
            },
            removeIntegration(namespace) {
                modify((draft) => {
                    delete draft.integrations[namespace];
                });
            },
            addCustomSources(externalData) {
                modify((draft) => {
                    draft.customSources[externalData.name] = externalData;
                });
            },
            removeCustomSources(namespace) {
                modify((draft) => {
                    delete draft.customSources[namespace];
                });
            },
            addComponent(component, pageRef, pageOpt) {
                modify((draft) => {
                    draft.maps.component[component.id] = component;
                    if (pageRef && pageOpt && typeof pageOpt.idx === "number") {
                        draft.maps.page[pageRef.page].components.splice(pageOpt.idx, 0, {
                            component: component.id,
                        });
                    }
                    else if (pageRef) {
                        draft.maps.page[pageRef.page].components.push({
                            component: component.id,
                        });
                    }
                });
            },
            addPageAndComponents(page, components) {
                modify((draft) => {
                    draft.maps.page[page.id] = page;
                    components.forEach((component) => {
                        draft.maps.component[component.id] = component;
                    });
                });
            },
            swapComponent(target, sourceIndex, destinationIndex, deletionLength = 1) {
                modify((draft) => {
                    const components = draft.maps.page[target.page].components;
                    const removed = components.splice(sourceIndex, deletionLength);
                    removed && components.splice(destinationIndex, 0, ...removed);
                    draft.maps.page[target.page].components = components;
                });
            },
            moveComponents(listUpdateComponents) {
                modify((draft) => {
                    listUpdateComponents.forEach((comp) => {
                        draft.maps.component[comp.id].metadata = comp.metadata;
                    });
                });
            },
            swapPage(targetPages, sourceIndex, destinationIndex) {
                modify((draft) => {
                    const pages = Object.values(draft.maps.page).filter((page) => page.type !== targetPages[0].type);
                    const [removed] = targetPages.splice(sourceIndex, 1);
                    removed && targetPages.splice(destinationIndex, 0, removed);
                    const newPages = pages.concat(targetPages);
                    draft.maps.page = newPages.reduce((reduced, page) => (Object.assign(Object.assign({}, reduced), { [page.id]: page })), {});
                });
            },
            setComponentProperty: function (target, field, value) {
                modify((draft) => {
                    draft.maps.component[target.component].properties[field] = value;
                });
            },
            setComponentMetadata: function (target, field, value) {
                modify((draft) => {
                    if (draft.maps.component[target.component].metadata) {
                        draft.maps.component[target.component].metadata[field] = value;
                    }
                    else {
                        draft.maps.component[target.component].metadata = {
                            [field]: value,
                        };
                    }
                });
            },
            setPageProperty: function (target, field, value) {
                modify((draft) => {
                    draft.maps.page[target.page].properties.values[field] = value;
                });
            },
            removeComponent(target, from, option) {
                modify((draft) => {
                    var _a;
                    if ((_a = option === null || option === void 0 ? void 0 : option.layoutDetail) === null || _a === void 0 ? void 0 : _a.componentsID) {
                        option.layoutDetail.componentsID.forEach((compId) => {
                            if (draft.maps.component[compId].metadata) {
                                draft.maps.component[compId].metadata.layout = undefined;
                            }
                            else {
                                draft.maps.component[compId].metadata = {};
                            }
                        });
                    }
                    const children = Object.entries(draft.maps.component).filter(([, value]) => value.metadata.container === target.component);
                    const childrenKeys = children.map((item) => item[0]);
                    draft.maps.page[from.page].components = draft.maps.page[from.page].components.filter((ref) => ![...childrenKeys, target.component].includes(ref.component));
                    childrenKeys.forEach((key) => {
                        delete draft.maps.component[key];
                    });
                    delete draft.maps.component[target.component];
                });
            },
            removeComponents(target, from) {
                modify((draft) => {
                    draft.maps.page[from.page].components = draft.maps.page[from.page].components.filter((ref) => !target.find((comp) => comp.component === ref.component));
                    target.forEach(({ component }) => {
                        delete draft.maps.component[component];
                    });
                });
            },
            removePage(target) {
                modify((draft) => {
                    const page = draft.maps.page[target.page];
                    for (const component of page.components) {
                        delete draft.maps.component[component.component];
                    }
                    delete draft.maps.page[target.page];
                });
            },
            setComponentSource(target, source) {
                modify((draft) => {
                    draft.maps.component[target.component].source = source;
                });
            },
            setComponentLayoutMetadata(target, column) {
                modify((draft) => {
                    if (!draft.maps.component[target.component].metadata ||
                        !draft.maps.component[target.component].metadata.layout)
                        return;
                    draft.maps.component[target.component].metadata.layout.column =
                        column;
                });
            },
            removeComponentLayoutMetadata(target) {
                modify((draft) => {
                    if (!draft.maps.component[target.component].metadata ||
                        !draft.maps.component[target.component].metadata.layout)
                        return;
                    draft.maps.component[target.component].metadata.layout = undefined;
                });
            },
            addComponentShowCondition(target) {
                modify((draft) => {
                    if (draft.maps.component[target.component]) {
                        draft.maps.component[target.component].showCondition.conditions.push({
                            id: nanoid(),
                            formula: "true == true",
                        });
                    }
                });
            },
            addComponentDisableCondition(target) {
                modify((draft) => {
                    if (!draft.maps.component[target.component].disableCondition) {
                        draft.maps.component[target.component].disableCondition = {
                            type: "all",
                            conditions: [],
                        };
                    }
                    if (draft.maps.component[target.component]) {
                        draft.maps.component[target.component].disableCondition.conditions.push({
                            id: nanoid(),
                            formula: "true == true",
                        });
                    }
                });
            },
            updatePage(target, update) {
                modify((draft) => {
                    update(draft.maps.page[target.page]);
                });
            },
            updateComponentShowCondition(target, update) {
                modify((draft) => {
                    if (draft.maps.component[target.component]) {
                        update(draft.maps.component[target.component].showCondition);
                    }
                });
            },
            updateComponentDisableCondition(target, update) {
                modify((draft) => {
                    if (draft.maps.component[target.component]) {
                        update(draft.maps.component[target.component].disableCondition);
                    }
                });
            },
            removeComponentShowCondition(target, index) {
                modify((draft) => {
                    if (draft.maps.component[target.component]) {
                        draft.maps.component[target.component].showCondition.conditions =
                            draft.maps.component[target.component].showCondition.conditions.filter((cond, i) => i !== index);
                    }
                });
            },
            removeComponentDisableCondition(target, index) {
                modify((draft) => {
                    if (draft.maps.component[target.component]) {
                        draft.maps.component[target.component].disableCondition.conditions = draft.maps.component[target.component].disableCondition.conditions.filter((cond, i) => i !== index);
                    }
                });
            },
            setPageTitle(target, title) {
                modify((draft) => {
                    draft.maps.page[target.page].title = title;
                });
            },
            setShowNavbar(target, type, toggle) {
                modify((draft) => {
                    draft.maps.page[target.page].navbar[type].show = toggle;
                });
            },
            setSideTopNavbar(value) {
                modify((draft) => {
                    draft.navbar.sidetop.position = value;
                });
            },
            setCollapsible(value) {
                modify((draft) => {
                    draft.navbar.sidetop.collapsible = value;
                });
            },
            setBottomNavLabel(toggle) {
                modify((draft) => {
                    draft.navbar.bottom.showLabel = toggle;
                });
            },
            addBottomNav(nav) {
                modify((draft) => {
                    draft.navbar.bottom.content.push(nav);
                });
            },
            removeBottomNav(index) {
                modify((draft) => {
                    draft.navbar.bottom.content.splice(index, 1);
                });
            },
            setBottomNavContent(idx, key, value) {
                modify((draft) => {
                    draft.navbar.bottom.content[idx][key] = value;
                });
            },
            swapBottomNavContent(source, destination) {
                modify((draft) => {
                    const newNav = [...draft.navbar.bottom.content];
                    const targetNav = newNav[source];
                    newNav.splice(source, 1);
                    newNav.splice(destination, 0, targetNav);
                    draft.navbar.bottom.content = newNav;
                });
            },
            swapSidetopNavContent(newTree) {
                modify((draft) => {
                    draft.navbar.sidetop.content = newTree;
                });
            },
            addSidetopNav(newNav) {
                modify((draft) => {
                    draft.navbar.sidetop.content.items.root.children.push(newNav.id);
                    draft.navbar.sidetop.content.items[newNav.id] = newNav;
                });
            },
            setSidetopNavbarContent(navId, key, value) {
                modify((draft) => {
                    draft.navbar.sidetop.content.items[navId].data[key] = value;
                });
            },
            removeSidetopNav(nav) {
                modify((draft) => {
                    const sideTop = Object.assign({}, draft.navbar.sidetop.content.items);
                    if (nav.hasChildren) {
                        sideTop[nav.id].children.forEach((id) => {
                            if (sideTop[id].hasChildren) {
                                sideTop[id].children.forEach((subId) => delete sideTop[subId]);
                            }
                            delete sideTop[id];
                        });
                    }
                    delete sideTop[nav.id];
                    for (const key in sideTop) {
                        if (sideTop[key].children.find((e) => e === nav.id)) {
                            const idx = sideTop[key].children.findIndex((e) => e === nav.id);
                            sideTop[key].children.splice(idx, 1);
                            if (sideTop[key].children.length === 0) {
                                sideTop[key].hasChildren = false;
                                sideTop[key].isExpanded = false;
                            }
                        }
                    }
                    draft.navbar.sidetop.content.items = sideTop;
                });
            },
        };
    }
    catch (error) { }
}, { enableHistory: true, trackChanges: true, trackPaths: ["app"] });
//# sourceMappingURL=RuntimeContext.js.map