"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.removeComponents = void 0;
const index_1 = require("@/constants/index");
const utils_1 = require("../utils");
/**
 * update content with remove nodes
 * @param list removed list
 * @param opt
 */
const removeComponents = (list = [], opt) => {
    let formatted = opt.formattedData;
    const { updates, removes } = initUpdatesAndRemoves(list, formatted);
    // remove in formatted data
    if (removes.length > 0) {
        formatted = removeFormattedData(removes, formatted);
    }
    return { formattedData: formatted, updates };
};
exports.removeComponents = removeComponents;
const removeFormattedData = (list = [], formattedData) => {
    const formatted = Object.assign({}, formattedData);
    // remove in formatted schemas
    // formatted.formattedSchemas = removeSchemas(list, formatted.formattedSchemas);
    // remove in origin page
    removeOriginPage(list, formatted);
    // remove in id mock
    removeMock(list, formatted);
    return formatted;
};
// const removeSchemas = (list: StructureNode[] = [], schemas: RenderStructureNode[]) => {
//   let structures = schemas;
//   list.forEach((item) => {
//     structures = removeStructure(structures, item.id);
//   });
//   return structures;
// };
const removeOriginPage = (list = [], formattedData) => {
    const cloned = Object.assign({}, formattedData.originPageNodeMap);
    list.forEach((item) => {
        delete cloned[item.id];
        formattedData.originPageNodeMap = cloned;
    });
};
const removeMock = (list = [], formattedData) => {
    const cloned = Object.assign({}, formattedData.idMockMap);
    list.forEach((item) => {
        delete cloned[item.id];
        formattedData.idMockMap = cloned;
    });
};
const initUpdatesAndRemoves = (list, formattedData) => {
    const updates = [];
    const removes = [];
    const mapper = (item, opt) => {
        const isExtendNode = !!item.extension.extendId;
        const baseNode = formattedData.extendPageNodeMap[item.id];
        if (isExtendNode) {
            // if blank node will rollback
            if (item.name === index_1.BLANK_NODE) {
                removes.push(item);
            }
            else {
                // update extend node to blank node
                updates.push(Object.assign(Object.assign({}, item), { name: index_1.BLANK_NODE }));
            }
        }
        else if (baseNode) {
            // new blank node
            updates.push(Object.assign(Object.assign({}, (0, utils_1.initNode)({ name: index_1.BLANK_NODE, label: opt.label || baseNode.label })), { extension: { extendId: item.id } }));
        }
        else {
            getRemoves(item, removes, formattedData.originPageNodeMap);
        }
    };
    list.forEach((item) => {
        const styleWrapper = (0, utils_1.getStyleWrapper)(item, formattedData);
        if (styleWrapper) {
            mapper(styleWrapper, { label: item.label });
        }
        else {
            mapper(item, {});
        }
    });
    return { updates, removes };
};
const getRemoves = (node, list = [], originPageNodeMap) => {
    const { childIds = [] } = node;
    list.push(node);
    if (childIds.length > 0) {
        childIds.forEach((item) => {
            const node = originPageNodeMap[item];
            const { __styleNode } = node;
            if (__styleNode && __styleNode.id) {
                getRemoves(__styleNode, list, originPageNodeMap);
            }
            else {
                getRemoves(node, list, originPageNodeMap);
            }
        });
    }
};
