"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.GetRootMapResponseMerger = void 0;
const tslib_1 = require("tslib");
const inversify_1 = require("inversify");
const _ = require("lodash");
/** IMPORTANT!!!  Merges two `SingleResult<RootMap>` instances (NOT raw `RootMap` entities).
 */
let GetRootMapResponseMerger = class GetRootMapResponseMerger {
    constructor() {
    }
    /** Mutates the original. */
    merge(original, additional) {
        // Extract the entity from the response envelope
        let originalOut = original === null || original === void 0 ? void 0 : original.output;
        let additionalOut = additional === null || additional === void 0 ? void 0 : additional.output;
        // merge the entities into 1
        let ult = this.mergeEntities(originalOut, additionalOut);
        // rewrite the original output
        original.output = ult;
        return original;
    }
    /** (Strongly-typed implementation of the merge method)
     * Mutates the original */
    mergeEntities(original, additional) {
        var _a, _b, _c;
        try {
            if (additional == undefined || _.isEmpty(additional))
                return original;
            // Append clusters
            if (additional.clusters && (additional.clusters.length > 0)) {
                original.clusters = [...(_a = original.clusters) !== null && _a !== void 0 ? _a : [], ...(_b = additional.clusters) !== null && _b !== void 0 ? _b : []];
            }
            // append environments and cabinets
            if (additional.enterprises && additional.enterprises.length > 0) {
                for (let ent of additional.enterprises) {
                    if (ent.environments && ent.environments.length > 0) {
                        let origEnt = original.enterprises.find(e => e.name == ent.name);
                        if (origEnt != undefined) {
                            origEnt.environments.push(...ent.environments);
                        }
                        else {
                            origEnt = ent;
                            original.enterprises.push(origEnt);
                        }
                    }
                }
            }
            return original;
        }
        catch (err) {
            const errMsg = "Error merging CabinetAssetVersion instances: ".concat((_c = err.message) !== null && _c !== void 0 ? _c : "");
            throw new Error(errMsg);
        }
    }
};
GetRootMapResponseMerger = tslib_1.__decorate([
    (0, inversify_1.injectable)(),
    tslib_1.__metadata("design:paramtypes", [])
], GetRootMapResponseMerger);
exports.GetRootMapResponseMerger = GetRootMapResponseMerger;
//# sourceMappingURL=root-map-entity-merger.js.map