"use client";
import { useCallback, useRef, useState } from "react";
import { withinRange } from "../utils/withinRange.js";
import { useTextField } from "./useTextField.js";
const noop = ()=>{
// do nothing
};
/**
 * @internal
 * @see {@link useTextField}
 * @see {@link useNumberField} overrides for other examples.
 */ export function useNumberField(options) {
    const { min, max, step, onBlur = noop, onChange = noop, updateValue = "change", updateValueOnBlur = true, defaultValue, ...textOptions } = options;
    const [number, setNumber] = useState(defaultValue);
    const initial = useRef(number);
    const { value: _value, reset: resetTextField, fieldProps, setState: setTextFieldState, ...remaining } = useTextField({
        ...textOptions,
        isNumber: true,
        defaultValue: `${number ?? ""}`,
        onBlur (event) {
            onBlur(event);
            if (event.isPropagationStopped()) {
                return;
            }
            const input = event.currentTarget;
            input.setCustomValidity("");
            input.checkValidity();
            if (!updateValueOnBlur || // do nothing else since it's a weird value like: `"--0"` which causes
            // the value to be `""` and `numberAsValue` to be `NaN`
            input.validity.badInput) {
                return;
            }
            let value = input.valueAsNumber;
            if (input.value === "" && typeof initial.current === "number") {
                value = min ?? initial.current;
            }
            // can't have both rangeUnderflow and rangeOverflow at the same time, so
            // it's "safe" to always provide both
            value = withinRange({
                min,
                max,
                value
            });
            if (!Number.isNaN(value)) {
                setNumber(value);
                input.value = `${value}`;
            } else if (typeof initial.current === "undefined") {
                setNumber(undefined);
            }
        },
        onChange (event) {
            onChange(event);
            if (event.isPropagationStopped() || updateValue === "blur") {
                return;
            }
            const input = event.currentTarget;
            input.checkValidity();
            const value = withinRange({
                min,
                max,
                value: event.currentTarget.valueAsNumber
            });
            if (!input.validity.valid && !input.validity.rangeUnderflow && !input.validity.rangeOverflow) {
                return;
            }
            if (!Number.isNaN(value)) {
                setNumber(value);
            } else if (initial.current === undefined) {
                setNumber(undefined);
            }
        }
    });
    const reset = useCallback(()=>{
        resetTextField();
        setNumber(initial.current);
    }, [
        resetTextField
    ]);
    const setState = useCallback((nextState)=>{
        if (typeof nextState === "function") {
            setNumber((prevNumber)=>{
                let nextNumber = prevNumber;
                setTextFieldState((prevState)=>{
                    const updated = nextState({
                        ...prevState,
                        value: prevNumber
                    });
                    nextNumber = updated.value;
                    return {
                        ...updated,
                        value: `${nextNumber ?? ""}`
                    };
                });
                return nextNumber;
            });
            return;
        }
        const { value, error, errorMessage } = nextState;
        setNumber(value);
        setTextFieldState({
            value: `${value ?? ""}`,
            error,
            errorMessage
        });
    }, [
        setTextFieldState
    ]);
    return {
        ...remaining,
        reset,
        value: number,
        setState,
        fieldProps: {
            ...fieldProps,
            min,
            max,
            step,
            type: "number"
        }
    };
}

//# sourceMappingURL=useNumberField.js.map