"use strict";

require("core-js/modules/es.array.iterator.js");
require("core-js/modules/es.object.define-property.js");
require("core-js/modules/es.object.get-own-property-descriptor.js");
require("core-js/modules/es.object.to-string.js");
require("core-js/modules/es.string.iterator.js");
require("core-js/modules/es.weak-map.js");
require("core-js/modules/esnext.weak-map.delete-all.js");
require("core-js/modules/esnext.weak-map.emplace.js");
require("core-js/modules/web.dom-collections.iterator.js");
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.FcrInputNumber = void 0;
require("core-js/modules/es.parse-int.js");
require("core-js/modules/es.regexp.exec.js");
require("core-js/modules/es.regexp.test.js");
require("core-js/modules/es.string.trim.js");
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _classnames = _interopRequireDefault(require("classnames"));
var _react = _interopRequireWildcard(require("react"));
var _isNumber = _interopRequireDefault(require("lodash/isNumber"));
require("./index.css");
var _useClickAnywhere = require("../../hooks/use-click-anywhere");
var _icon = require("../icon");
var _type = require("../icon/type");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, "default": e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
var FcrInputNumber = exports.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 = (0, _react.useState)(false),
    _useState2 = (0, _slicedToArray2["default"])(_useState, 2),
    focused = _useState2[0],
    setFocused = _useState2[1];
  var _useState3 = (0, _react.useState)(''),
    _useState4 = (0, _slicedToArray2["default"])(_useState3, 2),
    innerVal = _useState4[0],
    setInnerVal = _useState4[1];
  var lastValidNum = (0, _react.useRef)();
  var inputRef = (0, _react.useRef)(null);
  (0, _react.useEffect)(function () {
    if ((0, _isNumber["default"])(value)) {
      setInnerVal("".concat(value));
      lastValidNum.current = value;
    }
  }, [value]);
  var getValidNumber = function getValidNumber(n) {
    if ((0, _isNumber["default"])(max)) {
      n = Math.min(max, n);
    }
    if ((0, _isNumber["default"])(min)) {
      n = Math.max(min, n);
    }
    return n;
  };
  var handleFocus = function handleFocus() {
    setFocused(true);
  };
  var handleBlur = function handleBlur() {
    setFocused(false);
    if (!(0, _isNumber["default"])(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 = (0, _classnames["default"])('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 ((0, _isNumber["default"])(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 ((0, _isNumber["default"])(min)) {
      n = Math.max(min, n);
    }
    setInnerVal("".concat(n));
    lastValidNum.current = n;
    onChange(n);
  };
  var clickAnywhere = (0, _react.useCallback)(function () {
    setFocused(false);
  }, [setFocused]);
  var ref = (0, _useClickAnywhere.useClickAnywhere)(clickAnywhere);
  var addCls = (0, _classnames["default"])('fcr-input-number__button', {
    'fcr-input-number__button--disabled': disabled || (0, _isNumber["default"])(max) && ((_lastValidNum$current3 = lastValidNum.current) !== null && _lastValidNum$current3 !== void 0 ? _lastValidNum$current3 : 0) >= max
  });
  var subCls = (0, _classnames["default"])('fcr-input-number__button', {
    'fcr-input-number__button--disabled': disabled || (0, _isNumber["default"])(min) && ((_lastValidNum$current4 = lastValidNum.current) !== null && _lastValidNum$current4 !== void 0 ? _lastValidNum$current4 : 0) <= min
  });
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
    className: cls,
    onClick: handleClick,
    ref: ref,
    children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("input", {
      ref: inputRef,
      placeholder: placeholder,
      onFocus: handleFocus,
      onBlur: handleBlur,
      value: innerVal,
      disabled: disabled,
      onChange: handleChange
    }), /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
      className: "fcr-input-number__button-group",
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
        className: addCls,
        onClick: handleAdd,
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_icon.FcrIcon, {
          type: _type.FcrIconType.FCR_ORDER_UP,
          size: 10,
          colors: {
            iconPrimary: 'currentColor'
          }
        })
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
        className: "fcr-input-number__divider"
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
        className: subCls,
        onClick: handleSubtract,
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_icon.FcrIcon, {
          type: _type.FcrIconType.FCR_ORDER_DOWN,
          size: 10,
          colors: {
            iconPrimary: 'currentColor'
          }
        })
      })]
    })]
  });
};