"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.EntityRecord = void 0;
var react_1 = __importDefault(require("react"));
var ui_i18n_1 = __importDefault(require("ui-i18n"));
var ramda_1 = require("ramda");
var mdm_sdk_1 = require("@reltio/mdm-sdk");
var Restore_1 = __importDefault(require("@mui/icons-material/Restore"));
var types_1 = require("../types");
var EntityObject_1 = require("../EntityObject");
var Record_1 = require("../Record");
var RecordLabel_1 = require("../RecordLabel");
var RecordUpdates_1 = require("../RecordUpdates");
var MdmModuleContext_1 = require("../../../contexts/MdmModuleContext");
var styles_1 = require("./styles");
var EntityRecord = function (_a) {
    var _b;
    var item = _a.item;
    var styles = (0, styles_1.useStyles)();
    var openHistoryEvent = (0, MdmModuleContext_1.useMdmAction)('openHistoryEvent');
    var deltaArray = (_b = item.deltaCollection) === null || _b === void 0 ? void 0 : _b.delta;
    var isEntityChanged = (0, ramda_1.path)(['data', 'type'])(item) === types_1.ActivityTypes.ENTITY_CHANGED;
    var handleOpenHistoryEvent = function () {
        (0, mdm_sdk_1.getEntityPreviousChange)(item.objectUri, item.timestamp)
            .then(function (previousTimestamp) {
            if (previousTimestamp !== -1) {
                var historyEvent = {
                    aStamp: item.timestamp,
                    bStamp: previousTimestamp
                };
                openHistoryEvent({ entityUri: item.objectUri, historyEvent: historyEvent });
            }
        })
            .catch(function (err) { return console.warn(err); });
    };
    return (react_1.default.createElement(Record_1.Record, { className: styles.record },
        react_1.default.createElement(RecordLabel_1.RecordLabel, { description: deltaArray ? '' : ui_i18n_1.default.text('Profile changed') },
            react_1.default.createElement(EntityObject_1.EntityObject, { item: item })),
        isEntityChanged && (react_1.default.createElement(Restore_1.default, { "data-reltio-id": "reltio-entity-record-restore-icon", className: styles.restoreIcon, onClick: handleOpenHistoryEvent })),
        react_1.default.createElement(RecordUpdates_1.RecordUpdates, { updates: deltaArray, recordId: item.id })));
};
exports.EntityRecord = EntityRecord;
