"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.isLayoutEqual = exports.isPivotLayoutEqual = exports.isTableLayoutEqual = exports.logChanges = exports.getChanges = void 0;
const tslib_1 = require("tslib");
const isEqual_1 = tslib_1.__importDefault(require("lodash/isEqual"));
const infinite_react_1 = require("@infinite-table/infinite-react");
const isPivotLayoutModel_1 = require("./isPivotLayoutModel");
const normalizeLayoutModel_1 = require("./normalizeLayoutModel");
const logger = (0, infinite_react_1.debug)(`LayoutManager:Diffing`);
const getChanges = (l1, l2) => {
    if (!l1 || typeof l1 !== 'object' || !l2 || typeof l2 !== 'object') {
        return [
            {
                key: 'missing',
                value1: l1,
                value2: l2,
            },
        ];
    }
    const k1 = Object.keys(l1);
    const k2 = Object.keys(l2);
    const allKeys = new Set([...k1, ...k2]);
    const changes = [];
    for (const key of allKeys) {
        if (!(0, isEqual_1.default)(l1[key], l2[key])) {
            changes.push({ key, value1: l1[key], value2: l2[key] });
        }
    }
    return changes;
};
exports.getChanges = getChanges;
const logChanges = (l1, l2, log) => {
    log = log || logger;
    const changes = (0, exports.getChanges)(l1, l2);
    log('---');
    log('Layouts are not equal.');
    log('Layout 1', l1);
    log('Layout 2', l2);
    log('Differences:', changes);
    log('---');
};
exports.logChanges = logChanges;
function isTableLayoutEqual(l1, l2) {
    l1 = (0, normalizeLayoutModel_1.normalizeTableLayoutModel)(l1);
    l2 = (0, normalizeLayoutModel_1.normalizeTableLayoutModel)(l2);
    clearIgnoredProperties(l1);
    clearIgnoredProperties(l2);
    const res = (0, isEqual_1.default)(l1, l2);
    if (!res) {
        (0, exports.logChanges)(l1, l2);
    }
    return res;
}
exports.isTableLayoutEqual = isTableLayoutEqual;
//#clearIgnoredProperties
function clearIgnoredProperties(layout) {
    delete layout.Ignore_GridFilter;
    delete layout.Ignore_ColumnFilters;
    delete layout.Ignore_ColumnHeaders;
    delete layout.Ignore_AutoSizeColumns;
    delete layout.Ignore_RowSummaries;
    delete layout.Ignore_IsReadOnly;
    delete layout.Ignore_Tags;
    delete layout.Ignore_Source;
    delete layout.Ignore_AdaptableVersion;
    delete layout.Ignore_Uuid;
}
function isPivotLayoutEqual(l1, l2) {
    l1 = (0, normalizeLayoutModel_1.normalizePivotLayoutModel)(l1);
    l2 = (0, normalizeLayoutModel_1.normalizePivotLayoutModel)(l2);
    clearIgnoredProperties(l1);
    clearIgnoredProperties(l2);
    const res = (0, isEqual_1.default)(l1, l2);
    if (!res) {
        (0, exports.logChanges)(l1, l2);
    }
    return res;
}
exports.isPivotLayoutEqual = isPivotLayoutEqual;
function isLayoutEqual(l1, l2) {
    if ((0, isPivotLayoutModel_1.isPivotLayoutModel)(l1) != (0, isPivotLayoutModel_1.isPivotLayoutModel)(l2)) {
        return false;
    }
    if ((0, isPivotLayoutModel_1.isPivotLayoutModel)(l1) && (0, isPivotLayoutModel_1.isPivotLayoutModel)(l2)) {
        return isPivotLayoutEqual(l1, l2);
    }
    if (!(0, isPivotLayoutModel_1.isPivotLayoutModel)(l1) && !(0, isPivotLayoutModel_1.isPivotLayoutModel)(l2)) {
        return isTableLayoutEqual(l1, l2);
    }
    return false;
}
exports.isLayoutEqual = isLayoutEqual;
