"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DeltaField = void 0;
var react_1 = __importDefault(require("react"));
var classnames_1 = __importDefault(require("classnames"));
var mdm_sdk_1 = require("@reltio/mdm-sdk");
var EntityUriLink_1 = require("../../../../../EntityUriLink");
var Chip_1 = __importDefault(require("@mui/material/Chip"));
var ArrowForward_1 = __importDefault(require("@mui/icons-material/ArrowForward"));
var types_1 = require("../../../types");
var activities_1 = require("../../../utils/activities");
var MdmModuleContext_1 = require("../../../../../contexts/MdmModuleContext");
var Field_1 = require("../../../Field");
var DeltaField_module_css_1 = __importDefault(require("./DeltaField.module.css"));
var DeltaField = function (_a) {
    var _b, _c;
    var delta = _a.delta;
    var metadata = (0, MdmModuleContext_1.useMdmMetadata)();
    var attributesPresentation = (0, MdmModuleContext_1.useMdmAttributesPresentation)();
    var isMemberDelta = delta.type in types_1.MemberDeltaTypes;
    var attributeType = !isMemberDelta ? (0, mdm_sdk_1.findAttributeTypeByUri)(metadata, delta.attributeType) : undefined;
    var memberType = isMemberDelta ? (0, mdm_sdk_1.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 ? types_1.MemberDeltaTypes : types_1.DeltaTypes)[delta.type];
    var converter = function (deltaValue) {
        return isMemberDelta ? (react_1.default.createElement(EntityUriLink_1.EntityUriLink, { value: 'entities/' + deltaValue, className: DeltaField_module_css_1.default.entityLink }, deltaValue)) : ((0, activities_1.stringifyDeltaValue)(deltaValue, delta.attributeType, metadata, attributesPresentation));
    };
    return (react_1.default.createElement(Field_1.Field, { fieldName: fieldName, "data-reltio-id": "delta-field" },
        react_1.default.createElement("span", null,
            value && converter(value),
            newValue && oldValue && (react_1.default.createElement(react_1.default.Fragment, null,
                react_1.default.createElement(ArrowForward_1.default, { className: DeltaField_module_css_1.default.arrow }),
                converter(newValue)))),
        deltaLabel && (react_1.default.createElement(Chip_1.default, { variant: "outlined", label: deltaLabel, className: (0, classnames_1.default)(DeltaField_module_css_1.default.chip, DeltaField_module_css_1.default[deltaLabel]) }))));
};
exports.DeltaField = DeltaField;
