"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "Input", {
    enumerable: true,
    get: function() {
        return Input;
    }
});
const _react = /*#__PURE__*/ _interop_require_wildcard(require("react"));
const _NumberFormat = require("../../../NumberFormat");
const _utils = require("../../../NumberInput/utils");
const _TextFieldSlidertokens = require("../../TextFieldSlider.tokens");
const _utils1 = require("../../utils");
const _Inputstyles = require("./Input.styles");
function _getRequireWildcardCache(nodeInterop) {
    if (typeof WeakMap !== "function") return null;
    var cacheBabelInterop = new WeakMap();
    var cacheNodeInterop = new WeakMap();
    return (_getRequireWildcardCache = function(nodeInterop) {
        return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
    })(nodeInterop);
}
function _interop_require_wildcard(obj, nodeInterop) {
    if (!nodeInterop && obj && obj.__esModule) {
        return obj;
    }
    if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
        return {
            default: obj
        };
    }
    var cache = _getRequireWildcardCache(nodeInterop);
    if (cache && cache.has(obj)) {
        return cache.get(obj);
    }
    var newObj = {
        __proto__: null
    };
    var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
    for(var key in obj){
        if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
            var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
            if (desc && (desc.get || desc.set)) {
                Object.defineProperty(newObj, key, desc);
            } else {
                newObj[key] = obj[key];
            }
        }
    }
    newObj.default = obj;
    if (cache) {
        cache.set(obj, newObj);
    }
    return newObj;
}
const Input = /*#__PURE__*/ (0, _react.forwardRef)(({ id, size, value, min, max, decimalScale, thousandsGroupStyle, thousandSeparator = ' ', decimalSeparator, fixedDecimalScale, disabled, readOnly, // callbacks
onChange, onChangeTextField, setInnerValue, ...rest }, ref)=>{
    const [errorClass, setErrorClass] = (0, _react.useState)(undefined);
    const commonFormatterArgs = (0, _react.useMemo)(()=>(0, _utils1.createFormatterArgs)({
            decimalScale,
            thousandsGroupStyle,
            thousandSeparator,
            decimalSeparator,
            fixedDecimalScale
        }), [
        decimalScale,
        thousandsGroupStyle,
        thousandSeparator,
        decimalSeparator,
        fixedDecimalScale
    ]);
    const displayValue = (0, _react.useMemo)(()=>(0, _NumberFormat.numberFormatter)(String(value) || '', commonFormatterArgs), [
        value,
        commonFormatterArgs
    ]);
    const [lastValidValue, setLastValidValue] = (0, _react.useState)(value);
    const setValues = (event, newValue)=>{
        setInnerValue(newValue);
        const formatted = (0, _NumberFormat.numberFormatter)(String(newValue) || '', commonFormatterArgs);
        if (onChange) {
            onChange(event || undefined, {
                raw: newValue,
                formatted
            });
        }
        if (onChangeTextField) {
            onChangeTextField({
                raw: newValue,
                formatted
            });
        }
    };
    const validateValue = (event, newValue)=>{
        if (!newValue) {
            setValues(event, '');
            setLastValidValue('');
            return;
        }
        const cleanValue = Number(newValue);
        if (max !== undefined && cleanValue > max) {
            setErrorClass(_TextFieldSlidertokens.classes.errorAnimation);
            setValues(event, newValue);
            setLastValidValue(max);
            return;
        }
        if (min !== undefined && cleanValue < min) {
            setErrorClass(_TextFieldSlidertokens.classes.errorAnimation);
            setValues(event, newValue);
            setLastValidValue(min);
            return;
        }
        setValues(event, newValue);
        setLastValidValue(newValue);
    };
    const handleChange = (event)=>{
        if (disabled || readOnly) {
            return;
        }
        const { value: rawValue } = event.target;
        const withoutExtraSymbols = rawValue.replace(_utils.excludingNumberSchema, '');
        const formattedValue = (0, _NumberFormat.numberFormatter)(withoutExtraSymbols, (0, _utils1.createNormalizedFormatterArgs)({
            decimalScale,
            thousandsGroupStyle,
            thousandSeparator,
            decimalSeparator,
            fixedDecimalScale
        }));
        validateValue(event, formattedValue);
    };
    const handleEndErrorAnimation = ()=>{
        setErrorClass(undefined);
        setValues(null, lastValidValue);
    };
    return /*#__PURE__*/ _react.default.createElement(_Inputstyles.StyledInput, {
        id: id,
        ref: ref,
        size: size,
        value: displayValue,
        disabled: disabled,
        readOnly: readOnly,
        className: errorClass,
        onChange: handleChange,
        onAnimationEnd: handleEndErrorAnimation,
        ...rest
    });
});
