"use strict";
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);
};
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.InlineSimpleAttributeEditor = void 0;
var react_1 = __importStar(require("react"));
var Popover_1 = __importDefault(require("@mui/material/Popover"));
var mdm_sdk_1 = require("@reltio/mdm-sdk");
var classnames_1 = __importDefault(require("classnames"));
var ramda_1 = require("ramda");
var DataTypeValueEditor_1 = require("../DataTypeValueEditor");
var attributesView_1 = require("../helpers/attributesView");
var styles_1 = require("./styles");
var DATA_TYPES_WITH_MULTILINE_EDITOR = [mdm_sdk_1.DataTypes.TYPE_TEXT, mdm_sdk_1.DataTypes.TYPE_BLOB];
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 = (0, styles_1.useStyles)({
        color: color,
        containerWidth: containerRef.current ? containerRef.current.getBoundingClientRect().width : 0
    });
    var dataTypeDefinition = (0, mdm_sdk_1.getAttrDataTypeDefinition)(attributeType);
    var initEditorValue = (0, attributesView_1.attributeValueToEditorValue)(attributeValue, dataTypeDefinition);
    var _c = (0, react_1.useState)(initEditorValue), editorValue = _c[0], changeEditorValue = _c[1];
    var isTemporaryWithDefaultValue = !(0, ramda_1.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 = !(0, ramda_1.isNil)(editorValue) && (0, attributesView_1.editorValueToAttributeValue)(attributeType, editorValue);
        if ((!(0, ramda_1.isNil)(editorValue) && !(0, ramda_1.equals)(editorValue, initEditorValue)) || isTemporaryWithDefaultValue) {
            var editedAttributeValue = (0, mdm_sdk_1.isLookupAttrType)(attributeType)
                ? {
                    value: editedValue.value,
                    lookupCode: editedValue.lookupCode
                }
                : {
                    value: editorValue === '' && (0, mdm_sdk_1.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_1.default.createElement(Popover_1.default, { "data-reltio-id": "inline-simple-attribute-editor", open: !!containerRef, anchorEl: containerRef.current, anchorOrigin: { vertical: 'top', horizontal: 'left' }, transformOrigin: { vertical: 'top', horizontal: 'left' }, classes: {
            paper: (0, classnames_1.default)(styles.paper, (_b = {},
                _b[styles.booleanEditor] = (0, mdm_sdk_1.isBooleanType)(dataTypeDefinition.type),
                _b[styles.timestampEditor] = (0, mdm_sdk_1.isTimestampBasedType)(dataTypeDefinition.type),
                _b)),
            root: styles.popoverRoot
        }, onKeyUp: onKeyUp, onBackdropClick: applyChangesAndClose },
        react_1.default.createElement(DataTypeValueEditor_1.DataTypeValueEditor, { dataTypeDefinition: dataTypeDefinition, fieldName: (0, mdm_sdk_1.attributeUriToSearchUri)(attributeType.uri), onChange: changeEditorValue, TextFieldProps: TextFieldProps, value: editorValue })));
};
exports.InlineSimpleAttributeEditor = InlineSimpleAttributeEditor;
