import { _objectSpread2 } from "../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import useEngineContext from "../../hooks/useEngineContext.js";
import { buildEmptyQuantities, getEffectiveDenominationsForCurrency } from "./utils.js";
import { useCallback, useMemo, useRef, useState } from "react";
import { message } from "antd";
import { locales } from "@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 = useEngineContext().appHelper) === null || _context$appHelper === void 0 ? void 0 : _context$appHelper.utils) || {};
	const modalRef = useRef(null);
	const triggerRef = useRef(null);
	const [keyboardOpen, setKeyboardOpen] = useState(false);
	const [internalValue, setInternalValue] = useState(defaultValue);
	const [confirmedQuantities, setConfirmedQuantities] = useState({});
	const countedResultRef = useRef(null);
	const manualInputValueRef = useRef(null);
	const [manualCountMismatchOpen, setManualCountMismatchOpen] = useState(false);
	const globalConfig = 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 = 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 = useMemo(() => {
		return getEffectiveDenominationsForCurrency(denominations, _currencyCode);
	}, [_currencyCode, denominations]);
	const decimalPlaces = 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 = 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 = useMemo(() => String(value !== null && value !== void 0 ? value : internalValue), [value, internalValue]);
	const updateAmount = useCallback((nextValue, source) => {
		if (value === void 0) setInternalValue(nextValue);
		if (typeof onChange === "function") onChange(nextValue, { source });
	}, [
		decimalPlaces,
		onChange,
		value
	]);
	/**
	* @description: 价格键盘输入
	* @return {*}
	*/
	const handleAmountChange = useCallback((nextValue) => {
		manualInputValueRef.current = nextValue;
		updateAmount(String(nextValue), "manual");
	}, [updateAmount]);
	/**
	* @description: 价格键盘关闭
	* @return {*}
	*/
	const handleKeyboardClose = 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 = 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 = 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: useCallback(() => {
			var _modalRef$current;
			if (disabled || countDisabled) return;
			if (currencyDenominations.length === 0) {
				message.warning(locales.getText("pisell-cash-counting.no-country-config"));
				return;
			}
			const initialQuantities = _objectSpread2(_objectSpread2({}, buildEmptyQuantities(currencyDenominations)), confirmedQuantities);
			setKeyboardOpen(false);
			(_modalRef$current = modalRef.current) === null || _modalRef$current === void 0 || _modalRef$current.open({ initialQuantities });
		}, [
			confirmedQuantities,
			countDisabled,
			currencyDenominations,
			disabled
		]),
		handleCountCancel: useCallback(() => {
			onCountCancel === null || onCountCancel === void 0 || onCountCancel();
		}, [onCountCancel]),
		handleCountConfirm: useCallback((payload) => {
			countedResultRef.current = payload;
			setConfirmedQuantities(payload.quantities);
			updateAmount(payload.amount, "count");
			if (typeof onCountConfirm === "function") onCountConfirm(payload);
		}, [onCountConfirm, updateAmount]),
		manualCountMismatchOpen,
		confirmUseCountedTotal,
		confirmKeepManualAmount
	};
}
//#endregion
export { usePisellCashCounting };
