"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.EditModeNumberValueEditor = exports.DisplayModeNumberValueEditor = void 0;
const tslib_1 = require("tslib");
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = tslib_1.__importStar(require("react"));
const UITextField_1 = require("../BuildlingBlocks/UITextField");
const PropertyEditorNumberList_1 = require("../PropertyEditing/PropertyEditorNumberList");
var DisplayModeRender_1 = require("./DisplayModeRender");
Object.defineProperty(exports, "DisplayModeNumberValueEditor", { enumerable: true, get: function () { return DisplayModeRender_1.DisplayModeValueEditor; } });
exports.EditModeNumberValueEditor = react_1.default.forwardRef((props, ref) => {
    if (props.forData.list) {
        return (0, jsx_runtime_1.jsx)(NumberListEditor, { ...props, ref: ref });
    }
    return (0, jsx_runtime_1.jsx)(NumberEditor, { ...props, ref: ref });
});
const NumberEditor = react_1.default.forwardRef(({ value, forData, onUpdate }, ref) => {
    let datadef = forData;
    const [values, setValues] = (0, react_1.useState)(() => {
        return datadef.convert(value);
    });
    const [error, setError] = (0, react_1.useState)();
    (0, react_1.useEffect)(() => {
        setValues(forData.convert(value));
    }, [value, setValues, forData]);
    const onChange = (event) => {
        const validNumber = new RegExp(/^\d*\.?\d*$/);
        if (validNumber.test(event.target.value)) {
            if (event.target.value !== "") {
                let newValue = Number(event.target.value);
                if (!isNaN(newValue) && event.target.value.endsWith(".") && datadef.allowDecimals) {
                    setValues(event.target.value);
                    return;
                }
                if (datadef.upperBounds !== undefined) {
                    if (newValue < datadef.upperBounds) {
                        setValues(newValue);
                    }
                    else {
                        setError(`should be less than ${datadef.upperBounds}`);
                    }
                }
                else {
                    setValues(newValue);
                }
            }
            else {
                setValues(undefined);
            }
        }
        else {
            if (event.target.value !== "") {
                setError("only numbers are allowed");
            }
        }
    };
    return ((0, jsx_runtime_1.jsx)(UITextField_1.UITextField, { variant: "outlined", size: "small", value: values || "", onChange: onChange, placeholder: datadef.allowDecimals ? "0.00" : "00", type: "text", inputRef: ref, "data-testid": `ValueEditor-Edit-number`, onBlur: () => {
            if (datadef.lowerBounds === undefined ||
                (datadef.lowerBounds !== undefined && values < datadef.lowerBounds)) {
                onUpdate(values && values !== "" ? Number(values) : values);
            }
        }, inputProps: { style: { textAlign: "right" } }, error: !!error, helperText: error }));
});
const NumberListEditor = react_1.default.forwardRef(
// eslint-disable-next-line
({ value, onUpdate }, ref) => {
    const field = {
        value: Array.isArray(value) && typeof value !== "string" ? value : value ? [value] : [],
    };
    const onValueUpdate = () => {
        onUpdate(field.value);
    };
    return (0, jsx_runtime_1.jsx)(PropertyEditorNumberList_1.PropertyEditorNumberList, { editObject: field, propertyName: "value", updateCallback: onValueUpdate });
});
//# sourceMappingURL=Number.js.map