"use strict";
var __values = (this && this.__values) || function(o) {
    var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
    if (m) return m.call(o);
    if (o && typeof o.length === "number") return {
        next: function () {
            if (o && i >= o.length) o = void 0;
            return { value: o && o[i++], done: !o };
        }
    };
    throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.objectMerger = void 0;
function deepMerge(target, source) {
    var diff = {};
    innerDeepMerge(target, source, diff);
    return diff;
}
function innerDeepMerge(target, source, diff) {
    var e_1, _a, e_2, _b;
    var anyChange = false;
    try {
        for (var _c = __values(Object.keys(source)), _d = _c.next(); !_d.done; _d = _c.next()) {
            var key = _d.value;
            if (Array.isArray(source[key])) {
                target[key] = target[key] || [];
                var arrayDiff = [];
                var anyChangeInArray = false;
                var targetItems = [];
                var sourceItemIndex = 0;
                var _loop_1 = function (sourceItem) {
                    var targetItemIndex = void 0;
                    // For arrays, match by index directly to avoid duplicate matching issues
                    // e.g., [[1], [1]] should match positionally, not find first occurrence
                    if (Array.isArray(sourceItem)) {
                        var targetItemAtIndex = target[key][sourceItemIndex];
                        targetItemIndex =
                            targetItemAtIndex !== undefined && compare(targetItemAtIndex, sourceItem)
                                ? sourceItemIndex
                                : -1;
                    }
                    else {
                        targetItemIndex = target[key].findIndex(function (item) { return compare(item, sourceItem); });
                    }
                    if (sourceItem && typeof sourceItem === 'object' && !Array.isArray(sourceItem)) {
                        var targetItem = targetItemIndex > -1 ? target[key][targetItemIndex] : {};
                        var innerDiff = {};
                        var anyInnerChange = innerDeepMerge(targetItem, sourceItem, innerDiff);
                        if (targetItemIndex === sourceItemIndex) {
                            arrayDiff.push(innerDiff);
                            anyChangeInArray = anyChangeInArray || anyInnerChange;
                        }
                        else {
                            arrayDiff.push(target[key][sourceItemIndex]);
                            anyChangeInArray = true;
                        }
                        anyChange = anyChange || anyChangeInArray;
                        targetItems.push(targetItem);
                    }
                    else {
                        if (targetItemIndex === sourceItemIndex) {
                            arrayDiff.push(null);
                        }
                        else {
                            if (sourceItemIndex < target[key].length) {
                                arrayDiff.push(target[key][sourceItemIndex]);
                            }
                            anyChangeInArray = true;
                            anyChange = true;
                        }
                        targetItems.push(sourceItem);
                    }
                    sourceItemIndex++;
                };
                try {
                    for (var _e = (e_2 = void 0, __values(source[key])), _f = _e.next(); !_f.done; _f = _e.next()) {
                        var sourceItem = _f.value;
                        _loop_1(sourceItem);
                    }
                }
                catch (e_2_1) { e_2 = { error: e_2_1 }; }
                finally {
                    try {
                        if (_f && !_f.done && (_b = _e.return)) _b.call(_e);
                    }
                    finally { if (e_2) throw e_2.error; }
                }
                for (var i = source[key].length; i < target[key].length; i++) {
                    arrayDiff.push(target[key][i]);
                    anyChangeInArray = true;
                    anyChange = true;
                }
                if (anyChangeInArray) {
                    diff[key] = arrayDiff;
                }
                target[key] = targetItems;
            }
            else if (source[key] && typeof source[key] === 'object') {
                target[key] = target[key] || {};
                var innerDiff = {};
                var anyInnerChange = innerDeepMerge(target[key], source[key], innerDiff);
                if (anyInnerChange) {
                    diff[key] = innerDiff;
                    anyChange = true;
                }
            }
            else {
                if (source[key] !== target[key]) {
                    diff[key] = target[key];
                    anyChange = true;
                }
                target[key] = source[key];
            }
        }
    }
    catch (e_1_1) { e_1 = { error: e_1_1 }; }
    finally {
        try {
            if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
        }
        finally { if (e_1) throw e_1.error; }
    }
    return anyChange;
}
var objectWithId = function (obj) { return obj && typeof obj === 'object' && obj.id !== undefined && obj.id !== null; };
function compare(a, b) {
    if ((!a && b) || (a && !b)) {
        return false;
    }
    if (Array.isArray(a) && Array.isArray(b)) {
        if (a.length !== b.length) {
            return false;
        }
        return a.every(function (item, index) { return compare(item, b[index]); });
    }
    if (objectWithId(a) && objectWithId(b)) {
        return a.id === b.id;
    }
    return a === b;
}
exports.objectMerger = { deepMerge: deepMerge };
//# sourceMappingURL=object-merger.js.map