"use strict";

exports.__esModule = true;
exports.default = void 0;

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

var _clsx = _interopRequireDefault(require("clsx"));

var _reactTransitionGroup = require("react-transition-group");

var _utils = require("../utils");

var _NumberKeyboardKey = _interopRequireDefault(require("./NumberKeyboardKey"));

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

var _useClickAway = _interopRequireDefault(require("../hooks/use-click-away"));

var _ConfigProviderContext = _interopRequireDefault(require("../config-provider/ConfigProviderContext"));

var _excluded = ["className", "style"];

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function _extends() { _extends = Object.assign || 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 _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; }

var NumberKeyboard = function NumberKeyboard(_ref) {
  var className = _ref.className,
      style = _ref.style,
      props = _objectWithoutPropertiesLoose(_ref, _excluded);

  var _useContext = (0, _react.useContext)(_ConfigProviderContext.default),
      prefixCls = _useContext.prefixCls,
      createNamespace = _useContext.createNamespace;

  var _createNamespace = createNamespace('number-keyboard', prefixCls),
      bem = _createNamespace[0];

  var root = (0, _react.useRef)();

  var genBasicKeys = function genBasicKeys() {
    var keys = Array(9).fill('').map(function (_, i) {
      return {
        text: i + 1
      };
    });

    if (props.randomKeyOrder) {
      keys.sort(function () {
        return Math.random() > 0.5 ? 1 : -1;
      });
    }

    return keys;
  };

  var genDefaultKeys = function genDefaultKeys() {
    return [].concat(genBasicKeys(), [{
      text: props.extraKey,
      type: 'extra'
    }, {
      text: 0
    }, {
      text: props.showDeleteKey ? props.deleteButtonText : '',
      type: props.showDeleteKey ? 'delete' : ''
    }]);
  };

  var genCustomKeys = function genCustomKeys() {
    var keys = genBasicKeys();
    var extraKey = props.extraKey;
    var extraKeys = Array.isArray(extraKey) ? extraKey : [extraKey];

    if (extraKeys.length === 1) {
      keys.push({
        text: 0,
        wider: true
      }, {
        text: extraKeys[0],
        type: 'extra'
      });
    } else if (extraKeys.length === 2) {
      keys.push({
        text: extraKeys[0],
        type: 'extra'
      }, {
        text: 0
      }, {
        text: extraKeys[1],
        type: 'extra'
      });
    }

    return keys;
  };

  var keys = (0, _react.useMemo)(function () {
    return props.theme === 'custom' ? genCustomKeys() : genDefaultKeys();
  }, [props.theme, genCustomKeys, genDefaultKeys]);

  var onBlur = function onBlur() {
    if (props.visible) {
      props.onBlur == null ? void 0 : props.onBlur();
    }
  };

  var onClose = function onClose() {
    props.onClose == null ? void 0 : props.onClose();

    if (props.blurOnClose) {
      onBlur();
    }
  };

  var onAnimationEnd = function onAnimationEnd() {
    var _props;

    (_props = props[props.visible ? 'onShow' : 'onHide']) == null ? void 0 : _props.call(props);
  };

  var onPress = function onPress(text, type) {
    if (text === '') {
      if (type === 'extra') {
        onBlur();
      }

      return;
    }

    var value = props.value;

    if (type === 'delete') {
      props.onDelete == null ? void 0 : props.onDelete();
      props.onChange == null ? void 0 : props.onChange(value.slice(0, value.length - 1));
    } else if (type === 'close') {
      onClose();
    } else if (value.length < props.maxlength) {
      props.onInput == null ? void 0 : props.onInput(text);
      props.onChange == null ? void 0 : props.onChange(value + text);
    }
  };

  var renderTitle = function renderTitle() {
    var title = props.title,
        theme = props.theme,
        closeButtonText = props.closeButtonText;
    var showClose = closeButtonText && theme === 'default';
    var showTitle = title || showClose;

    if (!showTitle) {
      return null;
    }

    return /*#__PURE__*/_react.default.createElement("div", {
      className: (0, _clsx.default)(bem('header'))
    }, title && /*#__PURE__*/_react.default.createElement("h2", {
      className: (0, _clsx.default)(bem('title'))
    }, title), showClose && /*#__PURE__*/_react.default.createElement("button", {
      type: "button",
      className: (0, _clsx.default)(bem('close')),
      onClick: onClose
    }, closeButtonText));
  };

  var renderKeys = function renderKeys() {
    return keys.map(function (key, i) {
      var keySlots = null;

      if (!key.type) {
        keySlots = props.numberKeyRender == null ? void 0 : props.numberKeyRender(key);
      }

      if (key.type === 'delete') {
        keySlots = props.deleteRender == null ? void 0 : props.deleteRender();
      }

      if (key.type === 'extra') {
        keySlots = props.extraKeyRender == null ? void 0 : props.extraKeyRender();
      }

      return /*#__PURE__*/_react.default.createElement(_NumberKeyboardKey.default // eslint-disable-next-line react/no-array-index-key
      , {
        // eslint-disable-next-line react/no-array-index-key
        key: i,
        text: key.text,
        type: key.type,
        wider: key.wider,
        color: key.color,
        onPress: onPress,
        // eslint-disable-next-line react/no-children-prop
        children: keySlots
      });
    });
  };

  var renderSidebar = function renderSidebar() {
    if (props.theme === 'custom') {
      return /*#__PURE__*/_react.default.createElement("div", {
        className: (0, _clsx.default)(bem('sidebar'))
      }, props.showDeleteKey && /*#__PURE__*/_react.default.createElement(_NumberKeyboardKey.default, {
        large: true,
        text: props.deleteButtonText,
        type: "delete",
        onPress: onPress
      }), /*#__PURE__*/_react.default.createElement(_NumberKeyboardKey.default, {
        large: true,
        text: props.closeButtonText,
        type: "close",
        color: "blue",
        loading: props.closeButtonLoading,
        onPress: onPress
      }));
    }

    return null;
  };

  (0, _hooks.useUpdateEffect)(function () {
    if (!props.transition) {
      var _props2;

      (_props2 = props[props.visible ? 'onShow' : 'onHide']) == null ? void 0 : _props2.call(props);
    }
  }, [props.visible]);
  (0, _useClickAway.default)(root, props.hideOnClickOutside ? onBlur : _utils.noop, 'touchstart');
  var Title = renderTitle();

  var Content = /*#__PURE__*/_react.default.createElement(_reactTransitionGroup.CSSTransition, {
    mountOnEnter: true,
    unmountOnExit: true,
    nodeRef: root,
    in: props.visible,
    timeout: 300,
    classNames: props.transition ? 'rv-slide-up' : '',
    onExited: onAnimationEnd
  }, /*#__PURE__*/_react.default.createElement("div", {
    ref: root,
    style: _extends({}, style, (0, _utils.getZIndexStyle)(props.zIndex)),
    className: (0, _clsx.default)(className, bem({
      unfit: !props.safeAreaInsetBottom,
      'with-title': !!Title
    })),
    onTouchStart: _utils.stopPropagation
  }, Title, /*#__PURE__*/_react.default.createElement("div", {
    className: (0, _clsx.default)(bem('body'))
  }, /*#__PURE__*/_react.default.createElement("div", {
    className: (0, _clsx.default)(bem('keys'))
  }, renderKeys()), renderSidebar())));

  return Content;
};

NumberKeyboard.defaultProps = {
  transition: true,
  blurOnClose: true,
  showDeleteKey: true,
  hideOnClickOutside: true,
  safeAreaInsetBottom: true,
  theme: 'default',
  value: '',
  extraKey: '',
  maxlength: Number.MAX_VALUE
};
var _default = NumberKeyboard;
exports.default = _default;