"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DeployedStateEntityMerger = void 0;
const tslib_1 = require("tslib");
const inversify_1 = require("inversify");
const _ = require("lodash");
let DeployedStateEntityMerger = class DeployedStateEntityMerger {
    constructor() {
    }
    // public merge<T>(original: T, additional?: T | undefined): T {
    //   return this.merge(original as EnterpriseState, additional as EnterpriseState | undefined) as T;
    // }
    /** 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;
    }
    /** Mutates the original */
    mergeEntities(original, additional) {
        var _a, _b, _c, _d, _e;
        try {
            if (additional == undefined || _.isEmpty(additional))
                return original;
            if (additional.enterprise != original.enterprise)
                return original;
            // Append Environments
            if (additional.environmentsAsArray && (additional.environmentsAsArray.length > 0)) {
                original.environmentsAsArray = [...(_a = original.environmentsAsArray) !== null && _a !== void 0 ? _a : [], ...(_b = additional.environmentsAsArray) !== null && _b !== void 0 ? _b : []];
            }
            // Append enterprise versions
            if (additional.enterpriseVersions && (additional.enterpriseVersions.length > 0)) {
                original.enterpriseVersions = [...(_c = original.enterpriseVersions) !== null && _c !== void 0 ? _c : [], ...(_d = additional.enterpriseVersions) !== null && _d !== void 0 ? _d : []];
                original.enterpriseVersions = _.uniqBy(original.enterpriseVersions, x => x.version);
            }
            return original;
        }
        catch (err) {
            const errMsg = "Error merging CabinetAssetVersion instances: ".concat((_e = err.message) !== null && _e !== void 0 ? _e : "");
            throw new Error(errMsg);
        }
    }
};
DeployedStateEntityMerger = tslib_1.__decorate([
    (0, inversify_1.injectable)(),
    tslib_1.__metadata("design:paramtypes", [])
], DeployedStateEntityMerger);
exports.DeployedStateEntityMerger = DeployedStateEntityMerger;
//# sourceMappingURL=enterprise-state-entity-merger.js.map