"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.initStyleWrapper = exports.getStyleWrapper = void 0;
const index_1 = require("@/constants/index");
const node_1 = require("./node");
// style container wrapper
/**
 * get style wrapper node
 * @param node current node
 * @param formattedData formatted data
 * @returns style node
 */
const getStyleWrapper = (node, formattedData) => {
    var _a;
    const { componentMap = {}, originPageNodeMap, extendPageNodeMap } = formattedData;
    const component = componentMap[node.name];
    if (!component) {
        return null;
    }
    const { useStyleEditor = false } = component;
    if (useStyleEditor) {
        const nodeParentId = ((_a = node.extension) === null || _a === void 0 ? void 0 : _a.parentId) || '';
        let parent = originPageNodeMap[nodeParentId];
        if (!parent) {
            // parent id had changed, will find from extend and origin
            parent = Object.values(originPageNodeMap || {}).find((item) => { var _a; return ((_a = item.extension) === null || _a === void 0 ? void 0 : _a.extendId) === nodeParentId; });
        }
        if (!parent) {
            parent = extendPageNodeMap[nodeParentId];
        }
        return parent && parent.name === index_1.STYLE_CONTAINER ? Object.assign({}, parent) : null;
    }
    return null;
};
exports.getStyleWrapper = getStyleWrapper;
/**
 * init style node
 * @param node current node
 * @param componentMap component map
 * @returns styled node
 */
const initStyleWrapper = (node, componentMap) => {
    const styleContainerComponent = componentMap[index_1.STYLE_CONTAINER];
    if (styleContainerComponent) {
        const styleNode = (0, node_1.initNode)(styleContainerComponent);
        node.extension.parentId = styleNode.id;
        styleNode.children = [node];
        return styleNode;
    }
    return node;
};
exports.initStyleWrapper = initStyleWrapper;
