"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.ChangeItemEditor = void 0;
var react_1 = __importStar(require("react"));
var classnames_1 = __importDefault(require("classnames"));
var ramda_1 = require("ramda");
var ui_i18n_1 = __importDefault(require("ui-i18n"));
var Replay_1 = __importDefault(require("@mui/icons-material/Replay"));
var Button_1 = __importDefault(require("@mui/material/Button"));
var mdm_sdk_1 = require("@reltio/mdm-sdk");
var MdmModuleContext_1 = require("../../../../../contexts/MdmModuleContext");
var DataTypeValue_1 = require("../../../../../DataTypeValue");
var SmallIconButton_1 = require("../../../../../SmallIconButton");
var common_1 = require("../../../helpers/common");
var helpers_1 = require("../../helpers/helpers");
var OperationChip_1 = require("../../../OperationChip");
var RelationEntityLink_1 = require("../../../RelationEntityLink");
var useChangeRequestEditor_1 = require("../../hooks/useChangeRequestEditor");
var ChangeItemLabel_1 = require("../ChangeItemLabel");
var DCRValueEditor_1 = require("../DCRValueEditor");
var StatusChips_1 = require("../StatusChips");
var ChangeItemEditor_module_css_1 = __importDefault(require("./ChangeItemEditor.module.css"));
var ChangeItemEditor = function (_a) {
    var _b, _c, _d;
    var change = _a.change, _e = _a.editable, editable = _e === void 0 ? true : _e, classes = _a.classes;
    var metadata = (0, MdmModuleContext_1.useMdmMetadata)();
    var _f = (0, useChangeRequestEditor_1.useChangeRequestEditor)({ change: change }), handleChange = _f.handleChange, handleReject = _f.handleReject, handleRevert = _f.handleRevert, lineData = _f.lineData, isLineRejected = _f.isLineRejected, isParentLineRejected = _f.isParentLineRejected, isUpdated = _f.isUpdated, hasChangeId = _f.hasChangeId, isSubAttributeOfDeletedAttribute = _f.isSubAttributeOfDeletedAttribute;
    var entity = (0, MdmModuleContext_1.useMdmEntity)();
    var entityType = (0, mdm_sdk_1.getEntityType)(metadata, entity === null || entity === void 0 ? void 0 : entity.type);
    var isUpdateOperation = change.operation === mdm_sdk_1.DCROperationTypes.EDITED;
    var isAddOperation = change.operation === mdm_sdk_1.DCROperationTypes.ADDED;
    var oldValue = (0, mdm_sdk_1.getAttributeValue)(change.oldValue);
    var currentNewValue = !(0, ramda_1.isNil)(lineData === null || lineData === void 0 ? void 0 : lineData.value) && !isParentLineRejected ? lineData.value : change.newValue;
    var newValue = (0, mdm_sdk_1.getAttributeValue)(currentNewValue);
    var hasNewValue = !(0, ramda_1.isNil)(newValue);
    var dataTypeDefinition = (0, react_1.useMemo)(function () {
        return change.attributeType && (0, common_1.getDataTypeDefinition)(change.attributeType, newValue || oldValue);
    }, [change.attributeType, newValue, oldValue]);
    var isRolesOrTags = ((_b = change === null || change === void 0 ? void 0 : change.attributeType) === null || _b === void 0 ? void 0 : _b.uri) === mdm_sdk_1.EntityAttrTypes.roles.uri ||
        ((_c = change === null || change === void 0 ? void 0 : change.attributeType) === null || _c === void 0 ? void 0 : _c.uri) === mdm_sdk_1.EntityAttrTypes.tags.uri;
    var canEdit = (0, helpers_1.checkCanInitiateChangeRequest)(metadata, entityType, change);
    var shouldShowValueEditor = (isUpdateOperation || isAddOperation) && editable && canEdit;
    var preparedValue = (0, react_1.useMemo)(function () {
        return isRolesOrTags
            ? (0, common_1.prepareRolesOrTagsValue)(metadata, newValue, change.attributeType.uri)
            : newValue;
    }, [isRolesOrTags, metadata, newValue, (_d = change.attributeType) === null || _d === void 0 ? void 0 : _d.uri]);
    var showUndoButton = (isUpdated || isLineRejected) && !isParentLineRejected;
    var showRejectButton = canEdit &&
        !isLineRejected &&
        !isUpdated &&
        !isParentLineRejected &&
        hasChangeId &&
        !isSubAttributeOfDeletedAttribute &&
        !(change.isReferenceSubAttribute && !editable);
    var showStatusChips = !isParentLineRejected;
    var isRejected = isLineRejected || isParentLineRejected;
    return (react_1.default.createElement("div", { className: ChangeItemEditor_module_css_1.default.attributeRow, "data-reltio-id": "change-item-editor" },
        react_1.default.createElement(ChangeItemLabel_1.ChangeItemLabel, { change: change, className: ChangeItemEditor_module_css_1.default.labelColumn, showLabel: !isUpdateOperation }),
        react_1.default.createElement("div", { className: ChangeItemEditor_module_css_1.default.valueColumn },
            react_1.default.createElement(RelationEntityLink_1.RelationEntityLink, { attributeOrRelationType: change.attributeType || change.relationType, attributeValue: change.attributeValue, className: ChangeItemEditor_module_css_1.default.relationEntityLink }),
            shouldShowValueEditor ? (react_1.default.createElement(DCRValueEditor_1.DCRValueEditor, { attributeType: change.attributeType, value: currentNewValue, onChange: handleChange, disabled: !editable || isRejected })) : (hasNewValue && (react_1.default.createElement("div", { className: ChangeItemEditor_module_css_1.default.value },
                react_1.default.createElement(DataTypeValue_1.DataTypeValue, { value: preparedValue, dataTypeDefinition: dataTypeDefinition }))))),
        react_1.default.createElement("div", { className: ChangeItemEditor_module_css_1.default.revertColumn }, showUndoButton && (react_1.default.createElement(SmallIconButton_1.SmallIconButton, { icon: Replay_1.default, onClick: handleRevert, className: ChangeItemEditor_module_css_1.default.revertButton, title: ui_i18n_1.default.text('Undo'), size: "S" }))),
        react_1.default.createElement("div", { className: ChangeItemEditor_module_css_1.default.operationColumn }, change.operation && react_1.default.createElement(OperationChip_1.OperationChip, { operation: change.operation, size: "small", variant: "outlined" })),
        react_1.default.createElement("div", { className: ChangeItemEditor_module_css_1.default.statusColumn }, showStatusChips && react_1.default.createElement(StatusChips_1.StatusChips, { showUpdatedChip: isUpdated, showRejectedChip: isLineRejected })),
        react_1.default.createElement("div", { className: ChangeItemEditor_module_css_1.default.rejectColumn }, showRejectButton && (react_1.default.createElement(Button_1.default, { onClick: handleReject, className: (0, classnames_1.default)(ChangeItemEditor_module_css_1.default.rejectButton, classes === null || classes === void 0 ? void 0 : classes.rejectButton), title: ui_i18n_1.default.text('Reject'), size: "medium" }, ui_i18n_1.default.text('Reject'))))));
};
exports.ChangeItemEditor = ChangeItemEditor;
