import React from 'react';
import i18n from 'ui-i18n';
import { path } from 'ramda';
import { getEntityPreviousChange } from '@reltio/mdm-sdk';
import RestoreIcon from '@mui/icons-material/Restore';
import { ActivityTypes } from '../types';
import { EntityObject } from '../EntityObject';
import { Record } from '../Record';
import { RecordLabel } from '../RecordLabel';
import { RecordUpdates } from '../RecordUpdates';
import { useMdmAction } from '../../../contexts/MdmModuleContext';
import { useStyles } from './styles';
export var EntityRecord = function (_a) {
    var _b;
    var item = _a.item;
    var styles = useStyles();
    var openHistoryEvent = useMdmAction('openHistoryEvent');
    var deltaArray = (_b = item.deltaCollection) === null || _b === void 0 ? void 0 : _b.delta;
    var isEntityChanged = path(['data', 'type'])(item) === ActivityTypes.ENTITY_CHANGED;
    var handleOpenHistoryEvent = function () {
        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.createElement(Record, { className: styles.record },
        React.createElement(RecordLabel, { description: deltaArray ? '' : i18n.text('Profile changed') },
            React.createElement(EntityObject, { item: item })),
        isEntityChanged && (React.createElement(RestoreIcon, { "data-reltio-id": "reltio-entity-record-restore-icon", className: styles.restoreIcon, onClick: handleOpenHistoryEvent })),
        React.createElement(RecordUpdates, { updates: deltaArray, recordId: item.id })));
};
