const require_runtime = require("../../_virtual/_rolldown/runtime.js");
const require_index = require("../numberKeyboard/index.js");
let react = require("react");
react = require_runtime.__toESM(react);
let antd = require("antd");
let _pisell_utils = require("@pisell/utils");
let classnames = require("classnames");
classnames = require_runtime.__toESM(classnames);
let _pisell_icon_es_Delete = require("@pisell/icon/es/Delete");
_pisell_icon_es_Delete = require_runtime.__toESM(_pisell_icon_es_Delete);
let _pisell_icon_es_Plus = require("@pisell/icon/es/Plus");
_pisell_icon_es_Plus = require_runtime.__toESM(_pisell_icon_es_Plus);
let _pisell_icon_es_Minus = require("@pisell/icon/es/Minus");
_pisell_icon_es_Minus = require_runtime.__toESM(_pisell_icon_es_Minus);
require("./index.less");
//#region src/pro/pisellNumberSelector/index.tsx
const 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, react.useState)(false);
	const [inputValue, setInputValue] = (0, react.useState)(value.toString());
	const [tempValue, setTempValue] = (0, react.useState)(value.toString());
	(0, 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__ */ react.default.createElement(_pisell_icon_es_Delete.default, { className: "virtual-keyboard-input-delete-icon" }),
				disabled: false,
				type: "text"
			},
			{
				value: 0,
				label: "0",
				disabled: false,
				type: "text"
			},
			{
				value: "verify",
				label: _pisell_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 || onChange === void 0 || onChange(newValue);
	};
	const handleIncrease = (e) => {
		if (disabled) return;
		e.stopPropagation();
		const newValue = Math.min(max, value + step);
		onChange === null || onChange === void 0 || onChange(newValue);
	};
	const handleChange = (e) => {
		if (!e) {
			setTempValue(min.toString());
			return;
		}
		if (e === "delete") {
			setTempValue(tempValue.slice(0, -1) || "0");
			return;
		} else if (e === "verify") {
			onChange === null || onChange === void 0 || onChange(Math.max(min, Math.min(max, parseInt(tempValue) || 0)));
			setOpen(false);
			return;
		} else setTempValue(e);
	};
	const adjustPriceContent = (0, react.useMemo)(() => {
		if (!enablePriceAdjust) return null;
		return /* @__PURE__ */ react.default.createElement(require_index.default, {
			value: tempValue,
			max,
			onChange: handleChange,
			onOk: () => {
				onChange === null || onChange === void 0 || onChange(Math.max(min, Math.min(max, parseInt(tempValue) || 0)));
				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 clampedValue = Math.max(min, Math.min(max, parseInt(tempValue) || 0));
			if (clampedValue !== value) onChange === null || onChange === void 0 || onChange(clampedValue);
			setTempValue(value.toString());
		}
	};
	const numberDisplayElement = enablePriceAdjust ? /* @__PURE__ */ react.default.createElement("button", {
		type: "button",
		className: (0, classnames.default)("pisell-number-selector-number", { "pisell-number-selector-number-clickable": enablePriceAdjust && !disabled }),
		onClick: handleNumberClick,
		disabled
	}, value) : /* @__PURE__ */ react.default.createElement("div", { className: (0, classnames.default)("pisell-number-selector-number", { "pisell-number-selector-number-clickable": enablePriceAdjust && !disabled }) }, value);
	const numberElement = enablePriceAdjust ? /* @__PURE__ */ react.default.createElement(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__ */ react.default.createElement("div", {
		className: (0, 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__ */ react.default.createElement("button", {
		type: "button",
		className: "pisell-number-selector-btn pisell-number-selector-minus",
		onClick: handleDecrease,
		disabled: disabled || value <= min
	}, /* @__PURE__ */ react.default.createElement(_pisell_icon_es_Minus.default, { size: 16 })), numberElement, /* @__PURE__ */ react.default.createElement("button", {
		type: "button",
		className: "pisell-number-selector-btn pisell-number-selector-plus",
		onClick: handleIncrease,
		disabled: disabled || value >= max
	}, /* @__PURE__ */ react.default.createElement(_pisell_icon_es_Plus.default, { size: 16 })));
};
//#endregion
exports.default = PisellNumberSelector;
