import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import "core-js/modules/es.parse-int.js";
import "core-js/modules/es.regexp.exec.js";
import "core-js/modules/es.regexp.test.js";
import "core-js/modules/es.string.trim.js";
import classNames from 'classnames';
import React, { useCallback, useEffect, useRef, useState } from 'react';
import isNumber from 'lodash/isNumber';
import './index.css';
import { useClickAnywhere } from '../../hooks/use-click-anywhere';
import { FcrIcon } from '../icon';
import { FcrIconType } from '../icon/type';
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
export var FcrInputNumber = function FcrInputNumber(_ref) {
  var _lastValidNum$current3, _lastValidNum$current4;
  var placeholder = _ref.placeholder,
    value = _ref.value,
    _ref$step = _ref.step,
    step = _ref$step === void 0 ? 1 : _ref$step,
    min = _ref.min,
    max = _ref.max,
    disabled = _ref.disabled,
    _ref$size = _ref.size,
    size = _ref$size === void 0 ? 'medium' : _ref$size,
    _ref$onChange = _ref.onChange,
    onChange = _ref$onChange === void 0 ? function () {} : _ref$onChange;
  var _useState = useState(false),
    _useState2 = _slicedToArray(_useState, 2),
    focused = _useState2[0],
    setFocused = _useState2[1];
  var _useState3 = useState(''),
    _useState4 = _slicedToArray(_useState3, 2),
    innerVal = _useState4[0],
    setInnerVal = _useState4[1];
  var lastValidNum = useRef();
  var inputRef = useRef(null);
  useEffect(function () {
    if (isNumber(value)) {
      setInnerVal("".concat(value));
      lastValidNum.current = value;
    }
  }, [value]);
  var getValidNumber = function getValidNumber(n) {
    if (isNumber(max)) {
      n = Math.min(max, n);
    }
    if (isNumber(min)) {
      n = Math.max(min, n);
    }
    return n;
  };
  var handleFocus = function handleFocus() {
    setFocused(true);
  };
  var handleBlur = function handleBlur() {
    setFocused(false);
    if (!isNumber(lastValidNum.current)) {
      setInnerVal('');
      return;
    }
    if (/^-?\d+$/.test(innerVal)) {
      var n = parseInt(innerVal, 10);
      var vn = getValidNumber(n);
      if (vn !== n) {
        setInnerVal("".concat(vn));
        lastValidNum.current = vn;
        onChange(vn);
      }
    } else {
      setInnerVal("".concat(lastValidNum.current));
    }
  };
  var handleChange = function handleChange(e) {
    var s = e.target.value.trim();
    setInnerVal(e.target.value);
    if (!s && lastValidNum.current !== null) {
      lastValidNum.current = null;
      onChange(null);
    }
    if (/^-?\d+$/.test(s)) {
      var n = parseInt(e.target.value, 10);
      var vn = getValidNumber(n);
      if (n === vn) {
        lastValidNum.current = vn;
        onChange(vn);
      }
    }
  };
  var handleClick = function handleClick() {
    if (inputRef.current) {
      inputRef.current.focus();
    }
  };
  var cls = classNames('fcr-input-number', {
    'fcr-input-number--focused': focused,
    'fcr-input-number-l': size === 'large',
    'fcr-input-number-m': size === 'medium',
    'fcr-input-number-s': size === 'small',
    'fcr-input-number--disabled': disabled
  });
  var handleAdd = function handleAdd(e) {
    var _lastValidNum$current;
    if (disabled) {
      return;
    }
    e.stopPropagation();
    setFocused(true);
    var n = ((_lastValidNum$current = lastValidNum.current) !== null && _lastValidNum$current !== void 0 ? _lastValidNum$current : 0) + step;
    if (isNumber(max)) {
      n = Math.min(max, n);
    }
    setInnerVal("".concat(n));
    lastValidNum.current = n;
    onChange(n);
  };
  var handleSubtract = function handleSubtract(e) {
    var _lastValidNum$current2;
    if (disabled) {
      return;
    }
    e.stopPropagation();
    setFocused(true);
    var n = ((_lastValidNum$current2 = lastValidNum.current) !== null && _lastValidNum$current2 !== void 0 ? _lastValidNum$current2 : 0) - step;
    if (isNumber(min)) {
      n = Math.max(min, n);
    }
    setInnerVal("".concat(n));
    lastValidNum.current = n;
    onChange(n);
  };
  var clickAnywhere = useCallback(function () {
    setFocused(false);
  }, [setFocused]);
  var ref = useClickAnywhere(clickAnywhere);
  var addCls = classNames('fcr-input-number__button', {
    'fcr-input-number__button--disabled': disabled || isNumber(max) && ((_lastValidNum$current3 = lastValidNum.current) !== null && _lastValidNum$current3 !== void 0 ? _lastValidNum$current3 : 0) >= max
  });
  var subCls = classNames('fcr-input-number__button', {
    'fcr-input-number__button--disabled': disabled || isNumber(min) && ((_lastValidNum$current4 = lastValidNum.current) !== null && _lastValidNum$current4 !== void 0 ? _lastValidNum$current4 : 0) <= min
  });
  return /*#__PURE__*/_jsxs("div", {
    className: cls,
    onClick: handleClick,
    ref: ref,
    children: [/*#__PURE__*/_jsx("input", {
      ref: inputRef,
      placeholder: placeholder,
      onFocus: handleFocus,
      onBlur: handleBlur,
      value: innerVal,
      disabled: disabled,
      onChange: handleChange
    }), /*#__PURE__*/_jsxs("div", {
      className: "fcr-input-number__button-group",
      children: [/*#__PURE__*/_jsx("div", {
        className: addCls,
        onClick: handleAdd,
        children: /*#__PURE__*/_jsx(FcrIcon, {
          type: FcrIconType.FCR_ORDER_UP,
          size: 10,
          colors: {
            iconPrimary: 'currentColor'
          }
        })
      }), /*#__PURE__*/_jsx("div", {
        className: "fcr-input-number__divider"
      }), /*#__PURE__*/_jsx("div", {
        className: subCls,
        onClick: handleSubtract,
        children: /*#__PURE__*/_jsx(FcrIcon, {
          type: FcrIconType.FCR_ORDER_DOWN,
          size: 10,
          colors: {
            iconPrimary: 'currentColor'
          }
        })
      })]
    })]
  });
};