import { useCallback, useRef, useState } from 'react';
export function useControlled(controlledValue, defaultValue, onChange) {
    const onChangeRef = useRef(onChange);
    onChangeRef.current = onChange;
    const [uncontrolledValue, setUncontrolledValue] = useState(controlledValue !== undefined ? controlledValue : defaultValue);
    const value = controlledValue !== undefined ? controlledValue : uncontrolledValue;
    const setValue = useCallback((newValue, ...args) => {
        if (newValue === value)
            return;
        onChangeRef.current?.(newValue, ...args);
        if (controlledValue === undefined) {
            setUncontrolledValue(newValue);
        }
    }, [controlledValue, value]);
    return [value, setValue];
}
//# sourceMappingURL=useControlled.js.map