"use strict";
/* block区块添加方法类 */
var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.transformBlockToJson = exports.getBlockFromChildId = exports.judgeIfBlockElement = exports.signBlockJson = void 0;
const get_1 = require("lodash/get");
const cloneDeep_1 = require("lodash/cloneDeep");
const xmlToJson_1 = require("./xmlToJson");
const util_1 = require("./util");
// 区块blockJson加入编辑器前处理
const signBlockJson = (json, blockInfo) => {
    const newBlock = (0, cloneDeep_1.default)(json);
    if (!newBlock.data) {
        newBlock.data = {};
    }
    newBlock.data.blockSettings = {
        isBlock: true,
    };
    newBlock.name = blockInfo.name;
    // 区块内的静态内容不可点击和调整，动态placeholder及其内部children可增删改
    const mapChildren = (element, parent) => {
        var _a, _b;
        const { elements = [], type } = element;
        delete element.id;
        if (!element.data) {
            element.data = {};
        }
        if (!element.data.blockSettings) {
            element.data.blockSettings = {};
        }
        if (type === 'placeholder') {
            element.data.blockSettings.canEdit = true;
        }
        else if (parent) {
            element.data.blockSettings.canEdit = (_b = (_a = parent.data) === null || _a === void 0 ? void 0 : _a.blockSettings) === null || _b === void 0 ? void 0 : _b.canEdit;
        }
        else {
            element.data.blockSettings.canEdit = false;
        }
        elements.forEach((child) => {
            mapChildren(child, element);
        });
    };
    mapChildren(newBlock);
    return newBlock;
};
exports.signBlockJson = signBlockJson;
// 判断当前元素是否为block元素
const judgeIfBlockElement = (element) => {
    var _a;
    if (element.type === 'document') {
        const { isBlock } = ((_a = element.data) === null || _a === void 0 ? void 0 : _a.blockSettings) || {};
        return isBlock;
    }
    if (element.data && element.data.blockSettings
        && Object.prototype.hasOwnProperty.call(element.data.blockSettings, 'canEdit')) {
        return true;
    }
    return false;
};
exports.judgeIfBlockElement = judgeIfBlockElement;
// 根据childElement查找block
const getBlockFromChildId = (json, childElement) => {
    var _a, _b;
    if (!childElement.path) {
        return null;
    }
    const parentPaths = childElement.path.split('.').slice(0, -1);
    if (parentPaths.length <= 1) {
        return null;
    }
    const parentElement = (0, get_1.default)(json, parentPaths.join('.elements.').slice(2));
    if (parentElement && parentElement.type === 'document' && ((_b = (_a = parentElement.data) === null || _a === void 0 ? void 0 : _a.blockSettings) === null || _b === void 0 ? void 0 : _b.isBlock)) {
        return parentElement;
    }
    return (0, exports.getBlockFromChildId)(json, parentElement);
};
exports.getBlockFromChildId = getBlockFromChildId;
// 将block转换为真实json
const transformBlockToJson = (data, allBlocks) => {
    let newJson = {};
    const commonElementsInBlock = [];
    const recurseFun = (element, json) => {
        let targetJson = {};
        const { for: forBlock, elements = [] } = element, restAttrs = __rest(element, ["for", "elements"]);
        const isBlockElement = (0, exports.judgeIfBlockElement)(element);
        if (element.type === 'document') {
            if (isBlockElement) {
                // 当前element为block元素
                const { name } = element;
                const { metaData } = allBlocks.find(v => v.name === name) || {};
                if (metaData) {
                    const blockJson = (0, exports.signBlockJson)((0, xmlToJson_1.xmlToJson)(metaData), { name });
                    blockJson.path = element.path;
                    if (!json.elements) {
                        json.elements = [];
                    }
                    json.elements.push(blockJson);
                    targetJson = json;
                }
            }
            else {
                json.elements = [];
                Object.keys(restAttrs).forEach(key => {
                    json[key] = restAttrs[key];
                });
                targetJson = json;
            }
        }
        else if (forBlock) {
            // block内的普通元素
            commonElementsInBlock.push(element);
            targetJson = json;
        }
        else if (!isBlockElement) {
            // 页面内的普通元素
            json.elements.push(Object.assign(Object.assign({}, restAttrs), { elements: [] }));
            const { length } = json.elements;
            targetJson = json.elements[length - 1];
        }
        elements.forEach(item => {
            recurseFun(item, targetJson);
        });
    };
    recurseFun(data, newJson);
    (0, util_1.updateChildPaths)(newJson);
    if (commonElementsInBlock.length > 0) {
        // 根据path将页面元素添加至block内
        commonElementsInBlock.forEach(child => {
            newJson = (0, util_1.addElementByPath)(child, newJson, child.path);
        });
    }
    return newJson;
};
exports.transformBlockToJson = transformBlockToJson;
