require("../../../_virtual/_rolldown/runtime.js");
let react = require("react");
//#region src/components/checkout/hooks/usePaymentModalBaseSales.ts
function toOrderInfo(snapshot, amountSnapshot) {
	var _tempOrder$_extend, _ref, _ref2, _tempOrder$customer_i, _snapshot$customer, _snapshot$customer2, _snapshot$tempOrder, _snapshot$customer3, _snapshot$customer4;
	if (!snapshot && !amountSnapshot) return null;
	const identity = (snapshot === null || snapshot === void 0 ? void 0 : snapshot.identity) || {};
	const amount = amountSnapshot || (snapshot === null || snapshot === void 0 ? void 0 : snapshot.amount) || {};
	const tempOrder = (snapshot === null || snapshot === void 0 ? void 0 : snapshot.tempOrder) || {};
	const customerSnapshot = (tempOrder === null || tempOrder === void 0 || (_tempOrder$_extend = tempOrder._extend) === null || _tempOrder$_extend === void 0 ? void 0 : _tempOrder$_extend.customerSnapshot) || (tempOrder === null || tempOrder === void 0 ? void 0 : tempOrder.customer) || (snapshot === null || snapshot === void 0 ? void 0 : snapshot.customer) || null;
	const customerId = (_ref = (_ref2 = (_tempOrder$customer_i = tempOrder.customer_id) !== null && _tempOrder$customer_i !== void 0 ? _tempOrder$customer_i : customerSnapshot === null || customerSnapshot === void 0 ? void 0 : customerSnapshot.id) !== null && _ref2 !== void 0 ? _ref2 : customerSnapshot === null || customerSnapshot === void 0 ? void 0 : customerSnapshot.customer_id) !== null && _ref !== void 0 ? _ref : snapshot === null || snapshot === void 0 || (_snapshot$customer = snapshot.customer) === null || _snapshot$customer === void 0 ? void 0 : _snapshot$customer.customerId;
	const customerName = (customerSnapshot === null || customerSnapshot === void 0 ? void 0 : customerSnapshot.display_name) || (customerSnapshot === null || customerSnapshot === void 0 ? void 0 : customerSnapshot.name) || tempOrder.customer_name || (snapshot === null || snapshot === void 0 || (_snapshot$customer2 = snapshot.customer) === null || _snapshot$customer2 === void 0 ? void 0 : _snapshot$customer2.customerName) || "";
	return {
		orderId: identity.orderId || identity.externalSaleNumber || identity.orderNumber,
		status: snapshot === null || snapshot === void 0 ? void 0 : snapshot.status,
		totalAmount: amount.totalAmount || amount.expectAmount || "0",
		remainingAmount: amount.amountGap || amount.totalAmount || amount.expectAmount || "0",
		paymentStatus: snapshot === null || snapshot === void 0 ? void 0 : snapshot.paymentStatus,
		isDeposit: !!amount.isDeposit,
		depositAmount: amount.depositAmount || "0",
		itemCount: (snapshot === null || snapshot === void 0 ? void 0 : snapshot.productCount) || (snapshot === null || snapshot === void 0 || (_snapshot$tempOrder = snapshot.tempOrder) === null || _snapshot$tempOrder === void 0 || (_snapshot$tempOrder = _snapshot$tempOrder.products) === null || _snapshot$tempOrder === void 0 ? void 0 : _snapshot$tempOrder.length) || 0,
		hasLocalOrderData: !!(snapshot === null || snapshot === void 0 ? void 0 : snapshot.tempOrder),
		customerId,
		customerName,
		customerPhone: (customerSnapshot === null || customerSnapshot === void 0 ? void 0 : customerSnapshot.phone) || tempOrder.phone || (snapshot === null || snapshot === void 0 || (_snapshot$customer3 = snapshot.customer) === null || _snapshot$customer3 === void 0 ? void 0 : _snapshot$customer3.phone) || "",
		customerEmail: (customerSnapshot === null || customerSnapshot === void 0 ? void 0 : customerSnapshot.email) || tempOrder.email || (snapshot === null || snapshot === void 0 || (_snapshot$customer4 = snapshot.customer) === null || _snapshot$customer4 === void 0 ? void 0 : _snapshot$customer4.email) || "",
		customerSnapshot
	};
}
function readSafely(reader, fallback) {
	try {
		return {
			value: reader(),
			error: null
		};
	} catch (error) {
		return {
			value: fallback,
			error
		};
	}
}
function usePaymentModalBaseSales({ pisellos, baseSalesModuleName }) {
	const [version, setVersion] = (0, react.useState)(0);
	const baseSales = (0, react.useMemo)(() => {
		var _pisellos$getModule;
		if (!baseSalesModuleName) return null;
		return (pisellos === null || pisellos === void 0 || (_pisellos$getModule = pisellos.getModule) === null || _pisellos$getModule === void 0 ? void 0 : _pisellos$getModule.call(pisellos, baseSalesModuleName)) || null;
	}, [
		baseSalesModuleName,
		pisellos,
		version
	]);
	const orderSnapshotResult = (0, react.useMemo)(() => {
		return readSafely(() => {
			var _baseSales$getOrderSn;
			return (baseSales === null || baseSales === void 0 || (_baseSales$getOrderSn = baseSales.getOrderSnapshot) === null || _baseSales$getOrderSn === void 0 ? void 0 : _baseSales$getOrderSn.call(baseSales)) || null;
		}, null);
	}, [baseSales, version]);
	const orderSnapshot = orderSnapshotResult.value;
	const amountSnapshotResult = (0, react.useMemo)(() => {
		return readSafely(() => {
			var _baseSales$getOrderAm;
			return (baseSales === null || baseSales === void 0 || (_baseSales$getOrderAm = baseSales.getOrderAmountSnapshot) === null || _baseSales$getOrderAm === void 0 ? void 0 : _baseSales$getOrderAm.call(baseSales)) || (orderSnapshot === null || orderSnapshot === void 0 ? void 0 : orderSnapshot.amount) || null;
		}, null);
	}, [
		baseSales,
		orderSnapshot,
		version
	]);
	const amountSnapshot = amountSnapshotResult.value;
	const paymentItemsResult = (0, react.useMemo)(() => {
		return readSafely(() => {
			var _baseSales$getOrderPa, _orderSnapshot$tempOr;
			return (baseSales === null || baseSales === void 0 || (_baseSales$getOrderPa = baseSales.getOrderPayments) === null || _baseSales$getOrderPa === void 0 ? void 0 : _baseSales$getOrderPa.call(baseSales)) || (orderSnapshot === null || orderSnapshot === void 0 || (_orderSnapshot$tempOr = orderSnapshot.tempOrder) === null || _orderSnapshot$tempOr === void 0 ? void 0 : _orderSnapshot$tempOr.payments) || [];
		}, []);
	}, [
		baseSales,
		orderSnapshot,
		version
	]);
	const paymentItems = paymentItemsResult.value;
	const refresh = (0, react.useCallback)(() => {
		setVersion((current) => current + 1);
	}, []);
	const error = orderSnapshotResult.error || amountSnapshotResult.error || paymentItemsResult.error || (baseSalesModuleName && !baseSales ? /* @__PURE__ */ new Error(`BaseSales module not found: ${baseSalesModuleName}`) : null);
	return {
		baseSales,
		orderSnapshot,
		amountSnapshot,
		paymentItems,
		orderInfo: (0, react.useMemo)(() => toOrderInfo(orderSnapshot, amountSnapshot), [orderSnapshot, amountSnapshot]),
		error,
		refresh
	};
}
//#endregion
exports.usePaymentModalBaseSales = usePaymentModalBaseSales;
