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/pisellNumberSelector/index.tsx
var pisellNumberSelector_exports = {};
__export(pisellNumberSelector_exports, {
  default: () => pisellNumberSelector_default
});
module.exports = __toCommonJS(pisellNumberSelector_exports);
var import_react = __toESM(require("react"));
var import_antd = require("antd");
var import_Delete = __toESM(require("@pisell/icon/es/Delete"));
var import_Plus = __toESM(require("@pisell/icon/es/Plus"));
var import_Minus = __toESM(require("@pisell/icon/es/Minus"));
var import_numberKeyboard = __toESM(require("../numberKeyboard"));
var import_classnames = __toESM(require("classnames"));
var import_utils = require("@pisell/utils");
var import_index = require("./index.less");
var PisellNumberSelector = ({
  value = 0,
  min = 0,
  max = 999,
  step = 1,
  width = "112px",
  styleType = "capsule",
  disabled = false,
  onChange,
  size = "middle",
  className,
  enablePriceAdjust = false,
  originValue = 0
}) => {
  const [open, setOpen] = (0, import_react.useState)(false);
  const [inputValue, setInputValue] = (0, import_react.useState)(value.toString());
  const [tempValue, setTempValue] = (0, import_react.useState)(value.toString());
  const KeyboardItems = (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: "delete",
        label: /* @__PURE__ */ import_react.default.createElement(import_Delete.default, { className: "virtual-keyboard-input-delete-icon" }),
        disabled: false,
        type: "text"
      },
      {
        value: 0,
        label: "0",
        disabled: false,
        type: "text"
      },
      {
        value: "verify",
        label: import_utils.locales.getText("confirm") || "确认",
        disabled: false,
        type: "primary"
      }
    ];
  }, []);
  const handleDecrease = (e) => {
    if (disabled) return;
    e.stopPropagation();
    const newValue = Math.max(min, value - step);
    onChange == null ? void 0 : onChange(newValue);
  };
  const handleIncrease = (e) => {
    if (disabled) return;
    e.stopPropagation();
    const newValue = Math.min(max, value + step);
    onChange == null ? void 0 : onChange(newValue);
  };
  const handleChange = (e) => {
    if (!e) {
      setTempValue(min.toString());
      return;
    }
    if (e === "delete") {
      const newTempValue = tempValue.slice(0, -1);
      setTempValue(newTempValue || "0");
      return;
    } else if (e === "verify") {
      const newValue = parseInt(tempValue) || 0;
      const clampedValue = Math.max(min, Math.min(max, newValue));
      onChange == null ? void 0 : onChange(clampedValue);
      setOpen(false);
      return;
    } else {
      setTempValue(e);
    }
  };
  const adjustPriceContent = (0, import_react.useMemo)(() => {
    if (!enablePriceAdjust) return null;
    return /* @__PURE__ */ import_react.default.createElement(
      import_numberKeyboard.default,
      {
        value: tempValue,
        max,
        onChange: handleChange,
        onOk: () => {
          const newValue = parseInt(tempValue) || 0;
          const clampedValue = Math.max(min, Math.min(max, newValue));
          onChange == null ? void 0 : onChange(clampedValue);
          setOpen(false);
        }
      }
    );
  }, [enablePriceAdjust, tempValue, max, handleChange]);
  const handleNumberClick = () => {
    if (disabled) return;
    if (enablePriceAdjust) {
      setTempValue(value.toString());
    }
  };
  const handleOpenChange = (visible) => {
    setOpen(visible);
    if (visible) {
      setTempValue(value.toString());
    } else {
      const tempNumber = parseInt(tempValue) || 0;
      const clampedValue = Math.max(min, Math.min(max, tempNumber));
      if (clampedValue !== value) {
        onChange == null ? void 0 : onChange(clampedValue);
      }
      setTempValue(value.toString());
    }
  };
  const numberDisplayElement = enablePriceAdjust ? /* @__PURE__ */ import_react.default.createElement(
    "button",
    {
      type: "button",
      className: (0, import_classnames.default)("pisell-number-selector-number", {
        "pisell-number-selector-number-clickable": enablePriceAdjust && !disabled
      }),
      onClick: handleNumberClick,
      disabled
    },
    value
  ) : /* @__PURE__ */ import_react.default.createElement(
    "div",
    {
      className: (0, import_classnames.default)("pisell-number-selector-number", {
        "pisell-number-selector-number-clickable": enablePriceAdjust && !disabled
      })
    },
    value
  );
  const numberElement = enablePriceAdjust ? /* @__PURE__ */ import_react.default.createElement(
    import_antd.Popover,
    {
      open,
      content: adjustPriceContent,
      trigger: ["click"],
      overlayInnerStyle: { padding: 0 },
      destroyTooltipOnHide: true,
      arrow: false,
      align: {
        offset: [0, 0]
      },
      overlayClassName: "pisell-number-selector-popover",
      onOpenChange: handleOpenChange
    },
    numberDisplayElement
  ) : numberDisplayElement;
  return /* @__PURE__ */ import_react.default.createElement(
    "div",
    {
      className: (0, import_classnames.default)("pisell-number-selector-new", className, {
        [`pisell-number-selector-${size}`]: size,
        "pisell-number-selector-disabled": disabled,
        "pisell-number-selector-capsule": styleType === "capsule",
        "pisell-number-selector-separation": styleType === "separation"
      }),
      style: { width }
    },
    /* @__PURE__ */ import_react.default.createElement(
      "button",
      {
        type: "button",
        className: "pisell-number-selector-btn pisell-number-selector-minus",
        onClick: handleDecrease,
        disabled: disabled || value <= min
      },
      /* @__PURE__ */ import_react.default.createElement(import_Minus.default, { size: 16 })
    ),
    numberElement,
    /* @__PURE__ */ import_react.default.createElement(
      "button",
      {
        type: "button",
        className: "pisell-number-selector-btn pisell-number-selector-plus",
        onClick: handleIncrease,
        disabled: disabled || value >= max
      },
      /* @__PURE__ */ import_react.default.createElement(import_Plus.default, { size: 16 })
    )
  );
};
var pisellNumberSelector_default = PisellNumberSelector;
