var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var stdin_exports = {};
__export(stdin_exports, {
  default: () => stdin_default
});
module.exports = __toCommonJS(stdin_exports);
var import_react = __toESM(require("react"));
var import_classnames = __toESM(require("classnames"));
var import_popup = require("../popup");
var import_utils = require("./utils");
var import_ConfigProviderContext = __toESM(require("../config-provider/ConfigProviderContext"));
var import_NumberKeyboardKey = __toESM(require("./NumberKeyboardKey"));
var import_use_click_away = __toESM(require("../hooks/use-click-away"));
const NumberKeyboard = (props) => {
  const {
    prefixCls,
    createNamespace
  } = (0, import_react.useContext)(import_ConfigProviderContext.default);
  const [bem] = createNamespace("number-keyboard", prefixCls);
  const {
    visible,
    safeAreaInsetBottom,
    theme,
    blurOnClose,
    showDeleteKey,
    hideOnClickOutside,
    zIndex,
    teleport,
    title,
    closeButtonText,
    titleLeft
  } = props;
  const root = (0, import_react.useRef)(null);
  const showClose = closeButtonText && theme === "default";
  const showTitle = !!(title || showClose || titleLeft);
  const keys = (0, import_react.useMemo)(() => theme === "custom" ? (0, import_utils.genCustomKeys)(props) : (0, import_utils.genDefaultKeys)(props), [theme]);
  const onBlur = () => {
    var _a;
    if (visible) {
      (_a = props.onBlur) == null ? void 0 : _a.call(props);
    }
  };
  const onClose = () => {
    var _a;
    (_a = props.onClose) == null ? void 0 : _a.call(props);
    if (blurOnClose) {
      onBlur();
    }
  };
  const onPress = (text, type) => {
    var _a, _b;
    if (text === "") {
      if (type === "extra") {
        onBlur();
      }
      return;
    }
    if (type === "delete") {
      (_a = props.onDelete) == null ? void 0 : _a.call(props);
    } else if (type === "close") {
      onClose();
    } else {
      (_b = props.onInput) == null ? void 0 : _b.call(props, text);
    }
  };
  const renderTitle = () => {
    if (!showTitle) {
      return null;
    }
    return /* @__PURE__ */ import_react.default.createElement("div", {
      className: (0, import_classnames.default)(bem("header"))
    }, titleLeft && /* @__PURE__ */ import_react.default.createElement("span", {
      className: (0, import_classnames.default)(bem("title-left"))
    }, titleLeft), title && /* @__PURE__ */ import_react.default.createElement("h2", {
      className: (0, import_classnames.default)(bem("title"))
    }, title), showClose && /* @__PURE__ */ import_react.default.createElement("button", {
      type: "button",
      className: (0, import_classnames.default)(bem("close")),
      onClick: onClose
    }, closeButtonText));
  };
  const renderKeys = () => {
    return keys.map((key) => {
      return /* @__PURE__ */ import_react.default.createElement(import_NumberKeyboardKey.default, {
        key: `${key.text}${key.type}`,
        text: key.text,
        type: key.type,
        wider: key.wider,
        color: key.color,
        onPress
      });
    });
  };
  const renderSidebar = () => {
    if (theme === "custom") {
      return /* @__PURE__ */ import_react.default.createElement("div", {
        className: (0, import_classnames.default)(bem("sidebar"))
      }, showDeleteKey && /* @__PURE__ */ import_react.default.createElement(import_NumberKeyboardKey.default, {
        large: true,
        text: props.deleteButtonText,
        type: "delete",
        onPress
      }), /* @__PURE__ */ import_react.default.createElement(import_NumberKeyboardKey.default, {
        large: true,
        text: props.closeButtonText,
        type: "close",
        color: "blue",
        loading: props.closeButtonLoading,
        onPress
      }));
    }
    return null;
  };
  (0, import_use_click_away.default)(root, onBlur, "touchstart", hideOnClickOutside);
  return /* @__PURE__ */ import_react.default.createElement(import_popup.Popup, {
    visible,
    preventDefaultMouseDown: true,
    overlay: false,
    round: showTitle,
    zIndex,
    position: "bottom",
    duration: 300,
    teleport,
    safeAreaInsetBottom,
    onClose
  }, /* @__PURE__ */ import_react.default.createElement("div", {
    ref: root,
    className: (0, import_classnames.default)(bem())
  }, renderTitle(), /* @__PURE__ */ import_react.default.createElement("div", {
    className: (0, import_classnames.default)(bem("body"))
  }, /* @__PURE__ */ import_react.default.createElement("div", {
    className: (0, import_classnames.default)(bem("keys"))
  }, renderKeys()), renderSidebar())));
};
NumberKeyboard.defaultProps = {
  theme: "default",
  visible: false,
  extraKey: "",
  showDeleteKey: true,
  zIndex: 100,
  closeButtonLoading: false,
  hideOnClickOutside: true,
  randomKeyOrder: false,
  safeAreaInsetBottom: true,
  blurOnClose: true
};
var stdin_default = NumberKeyboard;
