"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DiffRenderer = void 0;
var react_1 = __importStar(require("react"));
var mdm_sdk_1 = require("@reltio/mdm-sdk");
var ChangeView_1 = require("../ChangeView");
var common_1 = require("../../../helpers/common");
var useCssVariableStyles_1 = require("../../../../../hooks/useCssVariableStyles");
var MdmModuleContext_1 = require("../../../../../contexts/MdmModuleContext");
var DataTypeValue_1 = require("../../../../../DataTypeValue");
var RelationEntityLink_1 = require("../../../RelationEntityLink");
var DiffRenderer_module_css_1 = __importDefault(require("./DiffRenderer.module.css"));
var DiffRenderer = function (_a) {
    var label = _a.label, level = _a.level, attributeType = _a.attributeType, relationType = _a.relationType, attributeValue = _a.attributeValue, oldValue = _a.oldValue, newValue = _a.newValue, operation = _a.operation;
    var variableStyles = (0, useCssVariableStyles_1.useCssVariableStyles)({
        '--level': level
    });
    var metadata = (0, MdmModuleContext_1.useMdmMetadata)();
    var changeLabel = (0, react_1.useMemo)(function () {
        return (react_1.default.createElement("span", null,
            label,
            react_1.default.createElement(RelationEntityLink_1.RelationEntityLink, { attributeOrRelationType: attributeType || relationType, attributeValue: attributeValue })));
    }, [label, attributeType, relationType, attributeValue]);
    var renderValue = (0, react_1.useCallback)(function (value) {
        var attrValue = (0, mdm_sdk_1.getAttributeValue)(value);
        var isRolesOrTags = attributeType.uri === mdm_sdk_1.EntityAttrTypes.roles.uri || attributeType.uri === mdm_sdk_1.EntityAttrTypes.tags.uri;
        var preparedValue = isRolesOrTags
            ? (0, common_1.prepareRolesOrTagsValue)(metadata, attrValue, attributeType.uri)
            : attrValue;
        return (react_1.default.createElement(DataTypeValue_1.DataTypeValue, { value: preparedValue, dataTypeDefinition: (0, common_1.getDataTypeDefinition)(attributeType, attrValue) }));
    }, [attributeType, metadata]);
    return (react_1.default.createElement("div", { className: DiffRenderer_module_css_1.default.change, style: variableStyles },
        react_1.default.createElement(ChangeView_1.ChangeView, { label: changeLabel, oldValue: oldValue && renderValue(oldValue), newValue: newValue && renderValue(newValue), operation: operation })));
};
exports.DiffRenderer = DiffRenderer;
