var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
import React, { useState } from 'react';
import Popover from '@mui/material/Popover';
import { attributeUriToSearchUri, DataTypes, getAttrDataTypeDefinition, isBooleanType, isLookupAttrType, isNumericType, isTimestampBasedType, shouldDisableTimestampLocalizationForAttribute } from '@reltio/mdm-sdk';
import classnames from 'classnames';
import { equals, isNil } from 'ramda';
import { DataTypeValueEditor } from '../DataTypeValueEditor';
import { attributeValueToEditorValue, editorValueToAttributeValue } from '../helpers/attributesView';
import { useMdmDisableTimestampLocalization } from '../contexts/MdmModuleContext';
import { useStyles } from './styles';
var DATA_TYPES_WITH_MULTILINE_EDITOR = [DataTypes.TYPE_TEXT, DataTypes.TYPE_BLOB];
export var InlineSimpleAttributeEditor = function (_a) {
    var _b;
    var attributeType = _a.attributeType, attributeValue = _a.attributeValue, color = _a.color, containerRef = _a.containerRef, onClose = _a.onClose, onEdit = _a.onEdit, isTemporary = _a.isTemporary;
    var styles = useStyles({
        color: color,
        containerWidth: containerRef.current ? containerRef.current.getBoundingClientRect().width : 0
    });
    var dataTypeDefinition = getAttrDataTypeDefinition(attributeType);
    var disableTimestampLocalizationFlag = useMdmDisableTimestampLocalization();
    var disableTimestampLocalization = shouldDisableTimestampLocalizationForAttribute(attributeType, {
        disableTimestampLocalization: disableTimestampLocalizationFlag
    });
    var initEditorValue = attributeValueToEditorValue(attributeValue, dataTypeDefinition, {
        disableTimestampLocalization: disableTimestampLocalization
    });
    var _c = useState(initEditorValue), editorValue = _c[0], changeEditorValue = _c[1];
    var isTemporaryWithDefaultValue = !isNil(attributeType.defaultValue) && isTemporary;
    var TextFieldProps = {
        className: styles.textField,
        InputProps: {
            disableUnderline: true,
            classes: {
                root: styles.input,
                multiline: styles.inputMultiline
            }
        },
        fullWidth: true,
        autoFocus: true
    };
    var applyChangesAndClose = function () {
        var editedValue = !isNil(editorValue) &&
            editorValueToAttributeValue(attributeType, editorValue, { disableTimestampLocalization: disableTimestampLocalization });
        if ((!isNil(editorValue) && !equals(editorValue, initEditorValue)) || isTemporaryWithDefaultValue) {
            var editedAttributeValue = isLookupAttrType(attributeType)
                ? {
                    value: editedValue.value,
                    lookupCode: editedValue.lookupCode
                }
                : {
                    value: (editorValue === '' && isNumericType(dataTypeDefinition.type)
                        ? null
                        : editedValue)
                };
            onEdit(__assign(__assign({}, attributeValue), editedAttributeValue));
        }
        onClose();
    };
    var onKeyUp = function (event) {
        if (event.key === 'Enter' && !DATA_TYPES_WITH_MULTILINE_EDITOR.includes(dataTypeDefinition.type)) {
            applyChangesAndClose();
        }
        else if (event.key === 'Escape')
            onClose();
    };
    return (React.createElement(Popover, { "data-reltio-id": "inline-simple-attribute-editor", open: !!containerRef, anchorEl: containerRef.current, anchorOrigin: { vertical: 'top', horizontal: 'left' }, transformOrigin: { vertical: 'top', horizontal: 'left' }, classes: {
            paper: classnames(styles.paper, (_b = {},
                _b[styles.booleanEditor] = isBooleanType(dataTypeDefinition.type),
                _b[styles.timestampEditor] = isTimestampBasedType(dataTypeDefinition.type),
                _b)),
            root: styles.popoverRoot
        }, onKeyUp: onKeyUp, onBackdropClick: applyChangesAndClose },
        React.createElement(DataTypeValueEditor, { dataTypeDefinition: dataTypeDefinition, fieldName: attributeUriToSearchUri(attributeType.uri), onChange: changeEditorValue, TextFieldProps: TextFieldProps, value: editorValue })));
};
