"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.merge = exports.mergeRelations = void 0;
const foxpage_js_sdk_1 = require("@foxpage/foxpage-js-sdk");
/**
 * merge relations
 * @param base
 * @param current
 * @returns
 */
function mergeRelations(base, current) {
    return {
        templates: (base.templates || []).concat(current.templates || []),
        variables: (base.variables || []).concat(current.variables || []),
        conditions: (base.conditions || []).concat(current.conditions || []),
        functions: (base.functions || []).concat(current.functions || []),
    };
}
exports.mergeRelations = mergeRelations;
/**
 * merge content
 * @param current
 * @param base
 * @returns merged content
 */
function merge(current, base) {
    const { content: baseContent } = base;
    const { content: curContent } = current;
    if (!baseContent) {
        return current;
    }
    try {
        const mergedContent = !!baseContent.schemas && (curContent === null || curContent === void 0 ? void 0 : curContent.schemas)
            ? foxpage_js_sdk_1.merger.merge(baseContent, curContent, {
                strategy: foxpage_js_sdk_1.merger.MergeStrategy.COMBINE_BY_EXTEND,
            })
            : curContent;
        return Object.assign(Object.assign({}, current), { content: mergedContent, relations: mergeRelations((base === null || base === void 0 ? void 0 : base.relations) || {}, current.relations) });
    }
    catch (e) {
        console.error('mergeContent error:', e);
        return null;
    }
}
exports.merge = merge;
