var _excluded = ["disabled", "plusAttribute", "minusAttribute", "inputAttribute", "defaultValue", "value", "digits", "max", "min", "placeholder", "maxLength", "readOnly", "autoFocus", "autoSelect", "clickButtonFocus", "licon", "liconAttribute", "ricon", "riconAttribute", "onClick", "onChange", "onBlur", "onFocus"],
    _excluded2 = ["onClick"];

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

// require PrototypeMath.js, 用于解决加减法精度丢失的问题
import React, { forwardRef, useRef, useImperativeHandle, useEffect, useState } from 'react';
import InputNumber from './../InputNumber'; // 数值框

var NumBox = forwardRef(function (_ref, ref) {
  var disabled = _ref.disabled,
      _ref$plusAttribute = _ref.plusAttribute,
      plusAttribute = _ref$plusAttribute === void 0 ? {} : _ref$plusAttribute,
      _ref$minusAttribute = _ref.minusAttribute,
      minusAttribute = _ref$minusAttribute === void 0 ? {} : _ref$minusAttribute,
      _ref$inputAttribute = _ref.inputAttribute,
      inputAttribute = _ref$inputAttribute === void 0 ? {} : _ref$inputAttribute,
      defaultValue = _ref.defaultValue,
      value = _ref.value,
      digits = _ref.digits,
      max = _ref.max,
      min = _ref.min,
      placeholder = _ref.placeholder,
      _ref$maxLength = _ref.maxLength,
      maxLength = _ref$maxLength === void 0 ? '16' : _ref$maxLength,
      readOnly = _ref.readOnly,
      autoFocus = _ref.autoFocus,
      autoSelect = _ref.autoSelect,
      clickButtonFocus = _ref.clickButtonFocus,
      licon = _ref.licon,
      liconAttribute = _ref.liconAttribute,
      ricon = _ref.ricon,
      riconAttribute = _ref.riconAttribute,
      onClick = _ref.onClick,
      onChange = _ref.onChange,
      onBlur = _ref.onBlur,
      onFocus = _ref.onFocus,
      others = _objectWithoutPropertiesLoose(_ref, _excluded);

  var rootRef = useRef(null);

  var _useState = useState(null),
      inputRef = _useState[0],
      setInputRef = _useState[1];

  useImperativeHandle(ref, function () {
    return rootRef.current;
  });

  var _useState2 = useState(false),
      minDisabled = _useState2[0],
      setMinDisabled = _useState2[1];

  var _useState3 = useState(false),
      maxDisabled = _useState3[0],
      setMaxDisabled = _useState3[1];

  useEffect(function () {
    var _inputRef$input;

    if (!(inputRef === null || inputRef === void 0 ? void 0 : inputRef.input)) return;
    var val = (inputRef === null || inputRef === void 0 ? void 0 : (_inputRef$input = inputRef.input) === null || _inputRef$input === void 0 ? void 0 : _inputRef$input.value) || '';
    updateState(val);
  }, [inputRef]); // eslint-disable-line
  // 更新禁用状态

  function updateState(val) {
    if (!isNaN(min) && !isNaN(val) && Number(val) <= Number(min)) {
      setMinDisabled(true);
    } else {
      setMinDisabled(false);
    }

    if (!isNaN(max) && !isNaN(val) && Number(val) >= Number(max)) {
      setMaxDisabled(true);
    } else {
      setMaxDisabled(false);
    }
  } // 修改值回调


  function handleChange(e, val) {
    // 非受控组件需要操作DOM
    if (defaultValue || defaultValue === '') {
      var _e$target;

      if (e === null || e === void 0 ? void 0 : (_e$target = e.target) === null || _e$target === void 0 ? void 0 : _e$target.input) e.target.input.value = val;
    }

    updateState(val);
    if (onChange) onChange(e, val);
  } // 点击减


  function handleMinus(e) {
    var _inputRef$input2;

    var event = {
      target: inputRef
    };
    var val = inputRef.correctNumber(Math.Calc.subtract(inputRef === null || inputRef === void 0 ? void 0 : (_inputRef$input2 = inputRef.input) === null || _inputRef$input2 === void 0 ? void 0 : _inputRef$input2.value, 1)); // Callback

    handleChange(event, val);
    if (minusAttribute.onClick) minusAttribute.onClick(e, val);

    if (clickButtonFocus) {
      inputRef.input.focus();
    }
  } // 点击加


  function handlePlus(e) {
    var _inputRef$input3, _inputRef$input4;

    var event = {
      target: inputRef
    };
    if (isNaN(inputRef === null || inputRef === void 0 ? void 0 : (_inputRef$input3 = inputRef.input) === null || _inputRef$input3 === void 0 ? void 0 : _inputRef$input3.value)) return;
    var val = inputRef.correctNumber(Math.Calc.add(inputRef === null || inputRef === void 0 ? void 0 : (_inputRef$input4 = inputRef.input) === null || _inputRef$input4 === void 0 ? void 0 : _inputRef$input4.value, 1)); // Callback

    handleChange(event, val);
    if (plusAttribute.onClick) plusAttribute.onClick(e, val);

    if (clickButtonFocus) {
      var _inputRef$input5;

      inputRef === null || inputRef === void 0 ? void 0 : (_inputRef$input5 = inputRef.input) === null || _inputRef$input5 === void 0 ? void 0 : _inputRef$input5.focus();
    }
  } // 点击容器


  function handleClick(e) {
    var _inputRef$input8;

    e.stopPropagation();
    if (disabled) return;
    var target = e.target;

    if (liconAttribute && liconAttribute.onClick && target.classList.contains('licon')) {
      var _inputRef$input6;

      liconAttribute.onClick(e, inputRef === null || inputRef === void 0 ? void 0 : (_inputRef$input6 = inputRef.input) === null || _inputRef$input6 === void 0 ? void 0 : _inputRef$input6.value);
      return;
    }

    if (riconAttribute && riconAttribute.onClick && target.classList.contains('ricon')) {
      var _inputRef$input7;

      riconAttribute.onClick(e, inputRef === null || inputRef === void 0 ? void 0 : (_inputRef$input7 = inputRef.input) === null || _inputRef$input7 === void 0 ? void 0 : _inputRef$input7.value);
      return;
    }

    if (target.classList.contains('numbox-input')) {
      // 由InputNumber透传inputAttribute.onClick
      return;
    }

    if (target.classList.contains('numbox-button-plus')) {
      handlePlus(e);
      return;
    }

    if (target.classList.contains('numbox-button-minus')) {
      handleMinus(e);
      return;
    }

    if (onClick) onClick(e, inputRef === null || inputRef === void 0 ? void 0 : (_inputRef$input8 = inputRef.input) === null || _inputRef$input8 === void 0 ? void 0 : _inputRef$input8.value);
  } // 文本框属性


  if (!inputAttribute) {
    inputAttribute = {};
  }

  inputAttribute.className = inputAttribute.className ? inputAttribute.className + " numbox-input" : 'numbox-input'; // render

  function getInputDOM() {
    return /*#__PURE__*/React.createElement(InputNumber, {
      ref: setInputRef,
      className: "numbox-input-box",
      type: "number",
      digits: digits,
      inputAttribute: inputAttribute,
      defaultValue: defaultValue,
      value: value,
      min: min,
      max: max,
      maxLength: maxLength,
      disabled: disabled,
      readOnly: readOnly,
      placeholder: placeholder,
      onChange: handleChange,
      autoFocus: autoFocus,
      autoSelect: autoSelect,
      onFocus: onFocus,
      onBlur: onBlur
    });
  } // 过滤已经回调的属性


  function filterProps(props) {
    if (!props) return {};

    var onClick = props.onClick,
        otherProps = _objectWithoutPropertiesLoose(props, _excluded2);

    return _extends({}, otherProps);
  } // 剔除掉onClick事件, 因为在容器onClick已经回调了


  var otherLiconAttribute = filterProps(liconAttribute);
  var otherRiconAttribute = filterProps(riconAttribute);
  return /*#__PURE__*/React.createElement("div", _extends({
    ref: rootRef
  }, others, {
    disabled: (!isNaN(min) && !isNaN(max) ? Number(min) >= Number(max) : false) || disabled,
    className: "numbox" + (others.className ? ' ' + others.className : ''),
    onClick: handleClick
  }), /*#__PURE__*/React.createElement("input", _extends({
    value: "-",
    disabled: minDisabled
  }, plusAttribute, {
    type: "button",
    className: "numbox-button numbox-button-minus" + (plusAttribute.className ? ' ' + plusAttribute.className : '')
  })), licon && licon, otherLiconAttribute && !Object.isEmptyObject(otherLiconAttribute) && /*#__PURE__*/React.createElement("i", _extends({}, otherLiconAttribute, {
    className: "licon icon" + (otherLiconAttribute.className ? ' ' + liconAttribute.className : '')
  })), getInputDOM(), otherRiconAttribute && !Object.isEmptyObject(otherRiconAttribute) && /*#__PURE__*/React.createElement("i", _extends({}, otherRiconAttribute, {
    className: "ricon icon" + (otherRiconAttribute.className ? ' ' + otherRiconAttribute.className : '')
  })), ricon && ricon, /*#__PURE__*/React.createElement("input", _extends({
    value: "+",
    disabled: maxDisabled
  }, minusAttribute, {
    type: "button",
    className: "numbox-button numbox-button-plus" + (minusAttribute.className ? ' ' + minusAttribute.className : '')
  })));
});
export default NumBox;