require("../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_useEngineContext = require("../../hooks/useEngineContext.js");
const require_utils = require("./utils.js");
let react = require("react");
let antd = require("antd");
let _pisell_utils = require("@pisell/utils");
//#region src/components/pisellCashCounting/hooks.ts
function usePisellCashCounting({ value, defaultValue = "", currencyCode, currencySymbol, amountPrecision, denominations = [], disabled = false, countDisabled = false, onChange, onCountConfirm, onCountCancel }) {
	var _context$appHelper, _store$getState2, _currencyDenomination3;
	const { store } = ((_context$appHelper = require_useEngineContext.default().appHelper) === null || _context$appHelper === void 0 ? void 0 : _context$appHelper.utils) || {};
	const modalRef = (0, react.useRef)(null);
	const triggerRef = (0, react.useRef)(null);
	const [keyboardOpen, setKeyboardOpen] = (0, react.useState)(false);
	const [internalValue, setInternalValue] = (0, react.useState)(defaultValue);
	const [confirmedQuantities, setConfirmedQuantities] = (0, react.useState)({});
	const countedResultRef = (0, react.useRef)(null);
	const manualInputValueRef = (0, react.useRef)(null);
	const [manualCountMismatchOpen, setManualCountMismatchOpen] = (0, react.useState)(false);
	const globalConfig = (0, react.useMemo)(() => {
		var _store$getState;
		const global = (store === null || store === void 0 || (_store$getState = store.getState) === null || _store$getState === void 0 || (_store$getState = _store$getState.call(store)) === null || _store$getState === void 0 ? void 0 : _store$getState.global) || {};
		return {
			symbol: (global === null || global === void 0 ? void 0 : global.symbol) || "$",
			currency_code: (global === null || global === void 0 ? void 0 : global.currency_code) || "AUD"
		};
	}, [store === null || store === void 0 || (_store$getState2 = store.getState) === null || _store$getState2 === void 0 || (_store$getState2 = _store$getState2.call(store)) === null || _store$getState2 === void 0 ? void 0 : _store$getState2.global]);
	const _currencyCode = (0, react.useMemo)(() => {
		return currencyCode || (globalConfig === null || globalConfig === void 0 ? void 0 : globalConfig.currency_code);
	}, [currencyCode, globalConfig === null || globalConfig === void 0 ? void 0 : globalConfig.currency_code]);
	const currencyDenominations = (0, react.useMemo)(() => {
		return require_utils.getEffectiveDenominationsForCurrency(denominations, _currencyCode);
	}, [_currencyCode, denominations]);
	const decimalPlaces = (0, react.useMemo)(() => {
		var _currencyDenomination, _currencyDenomination2;
		if (typeof amountPrecision === "number") return amountPrecision;
		return (_currencyDenomination = (_currencyDenomination2 = currencyDenominations[0]) === null || _currencyDenomination2 === void 0 ? void 0 : _currencyDenomination2.decimal_places) !== null && _currencyDenomination !== void 0 ? _currencyDenomination : 2;
	}, [amountPrecision, (_currencyDenomination3 = currencyDenominations[0]) === null || _currencyDenomination3 === void 0 ? void 0 : _currencyDenomination3.decimal_places]);
	const symbolValue = (0, react.useMemo)(() => {
		if (currencySymbol) return currencySymbol;
		return globalConfig === null || globalConfig === void 0 ? void 0 : globalConfig.symbol;
	}, [currencySymbol, globalConfig === null || globalConfig === void 0 ? void 0 : globalConfig.symbol]);
	const currentValue = (0, react.useMemo)(() => String(value !== null && value !== void 0 ? value : internalValue), [value, internalValue]);
	const updateAmount = (0, react.useCallback)((nextValue, source) => {
		if (value === void 0) setInternalValue(nextValue);
		if (typeof onChange === "function") onChange(nextValue, { source });
	}, [
		decimalPlaces,
		onChange,
		value
	]);
	/**
	* @description: 价格键盘输入
	* @return {*}
	*/
	const handleAmountChange = (0, react.useCallback)((nextValue) => {
		manualInputValueRef.current = nextValue;
		updateAmount(String(nextValue), "manual");
	}, [updateAmount]);
	/**
	* @description: 价格键盘关闭
	* @return {*}
	*/
	const handleKeyboardClose = (0, react.useCallback)(() => {
		var _countedResultRef$cur;
		setKeyboardOpen(false);
		if (((_countedResultRef$cur = countedResultRef.current) === null || _countedResultRef$cur === void 0 ? void 0 : _countedResultRef$cur.amount) && Number(countedResultRef.current.amount) !== Number(manualInputValueRef.current)) setManualCountMismatchOpen(true);
	}, [setManualCountMismatchOpen]);
	/**
	* @description: 使用数钱金额
	* @param {*} useCallback
	* @return {*}
	*/
	const confirmUseCountedTotal = (0, react.useCallback)(() => {
		var _countedResultRef$cur2, _countedResultRef$cur3;
		updateAmount((_countedResultRef$cur2 = (_countedResultRef$cur3 = countedResultRef.current) === null || _countedResultRef$cur3 === void 0 ? void 0 : _countedResultRef$cur3.amount) !== null && _countedResultRef$cur2 !== void 0 ? _countedResultRef$cur2 : "", "count");
		setManualCountMismatchOpen(false);
	}, [updateAmount]);
	/**
	* @description: 保留手动金额
	* @param {*} useCallback
	* @return {*}
	*/
	const confirmKeepManualAmount = (0, react.useCallback)(() => {
		var _manualInputValueRef$;
		countedResultRef.current = null;
		setConfirmedQuantities({});
		updateAmount((_manualInputValueRef$ = manualInputValueRef.current) !== null && _manualInputValueRef$ !== void 0 ? _manualInputValueRef$ : "", "manual");
		setManualCountMismatchOpen(false);
	}, [updateAmount]);
	return {
		modalRef,
		triggerRef,
		keyboardOpen,
		setKeyboardOpen,
		currencyDenominations,
		decimalPlaces,
		symbol: symbolValue,
		currencyCode: _currencyCode,
		currentValue,
		handleAmountChange,
		handleKeyboardClose,
		openCountModal: (0, react.useCallback)(() => {
			var _modalRef$current;
			if (disabled || countDisabled) return;
			if (currencyDenominations.length === 0) {
				antd.message.warning(_pisell_utils.locales.getText("pisell-cash-counting.no-country-config"));
				return;
			}
			const initialQuantities = require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, require_utils.buildEmptyQuantities(currencyDenominations)), confirmedQuantities);
			setKeyboardOpen(false);
			(_modalRef$current = modalRef.current) === null || _modalRef$current === void 0 || _modalRef$current.open({ initialQuantities });
		}, [
			confirmedQuantities,
			countDisabled,
			currencyDenominations,
			disabled
		]),
		handleCountCancel: (0, react.useCallback)(() => {
			onCountCancel === null || onCountCancel === void 0 || onCountCancel();
		}, [onCountCancel]),
		handleCountConfirm: (0, react.useCallback)((payload) => {
			countedResultRef.current = payload;
			setConfirmedQuantities(payload.quantities);
			updateAmount(payload.amount, "count");
			if (typeof onCountConfirm === "function") onCountConfirm(payload);
		}, [onCountConfirm, updateAmount]),
		manualCountMismatchOpen,
		confirmUseCountedTotal,
		confirmKeepManualAmount
	};
}
//#endregion
exports.usePisellCashCounting = usePisellCashCounting;
