"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "useRangeHandlers", {
    enumerable: true,
    get: function() {
        return useRangeHandlers;
    }
});
const clamp = (value, min, max)=>Math.max(min, Math.min(value, max));
const useRangeHandlers = ({ min, max, value, step, multipleStepSize, isControlled, onSetValue, onEmit, onCommit })=>{
    const handleChange = (e)=>{
        const newValue = Number(e.target.value);
        if (Number.isNaN(newValue)) {
            return;
        }
        if (!isControlled) {
            onSetValue(newValue);
        }
        onEmit(newValue);
    };
    const handleChangeCommitted = (e)=>{
        const newValue = Number(e.target.value);
        if (Number.isNaN(newValue)) {
            return;
        }
        if (onCommit) {
            onCommit(newValue);
        }
    };
    const handleKeyDown = (e)=>{
        if (e.key !== 'PageUp' && e.key !== 'PageDown') {
            return;
        }
        e.preventDefault();
        const pageStep = Math.round((max - min) * multipleStepSize / 100);
        const raw = clamp(e.key === 'PageUp' ? value + pageStep : value - pageStep, min, max);
        const newValue = step ? clamp(min + Math.round((raw - min) / step) * step, min, max) : raw;
        if (Number.isNaN(newValue)) {
            return;
        }
        if (!isControlled) {
            onSetValue(newValue);
        }
        onEmit(newValue);
        if (onCommit) {
            onCommit(newValue);
        }
    };
    return {
        handleChange,
        handleChangeCommitted,
        handleKeyDown
    };
};
