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