"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useHistoryDiff = void 0;
var react_1 = require("react");
var ramda_1 = require("ramda");
var mdm_sdk_1 = require("@reltio/mdm-sdk");
var MdmModuleContext_1 = require("../../../contexts/MdmModuleContext");
var constants_1 = require("../constants");
var fixRefEntity = (0, ramda_1.evolve)({
    refEntity: mdm_sdk_1.wrapInArrayIfNeeded,
    refRelation: mdm_sdk_1.wrapInArrayIfNeeded
});
var prepareHistorySlice = function (entity) {
    if (entity) {
        return (0, ramda_1.evolve)({
            attributes: (0, ramda_1.map)((0, ramda_1.map)(fixRefEntity))
        })(entity);
    }
    return entity;
};
var useHistoryDiff = function () {
    var setHistoryDiff = (0, MdmModuleContext_1.useMdmAction)('setHistoryDiff');
    var clearHistoryDiff = (0, MdmModuleContext_1.useMdmAction)('clearHistoryDiff');
    var historySlice = (0, MdmModuleContext_1.useMdmHistorySlice)();
    var historyDiff = (0, MdmModuleContext_1.useMdmHistoryDiff)();
    var historyMode = (0, MdmModuleContext_1.useMdmHistoryMode)();
    var entity = (0, MdmModuleContext_1.useMdmEntity)();
    (0, react_1.useEffect)(function () {
        if (historySlice) {
            var aEntity = historySlice.aEntity, bEntity = historySlice.bEntity;
            var historyManager = mdm_sdk_1.entity.HistoryManager.createHistoryManager();
            var historyDiff_1 = historyManager.computeEntityHistoryDiff(prepareHistorySlice(historyMode === mdm_sdk_1.HistoryMode.Current ? entity : bEntity), prepareHistorySlice(aEntity));
            setHistoryDiff(historyDiff_1);
        }
        else {
            clearHistoryDiff();
        }
    }, [historySlice, historyMode, setHistoryDiff, entity]);
    return historyDiff || constants_1.DEFAULT_HISTORY_DIFF;
};
exports.useHistoryDiff = useHistoryDiff;
