import { _ as _define_property } from "@swc/helpers/_/_define_property";
import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
import { _ as _object_without_properties } from "@swc/helpers/_/_object_without_properties";
import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
import React, { useEffect, useRef, useState } from "react";
import { Minus, Plus } from "@nutui/icons-react";
import classNames from "classnames";
import { usePropsValue } from "../../hooks/use-props-value";
import { ComponentDefaults } from "../../utils/typings";
var defaultProps = _object_spread_props(_object_spread({}, ComponentDefaults), {
    disabled: false,
    readOnly: false,
    allowEmpty: false,
    min: 1,
    max: 9999,
    step: 1,
    digits: 0,
    async: false,
    select: true
});
var classPrefix = "nut-inputnumber";
export var InputNumber = function(props) {
    var _ref = _object_spread({}, defaultProps, props), children = _ref.children, disabled = _ref.disabled, min = _ref.min, max = _ref.max, readOnly = _ref.readOnly, value = _ref.value, defaultValue = _ref.defaultValue, allowEmpty = _ref.allowEmpty, digits = _ref.digits, step = _ref.step, async = _ref.async, select = _ref.select, className = _ref.className, style = _ref.style, formatter = _ref.formatter, onPlus = _ref.onPlus, onMinus = _ref.onMinus, onOverlimit = _ref.onOverlimit, onBlur = _ref.onBlur, onFocus = _ref.onFocus, onChange = _ref.onChange, restProps = _object_without_properties(_ref, [
        "children",
        "disabled",
        "min",
        "max",
        "readOnly",
        "value",
        "defaultValue",
        "allowEmpty",
        "digits",
        "step",
        "async",
        "select",
        "className",
        "style",
        "formatter",
        "onPlus",
        "onMinus",
        "onOverlimit",
        "onBlur",
        "onFocus",
        "onChange"
    ]);
    var classes = classNames(classPrefix, className, _define_property({}, "".concat(classPrefix, "-disabled"), disabled));
    var _useState = _sliced_to_array(useState(false), 2), focused = _useState[0], setFocused = _useState[1];
    var inputRef = useRef(null);
    useEffect(function() {
        if (select && focused) {
            var _inputRef_current_select, _inputRef_current;
            (_inputRef_current = inputRef.current) === null || _inputRef_current === void 0 ? void 0 : (_inputRef_current_select = _inputRef_current.select) === null || _inputRef_current_select === void 0 ? void 0 : _inputRef_current_select.call(_inputRef_current);
        }
    }, [
        select,
        focused
    ]);
    var _usePropsValue = _sliced_to_array(usePropsValue({
        value: typeof value === 'string' ? parseFloat(value) : value,
        defaultValue: typeof defaultValue === 'string' ? parseFloat(defaultValue) : defaultValue,
        finalValue: 0,
        onChange: function(value) {}
    }), 2), shadowValue = _usePropsValue[0], setShadowValue = _usePropsValue[1];
    var bound = function(value, min, max) {
        var res = value;
        if (min !== undefined) {
            res = Math.max(Number(min), res);
        }
        if (max !== undefined) {
            res = Math.min(Number(max), res);
        }
        return res;
    };
    var format = function(value) {
        if (value === null) return '';
        // 如果超过 min 或 max, 需要纠正
        if (typeof value === 'string') value = parseFloat(value);
        var fixedValue = bound(value, Number(min), Number(max));
        if (formatter) {
            return formatter(fixedValue);
        }
        if (digits) {
            return fixedValue.toFixed(digits).toString();
        }
        return fixedValue.toString();
    };
    var _useState1 = _sliced_to_array(useState(format(shadowValue)), 2), inputValue = _useState1[0], setInputValue = _useState1[1];
    useEffect(function() {
        if (!focused && !async) {
            setShadowValue(bound(Number(shadowValue), Number(min), Number(max)));
            setInputValue(format(shadowValue));
        }
    }, [
        focused,
        shadowValue
    ]);
    useEffect(function() {
        if (async) {
            setShadowValue(bound(Number(value), Number(min), Number(max)));
            setInputValue(format(value));
        }
    }, [
        value
    ]);
    var calcNextValue = function(current, step, symbol) {
        var dig = digits + 1;
        return (parseFloat(current || '0') * dig + parseFloat(step) * dig * symbol) / dig;
    };
    var update = function(negative, e) {
        if (step !== undefined) {
            var shouldOverBoundary = calcNextValue(shadowValue, step, negative ? -1 : 1);
            var nextValue = bound(shouldOverBoundary, Number(min), Number(max));
            setShadowValue(nextValue);
            if (negative ? shouldOverBoundary < Number(min) : shouldOverBoundary > Number(max)) {
                onOverlimit === null || onOverlimit === void 0 ? void 0 : onOverlimit(e);
            } else {
                onChange === null || onChange === void 0 ? void 0 : onChange(nextValue, e);
            }
        }
    };
    var handleReduce = function(e) {
        if (disabled) return;
        onMinus === null || onMinus === void 0 ? void 0 : onMinus(e);
        update(true, e);
    };
    var handlePlus = function(e) {
        if (disabled) return;
        onPlus === null || onPlus === void 0 ? void 0 : onPlus(e);
        update(false, e);
    };
    var parseValue = function(text) {
        if (text === '') return null;
        if (text === '-') return null;
        return text;
    };
    var clampValue = function(valueStr) {
        if (valueStr === null) return defaultValue;
        var val = Number(parseFloat(valueStr || '0').toFixed(digits));
        return Math.max(Number(min), Math.min(Number(max), val));
    };
    var handleValueChange = function(valueStr, e) {
        var val = clampValue(valueStr);
        // input暂不触发onOverlimit
        // if (val !== Number(e.target.value)) {
        //   onOverlimit?.(e)
        // }
        if (val !== Number(shadowValue)) {
            onChange === null || onChange === void 0 ? void 0 : onChange(val, e);
        }
    };
    var handleInputChange = function(e) {
        // 设置 input 值， 在 blur 时格式化
        setInputValue(e.target.value);
        var valueStr = parseValue(e.target.value);
        if (valueStr === null) {
            if (allowEmpty) {
                setShadowValue(null);
            } else {
                setShadowValue(defaultValue);
            }
        } else {
            setShadowValue(clampValue(valueStr));
        }
        !async && handleValueChange(valueStr, e);
    };
    var handleFocus = function(e) {
        setFocused(true);
        setInputValue(shadowValue !== undefined && shadowValue !== null ? bound(Number(shadowValue), Number(min), Number(max)).toString() : '');
        onFocus === null || onFocus === void 0 ? void 0 : onFocus(e);
    };
    var handleBlur = function(e) {
        setFocused(false);
        onBlur === null || onBlur === void 0 ? void 0 : onBlur(e);
        var valueStr = parseValue(e.target.value);
        if (valueStr === null) {
            if (allowEmpty) {
                setShadowValue(null);
            } else {
                setShadowValue(defaultValue);
            }
        } else {
            setShadowValue(clampValue(valueStr));
        }
        async && handleValueChange(valueStr, e);
    };
    return /*#__PURE__*/ React.createElement("div", _object_spread({
        className: classes,
        style: style
    }, restProps), /*#__PURE__*/ React.createElement("div", {
        className: "".concat(classPrefix, "-minus"),
        onClick: handleReduce
    }, /*#__PURE__*/ React.createElement(Minus, {
        className: classNames("".concat(classPrefix, "-icon ").concat(classPrefix, "-icon-minus"), _define_property({}, "".concat(classPrefix, "-icon-disabled"), shadowValue === min || disabled))
    })), /*#__PURE__*/ React.createElement("input", {
        className: classNames("".concat(classPrefix, "-input"), _define_property({}, "".concat(classPrefix, "-input-disabled"), disabled)),
        ref: inputRef,
        inputMode: "decimal",
        disabled: disabled,
        readOnly: readOnly,
        value: inputValue,
        onInput: handleInputChange,
        onBlur: handleBlur,
        onFocus: handleFocus
    }), /*#__PURE__*/ React.createElement("div", {
        className: "".concat(classPrefix, "-add"),
        onClick: handlePlus
    }, /*#__PURE__*/ React.createElement(Plus, {
        className: classNames("".concat(classPrefix, "-icon ").concat(classPrefix, "-icon-plus"), _define_property({}, "".concat(classPrefix, "-icon-disabled"), shadowValue === max || disabled))
    })));
};
InputNumber.displayName = 'NutInputNumber';
