"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));

var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));

var _bem = require("@zarm-design/bem");

var _icons = require("@zarm-design/icons");

var _react = _interopRequireWildcard(require("react"));

var _configProvider = require("../config-provider");

var _useLongPress = _interopRequireDefault(require("../use-long-press"));

var _hooks = require("../utils/hooks");

var _BuildInConfig = _interopRequireDefault(require("./BuildInConfig"));

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { (0, _defineProperty2.default)(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

var Keyboard = /*#__PURE__*/(0, _react.forwardRef)(function (props, ref) {
  var className = props.className,
      style = props.style,
      type = props.type,
      dataSource = props.dataSource,
      onKeyClick = props.onKeyClick,
      restProps = (0, _objectWithoutProperties2.default)(props, ["className", "style", "type", "dataSource", "onKeyClick"]);

  var _useContext = (0, _react.useContext)(_configProvider.ConfigContext),
      globalLocal = _useContext.locale,
      prefixCls = _useContext.prefixCls;

  var locale = globalLocal === null || globalLocal === void 0 ? void 0 : globalLocal.Keyboard;
  var bem = (0, _bem.createBEM)('keyboard', {
    prefixCls: prefixCls
  });
  var cls = bem([className]);
  var getKeyConfig = dataSource || _BuildInConfig.default[type];

  var onKeyPress = function onKeyPress(e, text) {
    e.stopPropagation();
    var keyObj = (0, _typeof2.default)(text) === 'object' ? text : {
      text: text
    };
    var key = keyObj.value || keyObj.text;
    if (!text || keyObj.disabled) return;

    if (typeof onKeyClick === 'function') {
      onKeyClick(key);
    }
  };

  var timerRef = (0, _react.useRef)(0);
  var onKeyPressRef = (0, _hooks.useLatest)(onKeyPress);
  var longPressEvent = (0, _useLongPress.default)({
    // 长按连续删除
    onLongPress: function onLongPress(e) {
      clearInterval(timerRef.current);
      timerRef.current = window.setInterval(function () {
        onKeyPressRef.current(e, 'delete');
      }, 100);
    },
    onPress: function onPress(e) {
      clearInterval(timerRef.current);
      onKeyPressRef.current(e, 'delete');
    },
    onClear: function onClear() {
      return clearInterval(timerRef.current);
    }
  });
  var renderKey = (0, _react.useCallback)(function (text, index) {
    var keyObj = (0, _typeof2.default)(text) === 'object' ? text : {
      text: text
    };
    var commonProps = {
      key: +index,
      className: bem('item', [{
        blank: keyObj.text === '',
        ok: keyObj.text === 'ok',
        disabled: keyObj.disabled
      }]),
      style: {}
    };

    if (keyObj.rowSpan > 1) {
      commonProps.style = _objectSpread(_objectSpread({}, commonProps.style), {}, {
        gridRow: "auto / span ".concat(keyObj.rowSpan),
        height: '100%'
      });
    }

    if (keyObj.colSpan > 1) {
      commonProps.style = _objectSpread(_objectSpread({}, commonProps.style), {}, {
        gridColumn: "auto / span ".concat(keyObj.colSpan)
      });
    }

    var renderText = function renderText() {
      switch (keyObj.text) {
        case 'ok':
          return locale.confirmText;

        case 'delete':
          return /*#__PURE__*/_react.default.createElement(_icons.DeleteKey, {
            size: "lg",
            style: {
              pointerEvents: 'none'
            }
          });

        case 'close':
          return /*#__PURE__*/_react.default.createElement(_icons.Keyboard, {
            size: "lg"
          });

        default:
          return keyObj.text;
      }
    };

    if (keyObj.text === 'delete') return /*#__PURE__*/_react.default.createElement("div", (0, _extends2.default)({}, commonProps, longPressEvent), renderText());
    return /*#__PURE__*/_react.default.createElement("div", (0, _extends2.default)({}, commonProps, {
      onClick: function onClick(e) {
        var _onKeyPressRef$curren;

        return (_onKeyPressRef$curren = onKeyPressRef.current) === null || _onKeyPressRef$curren === void 0 ? void 0 : _onKeyPressRef$curren.call(onKeyPressRef, e, keyObj);
      }
    }), renderText());
  }, []);
  var gridTemplateColumns = Array.from(Array(getKeyConfig.columns).keys()).map(function () {
    return '1fr';
  }).join(' ');
  return /*#__PURE__*/_react.default.createElement("div", (0, _extends2.default)({
    ref: ref,
    className: cls,
    style: _objectSpread({
      gridTemplateColumns: gridTemplateColumns
    }, style),
    onClick: function onClick(e) {
      e.stopPropagation();
    }
  }, restProps), (getKeyConfig.keys || []).map(renderKey));
});
Keyboard.displayName = 'Keyboard';
Keyboard.defaultProps = {
  type: 'number'
};
var _default = Keyboard;
exports.default = _default;