import { _asyncToGenerator } from "../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js";
import useEngineContext from "../../../../hooks/useEngineContext.js";
import CashKeyboard from "../../../../pro/priceKeyboard/components/cashKeyboard/index.js";
import locales_default from "../../locales.js";
import useToast from "../../../../hooks/useToast.js";
import React, { useEffect, useState } from "react";
import { formatAmount, locales } from "@pisell/utils";
import { useDebounceFn } from "ahooks";
import "./index.less";
//#region src/components/checkout/components/CashPaymentModule/index.tsx
function CashPaymentModule({ amountSymbol, orderInfo, onPaymentComplete, customAmount, amountButtons = [], disabled = false, roundingFunction, balanceDueAmount = "0", balanceDueTotalAmount = "0", syncingLock = false, shortcutsSize = "default" }) {
	const [amount, setAmount] = useState(customAmount || "");
	const [roundingAmount, setRoundingAmount] = useState(0);
	const context = useEngineContext();
	const Toast = useToast();
	const getText = (key, fallback) => {
		var _context$appHelper, _context$appHelper$ge, _context$appHelper$ge2, _context$appHelper$ut, _checkoutLocales;
		const checkoutText = (_checkoutLocales = locales_default[((_context$appHelper = context.appHelper) === null || _context$appHelper === void 0 || (_context$appHelper = _context$appHelper.utils) === null || _context$appHelper === void 0 || (_context$appHelper$ge = _context$appHelper.getApp) === null || _context$appHelper$ge === void 0 || (_context$appHelper$ge = _context$appHelper$ge.call(_context$appHelper)) === null || _context$appHelper$ge === void 0 || (_context$appHelper$ge = _context$appHelper$ge.locales) === null || _context$appHelper$ge === void 0 || (_context$appHelper$ge2 = _context$appHelper$ge.getLocale) === null || _context$appHelper$ge2 === void 0 ? void 0 : _context$appHelper$ge2.call(_context$appHelper$ge)) || ((_context$appHelper$ut = context.appHelper.utils) === null || _context$appHelper$ut === void 0 || (_context$appHelper$ut = _context$appHelper$ut.storage) === null || _context$appHelper$ut === void 0 ? void 0 : _context$appHelper$ut.get("umi_locale")) || "en"]) === null || _checkoutLocales === void 0 ? void 0 : _checkoutLocales[key];
		if (checkoutText) return checkoutText;
		return locales.getText(key) || fallback;
	};
	useEffect(() => {
		handleKeyboardConfirm(customAmount || "");
	}, [
		customAmount,
		balanceDueAmount,
		orderInfo === null || orderInfo === void 0 ? void 0 : orderInfo.isDeposit
	]);
	const isAmountButtonsDisabled = disabled || Number(customAmount || 0) === 0;
	const isInputDisabled = disabled || Number(customAmount || 0) === 0;
	const handleCashPayInternal = () => {
		if (isAmountButtonsDisabled) return;
		if (onPaymentComplete) {
			if (Number(amount) === 0 && Math.abs(roundingAmount) === 0) {
				Toast.fail(getText("cash-payment-amount-cannot-be-zero", "The amount cannot be 0"));
				return;
			}
			const finalAmount = typeof amount === "number" ? amount : Number(amount) || 0;
			onPaymentComplete({
				type: "CASHMANUAL",
				amount: finalAmount,
				totalAmount: Number(customAmount) || 0,
				change: finalAmount - (Number(customAmount) || 0),
				roundingAmount
			});
		}
	};
	const { run: handleCashPay } = useDebounceFn(handleCashPayInternal, {
		wait: 500,
		leading: true,
		trailing: false
	});
	const handleKeyboardConfirm = function() {
		var _ref = _asyncToGenerator(function* (value) {
			if (isInputDisabled) {
				setAmount("0.00");
				setRoundingAmount(0);
				return;
			}
			const finalValue = String(value);
			console.log("键盘确认:", finalValue);
			if (Number(finalValue) === 0) {
				Toast.fail(getText("cash-payment-amount-cannot-be-zero", "The amount cannot be 0"));
				return;
			}
			if (finalValue === "" || finalValue === null || finalValue === void 0) setAmount("");
			else {
				const cleanValue = finalValue.replace(/[$\s]/g, "");
				const numValue = parseFloat(cleanValue);
				const balanceDue = parseFloat((orderInfo === null || orderInfo === void 0 ? void 0 : orderInfo.isDeposit) ? balanceDueTotalAmount || "0" : balanceDueAmount);
				const shouldApplyRounding = Math.abs(numValue - balanceDue) < .01;
				let finalAmount = numValue;
				let roundingDiff = 0;
				if (shouldApplyRounding && roundingFunction) {
					const { roundedAmount = numValue, roundingDifference = 0 } = (yield roundingFunction(numValue)) || {};
					finalAmount = roundedAmount;
					roundingDiff = roundingDifference;
					console.log("应用抹零：", numValue, "->", roundedAmount, "差额:", roundingDifference);
				} else console.log("不应用抹零，用户输入金额:", numValue, "剩余支付金额:", balanceDue);
				setRoundingAmount(roundingDiff);
				if (isNaN(numValue)) setAmount("");
				else {
					setAmount(finalAmount.toString());
					console.log("设置金额为:", finalAmount);
				}
			}
		});
		return function handleKeyboardConfirm(_x) {
			return _ref.apply(this, arguments);
		};
	}();
	const handleReset = function() {
		var _ref2 = _asyncToGenerator(function* () {
			if (isAmountButtonsDisabled) return;
			const cleanValue = (balanceDueAmount || "0").replace(/[$\s]/g, "");
			const numValue = parseFloat(cleanValue);
			const balanceDue = parseFloat(balanceDueAmount);
			const shouldApplyRounding = Math.abs(numValue - balanceDue) < .01;
			let finalAmount = numValue;
			let roundingDiff = 0;
			if (shouldApplyRounding && roundingFunction) {
				const { roundedAmount = numValue, roundingDifference = 0 } = (yield roundingFunction(finalAmount)) || {};
				finalAmount = roundedAmount;
				roundingDiff = roundingDifference;
				console.log("应用抹零：", numValue, "->", roundedAmount, "差额:", roundingDifference);
			} else console.log("不应用抹零，用户输入金额:", numValue, "剩余支付金额:", balanceDue);
			setAmount(finalAmount.toString());
			setRoundingAmount(roundingDiff);
		});
		return function handleReset() {
			return _ref2.apply(this, arguments);
		};
	}();
	return /* @__PURE__ */ React.createElement(CashKeyboard, {
		variant: "intact",
		originalValue: customAmount,
		value: amount,
		disabled,
		hiddenChangeDue: roundingAmount > 0,
		onChange: (value) => {
			setAmount(value);
			setRoundingAmount(0);
		},
		titleArea: {
			visible: true,
			title: { text: getText("cash-payment-title", "Cash Payment") },
			subtitle: {
				visible: Math.abs(roundingAmount) > 0,
				text: `${getText("cash-payment-rounding", "Rounding")}: ${formatAmount(roundingAmount, 2, amountSymbol)}`
			}
		},
		style: {
			width: "100%",
			backgroundColor: "transparent"
		},
		presets: amountButtons.slice(0, 5).map((buttonAmount) => {
			return {
				label: formatAmount(buttonAmount, 2, amountSymbol),
				value: buttonAmount
			};
		}),
		onReset: handleReset,
		onEnter: syncingLock ? void 0 : handleCashPay,
		shortcutsSize
	});
}
//#endregion
export { CashPaymentModule as default };
