import React from 'react';
import classnames from 'classnames';
import { findAttributeTypeByUri, findMemberTypeByUri } from '@reltio/mdm-sdk';
import { EntityUriLink } from '../../../../../EntityUriLink';
import Chip from '@mui/material/Chip';
import ArrowForwardIcon from '@mui/icons-material/ArrowForward';
import { DeltaTypes, MemberDeltaTypes } from '../../../types';
import { stringifyDeltaValue } from '../../../utils/activities';
import { useMdmMetadata, useMdmAttributesPresentation, useMdmDisableTimestampLocalization } from '../../../../../contexts/MdmModuleContext';
import { Field } from '../../../Field';
import styles from './DeltaField.module.css';
export var DeltaField = function (_a) {
    var _b, _c;
    var delta = _a.delta;
    var metadata = useMdmMetadata();
    var attributesPresentation = useMdmAttributesPresentation();
    var disableTimestampLocalization = useMdmDisableTimestampLocalization();
    var isMemberDelta = delta.type in MemberDeltaTypes;
    var attributeType = !isMemberDelta ? findAttributeTypeByUri(metadata, delta.attributeType) : undefined;
    var memberType = isMemberDelta ? findMemberTypeByUri(metadata, delta.attributeType) : undefined;
    var fieldType = isMemberDelta ? memberType : attributeType;
    var fieldName = (fieldType === null || fieldType === void 0 ? void 0 : fieldType.label) || (fieldType === null || fieldType === void 0 ? void 0 : fieldType.name);
    var newValue = (_b = delta.newValue) === null || _b === void 0 ? void 0 : _b.value;
    var oldValue = (_c = delta.oldValue) === null || _c === void 0 ? void 0 : _c.value;
    var value = oldValue || newValue;
    var deltaLabel = (isMemberDelta ? MemberDeltaTypes : DeltaTypes)[delta.type];
    var converter = function (deltaValue) {
        return isMemberDelta ? (React.createElement(EntityUriLink, { value: 'entities/' + deltaValue, className: styles.entityLink }, deltaValue)) : (stringifyDeltaValue(deltaValue, delta.attributeType, metadata, attributesPresentation, {
            disableTimestampLocalization: disableTimestampLocalization
        }));
    };
    return (React.createElement(Field, { fieldName: fieldName, "data-reltio-id": "delta-field" },
        React.createElement("span", null,
            value && converter(value),
            newValue && oldValue && (React.createElement(React.Fragment, null,
                React.createElement(ArrowForwardIcon, { className: styles.arrow }),
                converter(newValue)))),
        deltaLabel && (React.createElement(Chip, { variant: "outlined", label: deltaLabel, className: classnames(styles.chip, styles[deltaLabel]) }))));
};
