import { isNumber } from '../../../utils/isNumber.js';

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

export { useDoubleTextFieldHandlers };
