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(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/pro/keyboards/phoneKeyboard/index.tsx
var phoneKeyboard_exports = {};
__export(phoneKeyboard_exports, {
  PhoneKeyboardBase: () => PhoneKeyboardBase,
  default: () => phoneKeyboard_default
});
module.exports = __toCommonJS(phoneKeyboard_exports);
var import_react = __toESM(require("react"));
var import_classnames = __toESM(require("classnames"));
var import_antd = require("antd");
var import_ahooks = require("ahooks");
var import_materials = require("@pisell/materials");
var import_utils = require("@pisell/utils");
var import_useEngineContext = __toESM(require("../../../hooks/useEngineContext"));
var import_locales = __toESM(require("../locales"));
var import_utils2 = require("./utils");
var import_index = require("./index.less");
var generateSimpleUUID = () => {
  return Math.random().toString(36).substring(2) + Date.now().toString(36);
};
var normalizePhoneValue = (value) => `${value || ""}`.replace(/\D/g, "");
var PhoneKeyboardBase = (props) => {
  var _a, _b;
  const {
    countryCallingCode = "61",
    placeholder = "",
    maxLength,
    disabled = false,
    doneText,
    clearText,
    label,
    formatSeparator = "space",
    style,
    className,
    keydown = true,
    inputStyle,
    inputWrapperStyle,
    onEnter
  } = props;
  const context = (0, import_useEngineContext.default)();
  import_utils.locales.init(import_locales.default, ((_b = (_a = context == null ? void 0 : context.engine) == null ? void 0 : _a.props) == null ? void 0 : _b.locale) || "en");
  const [internalValue, setInternalValue] = (0, import_ahooks.useControllableValue)(
    props,
    {
      defaultValue: ""
    }
  );
  const uniqueKeyboardId = (0, import_react.useMemo)(() => {
    return `phone-keyboard-${generateSimpleUUID()}`;
  }, []);
  const handleValueChange = (0, import_react.useCallback)(
    (newValue) => {
      const normalizedValue = normalizePhoneValue(newValue);
      setInternalValue(normalizedValue);
    },
    [setInternalValue]
  );
  const handleDelete = (0, import_react.useCallback)(() => {
    if (internalValue.length > 0) {
      handleValueChange(internalValue.slice(0, -1));
    }
  }, [internalValue, handleValueChange]);
  const handleClear = (0, import_react.useCallback)(() => {
    handleValueChange("");
  }, [handleValueChange]);
  const handleDone = (0, import_react.useCallback)(() => {
    onEnter == null ? void 0 : onEnter(internalValue);
  }, [internalValue, onEnter]);
  const handleNumberInput = (0, import_react.useCallback)(
    (num) => {
      const newValue = internalValue + num;
      if (maxLength && newValue.length > maxLength) {
        return;
      }
      handleValueChange(newValue);
    },
    [internalValue, maxLength, handleValueChange]
  );
  const handleKeyboardChange = (0, import_react.useCallback)(
    (key) => {
      if (key === void 0) return;
      const keyStr = String(key);
      switch (keyStr) {
        case "delete":
        case "backspace":
          handleDelete();
          break;
        case "clear":
          handleClear();
          break;
        case "done":
        case "ok":
          handleDone();
          break;
        default:
          if (/^[0-9]$/.test(keyStr)) {
            handleNumberInput(keyStr);
          }
          break;
      }
    },
    [handleDelete, handleClear, handleDone, handleNumberInput]
  );
  const phoneKeyboardItems = (0, import_react.useMemo)(() => {
    return [
      { value: 1, label: "1", disabled: false, type: "text" },
      { value: 2, label: "2", disabled: false, type: "text" },
      { value: 3, label: "3", disabled: false, type: "text" },
      { value: 4, label: "4", disabled: false, type: "text" },
      { value: 5, label: "5", disabled: false, type: "text" },
      { value: 6, label: "6", disabled: false, type: "text" },
      { value: 7, label: "7", disabled: false, type: "text" },
      { value: 8, label: "8", disabled: false, type: "text" },
      { value: 9, label: "9", disabled: false, type: "text" },
      {
        value: "clear",
        label: clearText || import_utils.locales.getText("pisell2.keyboard.number-keybard.clear"),
        disabled: false,
        type: "text",
        style: { color: "#F04438", fontSize: 18 }
      },
      { value: 0, label: "0", disabled: false, type: "text" },
      {
        value: "done",
        label: doneText || import_utils.locales.getText("pisell2.keyboard.phone-keyboard.confirm"),
        disabled: false,
        type: "primary",
        style: { fontSize: 18 }
      }
    ];
  }, [clearText, doneText]);
  const displayValue = (0, import_react.useMemo)(() => {
    return (0, import_utils2.formatPhoneDisplay)(
      internalValue,
      countryCallingCode,
      formatSeparator
    );
  }, [internalValue, countryCallingCode, formatSeparator]);
  return /* @__PURE__ */ import_react.default.createElement(
    "div",
    {
      className: (0, import_classnames.default)(
        "pisell-phone-keyboard",
        {
          "pisell-phone-keyboard--disabled": disabled
        },
        className
      ),
      style
    },
    label && /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-phone-keyboard-label" }, label),
    /* @__PURE__ */ import_react.default.createElement("div", { className: (0, import_classnames.default)("pisell-phone-keyboard-input-wrapper") }, /* @__PURE__ */ import_react.default.createElement(
      import_materials.VirtualInput,
      {
        focused: !disabled,
        value: displayValue,
        placeholder,
        wrapperStyle: {
          flex: 1,
          padding: "19px 16px",
          borderRadius: "8px",
          backgroundColor: "white",
          boxShadow: "0px 1px 2px rgba(16, 24, 40, 0.06)",
          overflow: "hidden",
          ...inputWrapperStyle
        },
        style: {
          fontSize: "48px",
          fontWeight: "bold",
          border: "none",
          background: "transparent",
          width: "100%",
          ...inputStyle
        },
        preventBlurElements: [`#${uniqueKeyboardId}`],
        textAlign: "center",
        allowClear: true,
        onClear: handleDelete
      }
    )),
    /* @__PURE__ */ import_react.default.createElement(
      import_materials.Keyboard,
      {
        id: uniqueKeyboardId,
        className: "pisell-phone-keyboard-keys",
        items: phoneKeyboardItems,
        keydown,
        onChange: handleKeyboardChange
      }
    )
  );
};
var PhoneKeyboard = (props) => {
  const { renderType, containerProps, children, ...keyboardProps } = props;
  const { open, className, ...restProps } = containerProps || {};
  if (renderType === "popover") {
    return /* @__PURE__ */ import_react.default.createElement(
      import_antd.Popover,
      {
        key: `${open}`,
        open,
        trigger: ["click"],
        overlayClassName: (0, import_classnames.default)(
          "pisell-phone-keyboard-popover",
          className
        ),
        destroyTooltipOnHide: true,
        overlayInnerStyle: {
          padding: 0
        },
        arrow: false,
        ...restProps,
        content: /* @__PURE__ */ import_react.default.createElement(PhoneKeyboardBase, { ...keyboardProps })
      },
      children
    );
  }
  if (renderType === "modal") {
    return open ? /* @__PURE__ */ import_react.default.createElement(
      import_materials.PisellModal,
      {
        platform: "pc",
        open: true,
        width: "unset",
        header: null,
        footer: false,
        mask: false,
        maskClosable: true,
        bodyStyle: { padding: "0px", backgroundColor: "transparent" },
        closeIcon: null,
        className: (0, import_classnames.default)("pisell-phone-keyboard-modal", className),
        destroyOnClose: true,
        ...restProps
      },
      /* @__PURE__ */ import_react.default.createElement(PhoneKeyboardBase, { ...keyboardProps })
    ) : null;
  }
  return /* @__PURE__ */ import_react.default.createElement(PhoneKeyboardBase, { ...keyboardProps });
};
var phoneKeyboard_default = PhoneKeyboard;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  PhoneKeyboardBase
});
