var _excluded = ["plusProps", "minusProps", "stepFocus", "type", "autoFit", "readOnly", "disabled", "inputProps", "value", "formatter", "precision", "trim", "max", "min", "placeholder", "maxLength", "autoFocus", "autoSelect", "leftIcon", "rightIcon", "clear", "allowClear", "children", "onClick", "onCompositionStart", "onCompositionUpdate", "onCompositionEnd", "onInput", "onChange", "onBlur", "onFocus"];
function _extends() { _extends = Object.assign ? Object.assign.bind() : 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 _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
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; }
import React, { forwardRef, useRef, useImperativeHandle, useEffect } from 'react';
import InputNumber from "./../Number";

// 内库使用-start
import MathUtil from "./../../../utils/MathUtil";
// 内库使用-end

/* 测试使用-start
import { MathUtil } from 'seedsui-react'
测试使用-end */

// 数值框
var NumberBox = /*#__PURE__*/forwardRef(function (_ref, ref) {
  var _ref$plusProps = _ref.plusProps,
    plusProps = _ref$plusProps === void 0 ? {} : _ref$plusProps,
    _ref$minusProps = _ref.minusProps,
    minusProps = _ref$minusProps === void 0 ? {} : _ref$minusProps,
    stepFocus = _ref.stepFocus,
    _ref$type = _ref.type,
    type = _ref$type === void 0 ? 'text' : _ref$type,
    autoFit = _ref.autoFit,
    readOnly = _ref.readOnly,
    disabled = _ref.disabled,
    _ref$inputProps = _ref.inputProps,
    inputProps = _ref$inputProps === void 0 ? {} : _ref$inputProps,
    value = _ref.value,
    formatter = _ref.formatter,
    precision = _ref.precision,
    trim = _ref.trim,
    max = _ref.max,
    min = _ref.min,
    placeholder = _ref.placeholder,
    maxLength = _ref.maxLength,
    autoFocus = _ref.autoFocus,
    autoSelect = _ref.autoSelect,
    leftIcon = _ref.leftIcon,
    rightIcon = _ref.rightIcon,
    clear = _ref.clear,
    allowClear = _ref.allowClear,
    children = _ref.children,
    onClick = _ref.onClick,
    onCompositionStart = _ref.onCompositionStart,
    onCompositionUpdate = _ref.onCompositionUpdate,
    onCompositionEnd = _ref.onCompositionEnd,
    onInput = _ref.onInput,
    onChange = _ref.onChange,
    onBlur = _ref.onBlur,
    onFocus = _ref.onFocus,
    props = _objectWithoutProperties(_ref, _excluded);
  var rootRef = useRef(null);
  var inputRef = useRef(null);
  useImperativeHandle(ref, function () {
    var _inputRef$current;
    return {
      rootDOM: rootRef.current,
      inputDOM: inputRef === null || inputRef === void 0 ? void 0 : inputRef.current,
      getRootDOM: function getRootDOM() {
        return rootRef.current;
      },
      getInputDOM: inputRef === null || inputRef === void 0 || (_inputRef$current = inputRef.current) === null || _inputRef$current === void 0 ? void 0 : _inputRef$current.getInputDOM,
      getInputRef: function getInputRef() {
        return inputRef;
      }
    };
  });
  useEffect(function () {
    var inputDOM = _getInputDOM();
    var val = (inputDOM !== null && inputDOM !== void 0 && inputDOM.value ? inputDOM.value : value) || '';
    updateState(val);
  }, [value]); // eslint-disable-line

  // 获取文本框
  function _getInputDOM() {
    var _inputRef$current2;
    if (inputRef !== null && inputRef !== void 0 && (_inputRef$current2 = inputRef.current) !== null && _inputRef$current2 !== void 0 && _inputRef$current2.getInputDOM) {
      return inputRef.current.getInputDOM();
    }
    return null;
  }

  // 更新禁用状态
  function updateState(val) {
    var _rootRef$current, _rootRef$current$quer, _rootRef$current2, _rootRef$current2$que;
    var minus = (_rootRef$current = rootRef.current) === null || _rootRef$current === void 0 || (_rootRef$current$quer = _rootRef$current.querySelector) === null || _rootRef$current$quer === void 0 ? void 0 : _rootRef$current$quer.call(_rootRef$current, '.numberbox-button-minus');
    var plus = (_rootRef$current2 = rootRef.current) === null || _rootRef$current2 === void 0 || (_rootRef$current2$que = _rootRef$current2.querySelector) === null || _rootRef$current2$que === void 0 ? void 0 : _rootRef$current2$que.call(_rootRef$current2, '.numberbox-button-plus');
    if (!isNaN(min) && !isNaN(val) && Number(val) <= Number(min)) {
      minus.setAttribute('disabled', 'true');
    } else {
      minus.removeAttribute('disabled');
    }
    if (!isNaN(max) && !isNaN(val) && Number(val) >= Number(max)) {
      plus.setAttribute('disabled', 'true');
    } else {
      plus.removeAttribute('disabled');
    }
  }

  // 修改值回调
  function handleChange(val) {
    if (disabled) return;
    var inputDOM = _getInputDOM();
    if (!inputDOM) return;

    // 非受控组件需要操作DOM
    if (value === undefined) {
      inputDOM.value = val;
      updateState(val);
    }
    if (onChange) onChange(val);
  }

  // 点击减
  function handleMinus(e) {
    var _inputRef$current3;
    e.stopPropagation();
    if (disabled) return;
    var inputDOM = _getInputDOM();
    if (!inputDOM) return;
    var val = inputRef === null || inputRef === void 0 || (_inputRef$current3 = inputRef.current) === null || _inputRef$current3 === void 0 ? void 0 : _inputRef$current3.correctValue(MathUtil.strip(Number(inputDOM.value || 0) - 1), 'blur');
    // Callback
    handleChange(val);
    if (minusProps.onClick) minusProps.onClick(e, val);
    if (stepFocus) {
      inputDOM.focus();
    }
  }

  // 点击加
  function handlePlus(e) {
    var _inputRef$current4;
    e.stopPropagation();
    if (disabled) return;
    var inputDOM = _getInputDOM();
    if (!inputDOM) return;
    if (isNaN(inputDOM === null || inputDOM === void 0 ? void 0 : inputDOM.value)) return;
    var val = inputRef === null || inputRef === void 0 || (_inputRef$current4 = inputRef.current) === null || _inputRef$current4 === void 0 ? void 0 : _inputRef$current4.correctValue(MathUtil.strip(Number(inputDOM.value || 0) + 1), 'blur');
    // Callback
    handleChange(val);
    if (plusProps.onClick) plusProps.onClick(e, val);
    if (stepFocus) {
      inputDOM.focus();
    }
  }

  // 文本框属性
  if (!inputProps) {
    // eslint-disable-next-line
    inputProps = {};
  }
  inputProps.className = inputProps.className ? "".concat(inputProps.className, " numberbox-input") : 'numberbox-input';

  // render
  function getInputDOM() {
    return /*#__PURE__*/React.createElement(InputNumber, {
      ref: inputRef,
      className: "numberbox-input-wrapper",
      type: "number",
      readOnly: readOnly,
      disabled: disabled,
      inputProps: inputProps,
      value: value,
      formatter: formatter,
      precision: precision,
      trim: trim,
      max: max,
      min: min,
      placeholder: placeholder,
      maxLength: maxLength,
      autoFocus: autoFocus // 渲染时自动获取焦点
      ,
      autoSelect: autoSelect // 渲染时自动选中
      ,
      leftIcon: leftIcon,
      rightIcon: rightIcon,
      allowClear: allowClear,
      clear: clear,
      onClick: onClick,
      onCompositionStart: onCompositionStart // 输入开始时
      ,
      onCompositionUpdate: onCompositionUpdate // 输入进行中
      ,
      onCompositionEnd: onCompositionEnd // 输入完成时
      ,
      onInput: onInput,
      onChange: handleChange,
      onBlur: onBlur,
      onFocus: onFocus
    }, children);
  }
  return /*#__PURE__*/React.createElement("div", _extends({}, props, {
    disabled: (!isNaN(min) && !isNaN(max) ? Number(min) >= Number(max) : false) || disabled,
    className: "numberbox".concat(props.className ? ' ' + props.className : ''),
    ref: rootRef
  }), /*#__PURE__*/React.createElement("div", _extends({}, minusProps, {
    type: "button",
    className: "numberbox-button numberbox-button-minus".concat(minusProps.className ? ' ' + minusProps.className : ''),
    onClick: handleMinus
  }), /*#__PURE__*/React.createElement("i", {
    className: "numberbox-button-minus-icon"
  })), getInputDOM(), /*#__PURE__*/React.createElement("div", _extends({}, plusProps, {
    type: "button",
    className: "numberbox-button numberbox-button-plus".concat(plusProps.className ? ' ' + plusProps.className : ''),
    onClick: handlePlus
  }), /*#__PURE__*/React.createElement("i", {
    className: "numberbox-button-plus-icon"
  })));
});
export default NumberBox;