"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.mergeContributes = mergeContributes;
const tslib_1 = require("tslib");
/**
 * handle `sumiContributes` and `contributes`
 */
const mergeWith_1 = tslib_1.__importDefault(require("lodash/mergeWith"));
const ide_core_common_1 = require("@opensumi/ide-core-common");
const { asArray } = ide_core_common_1.arrays;
function mergeContributes(contributes, sumiContributes) {
    if (contributes === undefined) {
        return sumiContributes || {};
    }
    if (sumiContributes === undefined) {
        return contributes || {};
    }
    return (0, mergeWith_1.default)(sumiContributes, contributes, (value, srcValue, key, object, source) => {
        if (value === undefined || srcValue === undefined) {
            return value || srcValue;
        }
        if (['menus', 'viewsContainers', 'views'].includes(key)) {
            const childKeySet = new Set(Object.keys(value).concat(Object.keys(srcValue)));
            const result = {};
            // 合并掉相同 menuId 下的 menu items
            for (const childKey of childKeySet) {
                result[childKey] = (value[childKey] || []).concat(srcValue[childKey] || []);
            }
            return result;
        }
        if (key === 'configuration') {
            value = asArray(value);
            srcValue = asArray(srcValue);
        }
        if (Array.isArray(value) && Array.isArray(srcValue)) {
            return value.concat(srcValue);
        }
    });
}
//# sourceMappingURL=merge-contributes.js.map