import { isNumber } from "../../../utils";
/**
 * Инкапсулирует все обработчики текстовых полей двойного слайдера:
 * change / blur для каждого поля и общий keydown.
 */ export const useDoubleTextFieldHandlers = ({ isControlled, firstValue, secondValue, setDragValue, sortValues, emitChange, onChangeTextField, onBlurTextField, onKeyDownTextField, name, setFirstInputFocused, setFirstInputHovered, setSecondInputFocused, setSecondInputHovered })=>{
    const onFirstTextfieldChange = (event)=>{
        if (!isNumber(event.target.value)) {
            return;
        }
        const handleValue = Number(event.target.value);
        if (!isControlled) {
            setDragValue((prev)=>[
                    handleValue,
                    prev[1]
                ]);
        }
        if (onChangeTextField) {
            onChangeTextField([
                handleValue,
                secondValue
            ], event);
        }
        emitChange([
            handleValue,
            secondValue
        ]);
    };
    const onFirstTextfieldBlur = (event)=>{
        setFirstInputFocused(false);
        setFirstInputHovered(false);
        if (!isNumber(event.target.value)) {
            return;
        }
        const handleValue = Number(event.target.value);
        const sortedValues = sortValues([
            handleValue,
            secondValue
        ]);
        if (!isControlled) {
            setDragValue(sortedValues);
            emitChange(sortedValues);
        }
        if (onBlurTextField) {
            onBlurTextField(sortedValues, event);
        }
    };
    const onSecondTextfieldChange = (event)=>{
        if (!isNumber(event.target.value)) return;
        const handleValue = Number(event.target.value);
        if (!isControlled) {
            setDragValue((prev)=>[
                    prev[0],
                    handleValue
                ]);
        }
        if (onChangeTextField) {
            onChangeTextField([
                firstValue,
                handleValue
            ], event);
        }
        emitChange([
            firstValue,
            handleValue
        ]);
    };
    const onSecondTextfieldBlur = (event)=>{
        setSecondInputFocused(false);
        setSecondInputHovered(false);
        if (!isNumber(event.target.value)) {
            return;
        }
        const handleValue = Number(event.target.value);
        const sortedValues = sortValues([
            firstValue,
            handleValue
        ]);
        if (!isControlled) {
            setDragValue(sortedValues);
            emitChange(sortedValues);
        }
        if (onBlurTextField) {
            onBlurTextField(sortedValues, event);
        }
    };
    const onTextfieldKeyDown = (event)=>{
        if (onKeyDownTextField) {
            onKeyDownTextField([
                firstValue,
                secondValue
            ], event);
        }
        if (!isControlled && name && event.key === 'Enter') {
            emitChange(sortValues([
                firstValue,
                secondValue
            ]));
        }
    };
    return {
        onFirstTextfieldChange,
        onFirstTextfieldBlur,
        onSecondTextfieldChange,
        onSecondTextfieldBlur,
        onTextfieldKeyDown
    };
};
