"use strict";

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

function _react() {
  const data = _interopRequireWildcard(require("react"));

  _react = function () {
    return data;
  };

  return data;
}

function _tslib() {
  const data = require("tslib");

  _tslib = function () {
    return data;
  };

  return data;
}

function _jsxRuntime() {
  const data = require("react/jsx-runtime");

  _jsxRuntime = function () {
    return data;
  };

  return data;
}

function _clsx() {
  const data = _interopRequireDefault(require("clsx"));

  _clsx = function () {
    return data;
  };

  return data;
}

function _reactTransitionGroup() {
  const data = require("react-transition-group");

  _reactTransitionGroup = function () {
    return data;
  };

  return data;
}

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"));

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 (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; }

/* eslint-disable react-hooks/exhaustive-deps */
const NumberKeyboard = _a => {
  var {
    className,
    style
  } = _a,
      props = (0, _tslib().__rest)(_a, ["className", "style"]);
  const {
    prefixCls,
    createNamespace
  } = (0, _react().useContext)(_ConfigProviderContext.default);
  const [bem] = createNamespace('number-keyboard', prefixCls);
  const root = (0, _react().useRef)();

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

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

    return keys;
  };

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

  const genCustomKeys = () => {
    const keys = genBasicKeys();
    const {
      extraKey
    } = props;
    const 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;
  };

  const keys = (0, _react().useMemo)(() => props.theme === 'custom' ? genCustomKeys() : genDefaultKeys(), [props.theme, genCustomKeys, genDefaultKeys]);

  const onBlur = () => {
    var _a;

    if (props.visible) {
      (_a = props.onBlur) === null || _a === void 0 ? void 0 : _a.call(props);
    }
  };

  const onClose = () => {
    var _a;

    (_a = props.onClose) === null || _a === void 0 ? void 0 : _a.call(props);

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

  const onAnimationEnd = () => {
    var _a;

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

  const onPress = (text, type) => {
    var _a, _b, _c, _d;

    if (text === '') {
      if (type === 'extra') {
        onBlur();
      }

      return;
    }

    const {
      value
    } = props;

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

  const renderTitle = () => {
    const {
      title,
      theme,
      closeButtonText
    } = props;
    const showClose = closeButtonText && theme === 'default';
    const showTitle = title || showClose;

    if (!showTitle) {
      return null;
    }

    return (0, _jsxRuntime().jsxs)("div", Object.assign({
      className: (0, _clsx().default)(bem('header'))
    }, {
      children: [title && (0, _jsxRuntime().jsx)("h2", Object.assign({
        className: (0, _clsx().default)(bem('title'))
      }, {
        children: title
      })), showClose && (0, _jsxRuntime().jsx)("button", Object.assign({
        type: "button",
        className: (0, _clsx().default)(bem('close')),
        onClick: onClose
      }, {
        children: closeButtonText
      }))]
    }));
  };

  const renderKeys = () => keys.map((key, i) => {
    var _a, _b, _c;

    let keySlots = null;

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

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

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

    return (0, _jsxRuntime().jsx)(_NumberKeyboardKey.default // eslint-disable-next-line react/no-array-index-key
    , {
      text: key.text,
      type: key.type,
      wider: key.wider,
      color: key.color,
      onPress: onPress,
      // eslint-disable-next-line react/no-children-prop
      children: keySlots
    }, i);
  });

  const renderSidebar = () => {
    if (props.theme === 'custom') {
      return (0, _jsxRuntime().jsxs)("div", Object.assign({
        className: (0, _clsx().default)(bem('sidebar'))
      }, {
        children: [props.showDeleteKey && (0, _jsxRuntime().jsx)(_NumberKeyboardKey.default, {
          large: true,
          text: props.deleteButtonText,
          type: "delete",
          onPress: onPress
        }), (0, _jsxRuntime().jsx)(_NumberKeyboardKey.default, {
          large: true,
          text: props.closeButtonText,
          type: "close",
          color: "blue",
          loading: props.closeButtonLoading,
          onPress: onPress
        })]
      }));
    }

    return null;
  };

  (0, _hooks.useUpdateEffect)(() => {
    var _a;

    if (!props.transition) {
      (_a = props[props.visible ? 'onShow' : 'onHide']) === null || _a === void 0 ? void 0 : _a.call(props);
    }
  }, [props.visible]);
  (0, _useClickAway.default)(root, props.hideOnClickOutside ? onBlur : _utils.noop, 'touchstart');
  const Title = renderTitle();
  const Content = (0, _jsxRuntime().jsx)(_reactTransitionGroup().CSSTransition, Object.assign({
    mountOnEnter: true,
    unmountOnExit: true,
    nodeRef: root,
    in: props.visible,
    timeout: 300,
    classNames: props.transition ? 'rv-slide-up' : '',
    onExited: onAnimationEnd
  }, {
    children: (0, _jsxRuntime().jsxs)("div", Object.assign({
      ref: root,
      style: Object.assign(Object.assign({}, style), (0, _utils.getZIndexStyle)(props.zIndex)),
      className: (0, _clsx().default)(className, bem({
        unfit: !props.safeAreaInsetBottom,
        'with-title': !!Title
      })),
      onTouchStart: _utils.stopPropagation
    }, {
      children: [Title, (0, _jsxRuntime().jsxs)("div", Object.assign({
        className: (0, _clsx().default)(bem('body'))
      }, {
        children: [(0, _jsxRuntime().jsx)("div", Object.assign({
          className: (0, _clsx().default)(bem('keys'))
        }, {
          children: 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;