"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.ChangeItemEditorRecentlyAdded = void 0;
var react_1 = __importStar(require("react"));
var ui_i18n_1 = __importDefault(require("ui-i18n"));
var mdm_sdk_1 = require("@reltio/mdm-sdk");
var Delete_1 = __importDefault(require("@mui/icons-material/Delete"));
var MdmModuleContext_1 = require("../../../../../../contexts/MdmModuleContext");
var EntitySelector_1 = require("../../../../../../EntitySelector");
var SmallIconButton_1 = require("../../../../../../SmallIconButton");
var ChangeItemLabel_1 = require("../../ChangeItemLabel");
var DCRValueEditor_1 = require("../../DCRValueEditor");
var AddSameAttributeButton_1 = require("../../AddSameAttributeButton/AddSameAttributeButton");
var DCRImageEditor_1 = require("../../DCRImageEditor");
var useNewAttributesEditor_1 = require("../../../hooks/useNewAttributesEditor");
var EditorStatusChip_1 = require("../../EditorStatusChip");
var editors_module_css_1 = __importDefault(require("../editors.module.css"));
var ChangeItemEditorRecentlyAdded = function (_a) {
    var _b, _c, _d, _e;
    var change = _a.change, _f = _a.editable, editable = _f === void 0 ? true : _f;
    var metadata = (0, MdmModuleContext_1.useMdmMetadata)();
    var globalSearchRequestOptions = (0, MdmModuleContext_1.useMdmGlobalSearchRequestOptions)(['ovOnly']);
    var showMasking = (0, MdmModuleContext_1.useMdmShowMasking)();
    var _g = (0, useNewAttributesEditor_1.useNewAttributesEditor)({ change: change }), lineData = _g.lineData, handleDelete = _g.handleDelete, handleChange = _g.handleChange, handleEntityChange = _g.handleEntityChange, handleImageUpload = _g.handleImageUpload, selectedEntity = _g.selectedEntity, isRejected = _g.isRejected, hasAddedParent = _g.hasAddedParent;
    var attributeType = change.attributeType;
    var currentValue = (_b = lineData === null || lineData === void 0 ? void 0 : lineData.value) !== null && _b !== void 0 ? _b : change.newValue;
    var hasValue = !!(0, mdm_sdk_1.getAttributeValue)(currentValue);
    var isReferenceType = (0, mdm_sdk_1.isReference)(attributeType);
    var isImageType = (0, mdm_sdk_1.isImage)(attributeType);
    var isComplex = (0, mdm_sdk_1.isComplexAttribute)(attributeType);
    var isMasked = showMasking && (0, mdm_sdk_1.checkHasMasking)(metadata, attributeType);
    var entityTypesUris = (0, react_1.useMemo)(function () {
        if (!isReferenceType || !attributeType)
            return [];
        return [(0, mdm_sdk_1.getReferencedEntityTypeUriFromAttrType)(attributeType)];
    }, [isReferenceType, attributeType]);
    var renderEditor = function () {
        if (isReferenceType) {
            return (react_1.default.createElement(EntitySelector_1.EntitySelector, { entity: selectedEntity, entityTypesUris: entityTypesUris, mode: mdm_sdk_1.Mode.Suggesting, onChange: handleEntityChange, metadata: metadata, globalSearchRequestOptions: globalSearchRequestOptions, isMasked: isMasked }));
        }
        if (isImageType) {
            return react_1.default.createElement(DCRImageEditor_1.DCRImageEditor, { change: change, onUpload: handleImageUpload, disabled: isRejected });
        }
        if (!editable)
            return null;
        if (!isComplex) {
            return (react_1.default.createElement(DCRValueEditor_1.DCRValueEditor, { attributeType: attributeType, value: currentValue, onChange: handleChange, disabled: isRejected }));
        }
        return null;
    };
    var showAddButton = editable && ((_c = change.displayOptions) === null || _c === void 0 ? void 0 : _c.showAddButton) && !isRejected;
    var showDeleteButton = ((_d = change.displayOptions) === null || _d === void 0 ? void 0 : _d.hasSameAttributeSiblings) || !((attributeType === null || attributeType === void 0 ? void 0 : attributeType.required) && hasAddedParent);
    return (react_1.default.createElement("div", { className: editors_module_css_1.default.attributeRow, "data-reltio-id": "change-item-editor-recently-added" },
        react_1.default.createElement(ChangeItemLabel_1.ChangeItemLabel, { change: change, className: editors_module_css_1.default.labelColumn, showLabel: (_e = change.displayOptions) === null || _e === void 0 ? void 0 : _e.showLabel }),
        react_1.default.createElement("div", { className: editors_module_css_1.default.valueColumn }, renderEditor()),
        react_1.default.createElement("div", { className: editors_module_css_1.default.addSameAttributeColumn }, showAddButton && react_1.default.createElement(AddSameAttributeButton_1.AddSameAttributeButton, { change: change })),
        react_1.default.createElement("div", { className: editors_module_css_1.default.revertColumn }, showDeleteButton && (react_1.default.createElement(SmallIconButton_1.SmallIconButton, { icon: Delete_1.default, onClick: handleDelete, title: ui_i18n_1.default.text('Delete'), size: "S" }))),
        react_1.default.createElement("div", { className: editors_module_css_1.default.operationColumn }),
        react_1.default.createElement("div", { className: editors_module_css_1.default.statusColumn },
            react_1.default.createElement(EditorStatusChip_1.EditorStatusChip, { status: !isComplex && hasValue ? EditorStatusChip_1.EditorStatus.RECENTLY_ADDED : undefined })),
        react_1.default.createElement("div", { className: editors_module_css_1.default.rejectColumn })));
};
exports.ChangeItemEditorRecentlyAdded = ChangeItemEditorRecentlyAdded;
