"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.format = void 0;
const index_1 = require("@/constants/index");
const utils_1 = require("../utils");
/**
 * format
 * for visual editor render use
 * @param data
 * @returns
 */
const format = (data, opt) => {
    var _a, _b;
    const { content } = data;
    const { origin, extend, mocks = [], components = [] } = opt;
    // prepare
    const templates = (((_a = origin.relations) === null || _a === void 0 ? void 0 : _a.templates) || []).concat(((_b = extend === null || extend === void 0 ? void 0 : extend.relations) === null || _b === void 0 ? void 0 : _b.templates) || []);
    const { nodeMap: templateNodeMap } = prepareTemplate(templates);
    const { nodeMap: originPageNodeMap } = preparePage(origin);
    const { nodeMap: extendPageNodeMap } = extend && extend.id ? preparePage(extend) : { nodeMap: {} };
    const { idMap: idMockMap } = prepareMock(mocks);
    const { componentMap } = componentsMap(components);
    // format content.schemas
    const formattedSchemas = formatSchemas(content.schemas, {
        templateNodeMap,
        originPageNodeMap,
        extendPageNodeMap,
        idMockMap,
        componentMap,
        pageContent: origin,
    });
    return {
        formattedSchemas,
        templateNodeMap,
        originPageNodeMap,
        extendPageNodeMap,
        idMockMap,
        componentMap,
    };
};
exports.format = format;
const formatSchemas = (schemas = [], opt) => {
    var _a;
    const { templateNodeMap, extendPageNodeMap, idMockMap, pageContent } = opt;
    const mockEnable = !!((_a = pageContent.mock) === null || _a === void 0 ? void 0 : _a.enable);
    function doFormat(structures = []) {
        const list = [];
        structures.forEach((item) => {
            var _a;
            const renderNode = Object.assign({}, item);
            const templateNode = templateNodeMap[item.id];
            const styleNode = (0, utils_1.getStyleWrapper)(item, opt);
            renderNode.props = item.__parsedProps || {};
            // children & not extend removed
            if (item.children && item.name !== index_1.BLANK_NODE) {
                renderNode.children = doFormat(item.children);
                renderNode.childIds = renderNode.children.map((item) => item.id);
            }
            else {
                renderNode.children = [];
                renderNode.childIds = [];
            }
            renderNode.__renderProps = item.props;
            renderNode.__styleNode = styleNode;
            renderNode.__editorConfig = {
                visible: item.name !== index_1.BLANK_NODE,
                showInStructure: !templateNode && item.name !== index_1.STYLE_CONTAINER && item.type !== 'page',
                editable: !templateNode,
                moveable: !((_a = item.extension) === null || _a === void 0 ? void 0 : _a.extendId) && !extendPageNodeMap[item.id],
                directiveable: false,
                styleable: !!styleNode,
            };
            if (mockEnable) {
                const mockNode = (getNodeMock(item, { idMockMap }) || (0, utils_1.initMockNode)(item));
                mockNode.__editorConfig = {
                    editable: true,
                };
                renderNode.__mock = mockNode;
            }
            list.push(renderNode);
        });
        return list;
    }
    return doFormat(schemas);
};
const prepareTemplate = (templates = []) => {
    let map = {};
    templates.forEach((item) => {
        const result = (0, utils_1.structureToNodeMap)(item.schemas);
        map = Object.assign(Object.assign({}, map), result);
    });
    return { nodeMap: map };
};
const preparePage = (page) => {
    var _a;
    let map = {};
    map = (0, utils_1.structureToNodeMap)(((_a = page.content) === null || _a === void 0 ? void 0 : _a.schemas) || []);
    return { nodeMap: map };
};
const prepareMock = (mocks = []) => {
    let idMap = {};
    mocks.forEach((item) => {
        var _a;
        (_a = item.schemas) === null || _a === void 0 ? void 0 : _a.forEach((node) => {
            if (node.id) {
                idMap[node.id] = node;
            }
        });
    });
    return { idMap };
};
const getNodeMock = (node, opt) => {
    const { id } = node;
    const idMock = opt.idMockMap[id];
    return idMock;
};
const componentsMap = (components = []) => {
    const map = {};
    components.forEach((item) => {
        map[item.name] = item;
    });
    return { componentMap: map };
};
