"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _classnames = _interopRequireDefault(require("classnames"));
var _react = require("react");
var _utils = require("../utils");
var _Input = _interopRequireDefault(require("../Input"));
var _Button = _interopRequireDefault(require("../Button"));
var _constants = require("../../constants");
var _jsxRuntime = require("react/jsx-runtime");
// Copyright 1999-2023. Plesk International GmbH. All rights reserved.

const fitToMinMax = function (value) {
  let min = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : Number.MIN_SAFE_INTEGER || -(Math.pow(2, 53) - 1);
  let max = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : Number.MAX_SAFE_INTEGER || Math.pow(2, 53) - 1;
  if (max !== undefined && value > max) {
    return max;
  }
  if (min !== undefined && value < min) {
    return min;
  }
  return value;
};
const isLikeNumber = value => {
  if (String(value).trim() === '') {
    return false;
  }
  const number = parseFloat(String(value));
  return typeof number === 'number' && !isNaN(number);
};
const toNumber = value => {
  if (typeof value === 'string') {
    const number = parseFloat(value);
    if (!isLikeNumber(number)) {
      return 0;
    }
    return number;
  }
  return value;
};
const isMinDisabled = (value, min) => min !== undefined && value !== '' && value <= min;
const isMaxDisabled = (value, max) => max !== undefined && value !== '' && value >= max;

/**
 * `InputNumber` component is used for entering integer numbers.
 * @since 2.2.0
 */
const InputNumber = _ref => {
  let {
    baseClassName = `${_constants.CLS_PREFIX}input-number`,
    className,
    defaultValue,
    value: propValue,
    disabled,
    onChange,
    min,
    max,
    step = 1,
    decrementIcon = 'minus',
    incrementIcon = 'plus',
    ...props
  } = _ref;
  const inputRef = (0, _react.useRef)(null);
  const isControlled = propValue !== undefined;
  const safeDefaultValue = typeof defaultValue === 'number' ? defaultValue : '';
  const safePropValue = typeof propValue === 'number' ? propValue : '';
  const [value, setValue] = (0, _react.useState)(isControlled ? safePropValue : safeDefaultValue);
  const firedRef = (0, _react.useRef)(Number(isControlled ? safePropValue : safeDefaultValue));
  (0, _react.useLayoutEffect)(() => {
    if (isControlled) {
      setValue(safePropValue);
    }
  }, [isControlled, safePropValue]);
  const fireChange = nextValue => {
    if (firedRef.current !== nextValue) {
      firedRef.current = nextValue;
      (0, _utils.safeInvoke)(onChange, nextValue);
    }
  };
  const createIncDec = diff => () => {
    const nextValue = fitToMinMax(toNumber(value) + diff, min, max);
    if (!isControlled || nextValue === safePropValue) {
      setValue(nextValue);
    }
    fireChange(nextValue);
  };
  const handleInc = createIncDec(+step);
  const handleDec = createIncDec(-step);
  const handleChange = e => {
    const currValue = e.target.value;
    let nextValue = currValue;
    if (String(currValue).trim() === '') {
      nextValue = '';
    }
    setValue(nextValue);
  };
  const handleKeyDown = e => {
    switch (e.key) {
      case 'ArrowUp':
        handleInc();
        break;
      case 'ArrowDown':
        handleDec();
        break;
      case 'Enter':
        handleBlur();
        break;
    }
  };
  const handleBlur = () => {
    let nextValue;
    if (isLikeNumber(value)) {
      nextValue = fitToMinMax(toNumber(value), min, max);
      fireChange(nextValue);
    } else {
      nextValue = '';
    }
    if (isControlled) {
      setValue(safePropValue);
    } else {
      setValue(nextValue);
    }
  };
  const handleMouseDown = e => {
    e.preventDefault();
    if (e.currentTarget.hasAttribute('disabled')) {
      return;
    }
    if (inputRef.current && document.activeElement !== inputRef.current) {
      inputRef.current.focus();
    }
  };
  const minDisabled = disabled || isMinDisabled(value, min);
  const maxDisabled = disabled || isMaxDisabled(value, max);
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_Input.default, {
    textAlign: "center",
    size: "sm",
    ...props,
    value: value,
    disabled: disabled,
    onChange: handleChange,
    onKeyDown: handleKeyDown,
    onBlur: handleBlur,
    className: (0, _classnames.default)(baseClassName, className),
    innerRef: inputRef,
    prefix: /*#__PURE__*/(0, _jsxRuntime.jsx)(_Button.default, {
      icon: decrementIcon,
      ghost: true,
      disabled: minDisabled,
      onClick: handleDec,
      onMouseDown: handleMouseDown,
      className: `${baseClassName}__dec`
    }),
    suffix: /*#__PURE__*/(0, _jsxRuntime.jsx)(_Button.default, {
      icon: incrementIcon,
      ghost: true,
      disabled: maxDisabled,
      onClick: handleInc,
      onMouseDown: handleMouseDown,
      className: `${baseClassName}__inc`
    })
  });
};
var _default = exports.default = InputNumber;