import _extends from "@babel/runtime/helpers/extends";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _typeof from "@babel/runtime/helpers/typeof";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";

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) { _defineProperty(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; }

import { createBEM } from '@zarm-design/bem';
import { DeleteKey as DeleteKeyIcon, Keyboard as KeyboardIcon } from '@zarm-design/icons';
import React, { forwardRef, useContext, useRef, useCallback } from 'react';
import { ConfigContext } from '../config-provider';
import useLongPress from '../use-long-press';
import { useLatest } from '../utils/hooks';
import BuildInConfig from './BuildInConfig';
var Keyboard = /*#__PURE__*/forwardRef(function (props, ref) {
  var className = props.className,
      style = props.style,
      type = props.type,
      dataSource = props.dataSource,
      onKeyClick = props.onKeyClick,
      restProps = _objectWithoutProperties(props, ["className", "style", "type", "dataSource", "onKeyClick"]);

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

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

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

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

  var timerRef = useRef(0);
  var onKeyPressRef = useLatest(onKeyPress);
  var longPressEvent = useLongPress({
    // 长按连续删除
    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 = useCallback(function (text, index) {
    var keyObj = _typeof(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.createElement(DeleteKeyIcon, {
            size: "lg",
            style: {
              pointerEvents: 'none'
            }
          });

        case 'close':
          return /*#__PURE__*/React.createElement(KeyboardIcon, {
            size: "lg"
          });

        default:
          return keyObj.text;
      }
    };

    if (keyObj.text === 'delete') return /*#__PURE__*/React.createElement("div", _extends({}, commonProps, longPressEvent), renderText());
    return /*#__PURE__*/React.createElement("div", _extends({}, 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.createElement("div", _extends({
    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'
};
export default Keyboard;