function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
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 ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
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(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
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,
    rightItemsStyle = props.rightItemsStyle,
    customDisplay = props.customDisplay,
    style = props.style;
  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',
      style: {
        color: (rightItemsStyle === null || rightItemsStyle === void 0 ? void 0 : rightItemsStyle.textColor) || '#000000',
        backgroundColor: (rightItemsStyle === null || rightItemsStyle === void 0 ? void 0 : rightItemsStyle.buttonBackgroundColor) || '#ffffff'
      }
    }, {
      value: 'reset',
      label: resetText || 'Reset',
      disabled: false,
      type: 'text',
      style: {
        fontSize: '18px',
        color: (rightItemsStyle === null || rightItemsStyle === void 0 ? void 0 : rightItemsStyle.resetButtonTextColor) || '#D92D20',
        backgroundColor: (rightItemsStyle === null || rightItemsStyle === void 0 ? void 0 : rightItemsStyle.resetButtonBackgroundColor) || '#ffffff'
      }
    }, {
      value: 'done',
      label: doneText || 'Done',
      disabled: false,
      type: 'primary',
      size: 2,
      style: {
        fontSize: '18px',
        background: rightItemsStyle !== null && rightItemsStyle !== void 0 && rightItemsStyle.isDoneButtonFollowTheme ? 'var(--theme-color)' : (rightItemsStyle === null || rightItemsStyle === void 0 ? void 0 : rightItemsStyle.doneButtonBackgroundColor) || '#5d3f9f',
        color: rightItemsStyle !== null && rightItemsStyle !== void 0 && rightItemsStyle.isDoneButtonFollowTheme ? '#ffffff' : (rightItemsStyle === null || rightItemsStyle === void 0 ? void 0 : rightItemsStyle.doneButtonTextColor) || '#ffffff'
      }
    }];
  }, [doneText, resetText, rightItemsStyle]);

  // 给每个item增加背景色
  var itemsWithStyle = useMemo(function () {
    return items.map(function (item) {
      return _objectSpread(_objectSpread({}, item), {}, {
        style: {
          backgroundColor: (rightItemsStyle === null || rightItemsStyle === void 0 ? void 0 : rightItemsStyle.buttonBackgroundColor) || '#fff',
          color: (rightItemsStyle === null || rightItemsStyle === void 0 ? void 0 : rightItemsStyle.textColor) || '#000000'
        }
      });
    });
  }, [items, rightItemsStyle === null || rightItemsStyle === void 0 ? void 0 : rightItemsStyle.buttonBackgroundColor, rightItemsStyle === null || rightItemsStyle === void 0 ? void 0 : rightItemsStyle.textColor]);
  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 _val;
    val = typeof val === 'string' ? val : ((_val = val) === null || _val === void 0 || (_val = _val.target) === null || _val === void 0 ? void 0 : _val.value) || '';
    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 || onEnter(value);
    },
    ok: function ok() {
      onEnter === null || onEnter === 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);
  });
  var renderDisplayField = function renderDisplayField() {
    if (customDisplay) {
      if (typeof customDisplay === 'function') {
        return customDisplay({
          value: value,
          onChange: handleChangeValue,
          onValueSelect: setSelect
        });
      }
      return customDisplay;
    }
    return /*#__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,
      style: {
        backgroundColor: (rightItemsStyle === null || rightItemsStyle === void 0 ? void 0 : rightItemsStyle.buttonBackgroundColor) || '#fff',
        color: (rightItemsStyle === null || rightItemsStyle === void 0 ? void 0 : rightItemsStyle.textColor) || '#000000'
      }
    });
  };
  return /*#__PURE__*/React.createElement("div", {
    className: "pisell-virtual-keyboard-base",
    style: style
  }, renderDisplayField(), /*#__PURE__*/React.createElement(Presets, {
    presets: presets,
    onChange: handleChangeValue
  }), /*#__PURE__*/React.createElement(Keyboard, _extends({
    items: itemsWithStyle,
    rightItems: rightItems,
    onChange: handleKeyboardChange
  }, props.keyboardProps || {})));
};
export default BaseNumberKeyboard;