"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "composeNumberFormat", {
    enumerable: true,
    get: function() {
        return composeNumberFormat;
    }
});
const _react = /*#__PURE__*/ _interop_require_wildcard(require("react"));
const _reactnumberformat = require("react-number-format");
const _hooks = require("../../hooks");
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 composeNumberFormat = (InputComponent)=>{
    return /*#__PURE__*/ (0, _react.forwardRef)(({ thousandSeparator = ' ', decimalScale = 3, decimalSeparator = '.', disabled, value: outerValue, onChange, defaultValue, onPaste, ...rest }, outerRef)=>{
        const inputRef = (0, _react.useRef)(null);
        const inputForkRef = (0, _hooks.useForkRef)(inputRef, outerRef);
        const [innerValue, setInnerValue] = (0, _react.useState)(defaultValue || '');
        const value = (0, _reactnumberformat.numericFormatter)(String(outerValue ?? innerValue), {
            thousandSeparator,
            decimalScale,
            decimalSeparator
        });
        const handleChangeValue = (values, sourceInfo)=>{
            const changeEvent = sourceInfo.event;
            setInnerValue(values.value);
            if (onChange) {
                onChange(changeEvent, values);
            }
        };
        const handlePaste = (event)=>{
            if (disabled) {
                return;
            }
            if (decimalSeparator !== '.' && inputRef.current) {
                if (!Number.isNaN(Number(event.clipboardData.getData('text/plain')))) {
                    event.preventDefault();
                    const pastedValue = event.clipboardData.getData('text/plain').replace('.', decimalSeparator);
                    const input = event.target;
                    const startPos = input.selectionStart || 0;
                    const newCursorPos = startPos + pastedValue.length;
                    setInnerValue(pastedValue);
                    setTimeout(()=>{
                        inputRef.current?.setSelectionRange(newCursorPos, newCursorPos);
                    }, 10);
                }
            }
            if (onPaste) {
                onPaste(event);
            }
        };
        return /*#__PURE__*/ _react.default.createElement(_reactnumberformat.NumericFormat, {
            value: value,
            disabled: disabled,
            thousandSeparator: thousandSeparator,
            decimalScale: decimalScale,
            decimalSeparator: decimalSeparator,
            customInput: InputComponent,
            getInputRef: inputForkRef,
            _textEllipsisDisable: true,
            onValueChange: handleChangeValue,
            onPaste: handlePaste,
            ...rest
        });
    });
};
