"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.isNode = exports.updateMockContent = exports.updateContent = void 0;
const index_1 = require("../services/index");
const utils_1 = require("../utils");
/**
 * update dsl with update nodes
 * @param list update nodes
 * @param formattedData
 * @returns schemas
 */
const updateContent = (list = [], formattedData) => {
    console.log('[ UPDATE CONTENT ]:', list);
    const formatted = Object.assign({}, formattedData);
    // node mapper
    const mapper = (item) => {
        var _a, _b, _c, _d;
        const { id, extension } = item;
        const { extendId = '' } = extension || {};
        const node = formatted.originPageNodeMap[id];
        let nodeParentId = '';
        let nodeId = '';
        const extendNode = formatted.extendPageNodeMap[extendId];
        const baseNode = formatted.extendPageNodeMap[id];
        let newNode = (0, utils_1.pickNode)(item); // updated
        // get new node (contain extend logic)
        if (baseNode) {
            const diffed = (0, index_1.nodeDiff)(baseNode, newNode);
            if (diffed) {
                diffed.extension.parentId = ((_a = baseNode.extension) === null || _a === void 0 ? void 0 : _a.parentId) || '';
                newNode = diffed;
            }
        }
        else if (extendNode) {
            const baseNode = formatted.extendPageNodeMap[extendId];
            const diffed = (0, index_1.nodeDiff)(baseNode, newNode);
            if (diffed) {
                diffed.extension.parentId = ((_b = extendNode.extension) === null || _b === void 0 ? void 0 : _b.parentId) || '';
                newNode = Object.assign(Object.assign({}, diffed), { id: newNode.id });
            }
        }
        else if (node) {
            nodeParentId = node.extension.parentId || '';
            nodeId = node.id;
            newNode = Object.assign(Object.assign({}, node), newNode);
        }
        // for move cross level
        if (newNode.extension.parentId !== nodeParentId) {
            const oldParent = formatted.originPageNodeMap[nodeParentId];
            if (oldParent) {
                const childIds = ((_c = oldParent.childIds) === null || _c === void 0 ? void 0 : _c.filter((item) => item !== nodeId)) || [];
                const _oldParent = Object.assign(Object.assign({}, oldParent), { childIds });
                formatted.originPageNodeMap = Object.assign(Object.assign({}, formatted.originPageNodeMap), { [_oldParent.id]: _oldParent });
            }
        }
        // update with parent node
        const parent = formatted.originPageNodeMap[newNode.extension.parentId || ''];
        if (parent) {
            const childIds = Array.from(new Set((_d = parent.childIds) === null || _d === void 0 ? void 0 : _d.concat([newNode.id])));
            const newParent = Object.assign(Object.assign({}, parent), { childIds });
            formatted.originPageNodeMap = Object.assign(Object.assign({}, formatted.originPageNodeMap), { [newParent.id]: newParent, [newNode.id]: newNode });
        }
        else {
            formatted.originPageNodeMap = Object.assign(Object.assign({}, formatted.originPageNodeMap), { [newNode.id]: newNode });
        }
    };
    list.forEach((item) => mapper(item));
    console.log(formatted.originPageNodeMap);
    // to generate new content
    const newContent = (0, utils_1.mapToTree)(formatted.originPageNodeMap);
    console.log(newContent);
    return newContent;
};
exports.updateContent = updateContent;
/**
 * update mock content
 * @param list
 * @param formattedData
 * @returns
 */
const updateMockContent = (list = [], mock) => {
    console.log('[ UPDATE MOCK CONTENT ]:', list);
    const schemas = [...(mock.schemas || [])];
    // node mapper
    const mapper = (item) => {
        const { id } = item;
        const idx = schemas.findIndex((node) => node.id === id);
        if (idx > -1) {
            schemas.splice(idx, 1, Object.assign(Object.assign({}, schemas[idx]), item));
        }
        else {
            schemas.push(item);
        }
    };
    list.forEach((item) => mapper(item));
    return schemas;
};
exports.updateMockContent = updateMockContent;
/**
 * check is node or mock node
 * @param node
 * @returns
 */
const isNode = (node) => {
    return !!node.extension || node.label || node.type || node.version;
};
exports.isNode = isNode;
