function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0); } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i.return && (_r = _i.return(), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import React, { useEffect, useMemo, useState } from 'react';
import Delete from '@pisell/icon/es/Delete';
import { useControllableValue, useMemoizedFn } from 'ahooks';
import { isNumber, isUndefined } from '@pisell/utils';
import Presets from "./Presets";
import Keyboard from "../Keyboard";
import VirtualKeyInput from "../VirtualKeyInput";
import VirtualInput from "../VirtualInput";
import "./index.less";
var items = [{
  value: 1,
  label: '1',
  disabled: false,
  type: 'text'
}, {
  value: 2,
  label: '2',
  disabled: false,
  type: 'text'
}, {
  value: 3,
  label: '3',
  disabled: false,
  type: 'text'
}, {
  value: 4,
  label: '4',
  disabled: false,
  type: 'text'
}, {
  value: 5,
  label: '5',
  disabled: false,
  type: 'text'
}, {
  value: 6,
  label: '6',
  disabled: false,
  type: 'text'
}, {
  value: 7,
  label: '7',
  disabled: false,
  type: 'text'
}, {
  value: 8,
  label: '8',
  disabled: false,
  type: 'text'
}, {
  value: 9,
  label: '9',
  disabled: false,
  type: 'text'
}, {
  value: '0',
  label: '0',
  disabled: false,
  type: 'text'
}, {
  value: '00',
  label: '00',
  disabled: false,
  type: 'text'
}, {
  value: '.',
  label: '.',
  disabled: false,
  type: 'text'
}];
var BaseNumberKeyboard = function BaseNumberKeyboard(props) {
  var onChange = props.onChange,
    placeholder = props.placeholder,
    format = props.format,
    _props$defaultValue = props.defaultValue,
    defaultValue = _props$defaultValue === void 0 ? '' : _props$defaultValue,
    doneText = props.doneText,
    resetText = props.resetText,
    max = props.max,
    min = props.min,
    onEnter = props.onEnter,
    presets = props.presets,
    defaultSelect = props.defaultSelect;
  var _useControllableValue = useControllableValue(props, {
      defaultValue: defaultValue
    }),
    _useControllableValue2 = _slicedToArray(_useControllableValue, 2),
    value = _useControllableValue2[0],
    setValue = _useControllableValue2[1];
  var _useState = useState(defaultSelect !== null && defaultSelect !== void 0 ? defaultSelect : false),
    _useState2 = _slicedToArray(_useState, 2),
    select = _useState2[0],
    setSelect = _useState2[1];
  useEffect(function () {
    document.body.id = 'body';
  }, []);
  var rightItems = useMemo(function () {
    return [{
      value: 'delete',
      label: /*#__PURE__*/React.createElement(Delete, {
        className: "virtual-keyboard-input-delete-icon"
      }),
      disabled: false,
      type: 'text'
    }, {
      value: 'reset',
      label: resetText || 'Reset',
      disabled: false,
      type: 'text',
      style: {
        fontSize: '18px',
        color: '#D92D20'
      }
    }, {
      value: 'done',
      label: doneText || 'Done',
      disabled: false,
      type: 'primary',
      size: 2,
      style: {
        fontSize: '18px',
        background: 'var(--Green-light-600, #4CA30D)',
        color: '#fff'
      }
    }];
  }, [doneText, resetText]);
  var checkValue = function checkValue(val) {
    // 超过最大值时使用最大值
    if (isNumber(max) && Number(val) > max) {
      var newValue = Math.min(Number(val), Number(max));
      setValue("".concat(newValue));
      return {
        message: ''
      };
    }
    // 小于最小值时使用最小值
    if (isNumber(min) && Number(val) < min) {
      var _newValue = Math.max(Number(val), Number(min));
      setValue("".concat(_newValue));
      return {
        message: ''
      };
    }
  };
  var handleChangeValue = function handleChangeValue(val) {
    var detail = checkValue(val);
    if (detail) return;
    setValue(val);
  };
  var actions = {
    '.': function _(val) {
      // 如果已经有小数点了，则不处理
      if (value.includes('.')) return;
      actions.default(val);
    },
    delete: function _delete() {
      // 选中时删除全部 否则删除最后一位
      handleChangeValue(select ? '' : "".concat(value || "").slice(0, -1));
    },
    reset: function reset() {
      // 重置到默认值
      handleChangeValue("".concat(defaultValue || "") || '');
    },
    done: function done() {
      onEnter === null || onEnter === void 0 ? void 0 : onEnter(value);
    },
    ok: function ok() {
      onEnter === null || onEnter === void 0 ? void 0 : onEnter(value);
    },
    default: function _default(val) {
      // 选中时输入覆盖原有数据
      handleChangeValue(select ? "".concat(val) : "".concat(value).concat(val));
    }
  };
  var handleKeyboardChange = useMemoizedFn(function (val) {
    return (actions[val] || actions.default)(val);
  });
  return /*#__PURE__*/React.createElement("div", {
    className: "pisell-virtual-keyboard-base"
  }, /*#__PURE__*/React.createElement(VirtualKeyInput, {
    value: value,
    onChange: function onChange(e) {
      return handleChangeValue(e.target.value);
    },
    renderInput: function renderInput(props) {
      if (isUndefined(props.value) || props.value === '') {
        return /*#__PURE__*/React.createElement(VirtualInput, {
          autoFocus: true,
          placeholder: placeholder,
          value: props.value
        });
      }
      return (format === null || format === void 0 ? void 0 : format(props.value)) || props.value;
    },
    showDelete: false,
    onValueSelect: setSelect,
    defaultSelect: defaultSelect
  }), /*#__PURE__*/React.createElement(Presets, {
    presets: presets,
    onChange: handleChangeValue
  }), /*#__PURE__*/React.createElement(Keyboard, {
    items: items,
    rightItems: rightItems,
    onChange: handleKeyboardChange
  }));
};
export default BaseNumberKeyboard;