"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.copyComponents = void 0;
const build_1 = require("@/constants/build");
const utils_1 = require("../utils");
const drop_1 = require("./drop");
/**
 * copy structure
 * if had children, will dfs to copy too
 * @param list need to copy component
 * @param opt copy options
 * @returns copied
 */
const copyComponents = (list = [], opt) => {
    const { formattedData } = opt;
    let effects = [];
    const mapper = (item) => {
        const result = copy([item], { parentId: item.extension.parentId || '' })[0];
        if (result) {
            const placementEffects = (0, drop_1.afterPlacement)(result, item, formattedData);
            effects = effects.concat(placementEffects);
        }
    };
    list.forEach((item) => {
        let styleContainer = (0, utils_1.getStyleWrapper)(item, formattedData);
        if (styleContainer) {
            styleContainer = (0, utils_1.findStructureById)(formattedData.formattedSchemas, styleContainer.id);
        }
        mapper((styleContainer || item));
    });
    return effects;
};
exports.copyComponents = copyComponents;
const copy = (data = [], opt) => {
    const list = [];
    data.forEach((node) => {
        var _a;
        if (node.name !== build_1.BLANK_NODE) {
            const id = (0, utils_1.generateStructureId)();
            const extension = Object.assign(Object.assign({}, (node.extension || {})), { parentId: opt.parentId });
            const item = Object.assign(Object.assign({}, node), { id, extension });
            // delete extendId
            (_a = item.extension) === null || _a === void 0 ? true : delete _a.extendId;
            if (item.children) {
                item.children = copy(item.children, { parentId: id });
                // TODO:
                item.childIds = item.children.map((item) => item.id);
            }
            list.push(item);
        }
    });
    return list;
};
