const require_runtime = require("../../../../_virtual/_rolldown/runtime.js");
const require_utils = require("./utils.js");
const require_useWalletPass = require("../../hooks/useWalletPass.js");
const require_index = require("./components/WalletPassHeader/index.js");
const require_index$1 = require("./components/WalletPassCardDisplay/index.js");
const require_index$2 = require("./components/WalletModal/index.js");
const require_index$3 = require("./components/DiscountWallet/index.js");
const require_constants = require("../../constants.js");
let react = require("react");
react = require_runtime.__toESM(react);
let _pisell_utils = require("@pisell/utils");
let _pisell_materials = require("@pisell/materials");
require("./index.less");
//#region src/components/checkout/components/WalletPassModule/index.tsx
const WalletPassModule = (0, react.forwardRef)((props, ref) => {
	var _locales$getText;
	const { orderInfo, onSelectChange, customAmount, paymentItems, clientId } = props;
	const { handleSelectWallet, cardData, selectedWalletIds, expiredWalletIds, expiredWalletData, disabledWalletData, clearAllSelectedWallet, clearDisabledWalletData, handleBestSaving, loading, handleSearchIdentificationCode, availableDiscountList, selectedDiscountList, handleSelectDiscount, customerName, handleConfirmEditAmount, handleBeforeEditAmount } = require_useWalletPass.useWalletPass({
		onSelectChange,
		customAmount,
		totalAmount: Number(orderInfo === null || orderInfo === void 0 ? void 0 : orderInfo.totalAmount) || 0,
		clientId
	});
	const [expiredModalVisible, setExpiredModalVisible] = (0, react.useState)(false);
	const [disabledModalVisible, setDisabledModalVisible] = (0, react.useState)(false);
	const [isCardMode, setIsCardMode] = (0, react.useState)(true);
	const handleExpiredClick = () => {
		setExpiredModalVisible(true);
	};
	const handleExpiredModalClose = () => {
		setExpiredModalVisible(false);
	};
	const handleDisabledModalClose = () => {
		setDisabledModalVisible(false);
		clearDisabledWalletData();
	};
	const toggleDisplayMode = () => {
		setIsCardMode(!isCardMode);
	};
	const handleDetail = (record, index) => {};
	const formattedDisabledWalletData = react.default.useMemo(() => {
		return require_utils.formatMachineCodeServer2CardList(disabledWalletData || [], [], {
			error: true,
			showStatus: false
		});
	}, [disabledWalletData]);
	(0, react.useEffect)(() => {
		if (disabledWalletData && disabledWalletData.length > 0) {
			if (!disabledModalVisible) setDisabledModalVisible(true);
		}
	}, [disabledWalletData, disabledModalVisible]);
	(0, react.useImperativeHandle)(ref, () => ({
		handleSearchIdentificationCode,
		getWalletPassData: (select) => ({
			selectedWallet: select.length ? cardData.filter((card) => selectedWalletIds.includes(card.id)) : [],
			availableDiscountList,
			selectedDiscountList
		})
	}));
	const disabledTitle = (0, react.useMemo)(() => {
		if (disabledWalletData && disabledWalletData.length === 1) {
			var _disabledWalletData$;
			return ((_disabledWalletData$ = disabledWalletData[0]) === null || _disabledWalletData$ === void 0 || (_disabledWalletData$ = _disabledWalletData$.unified_messages) === null || _disabledWalletData$ === void 0 ? void 0 : _disabledWalletData$.title) || "";
		}
		return _pisell_utils.locales.getText("wallet-pass-module-disabled-modal-title");
	}, [disabledWalletData]);
	const selectedCount = (0, react.useMemo)(() => {
		return selectedWalletIds.length + selectedDiscountList.length;
	}, [selectedWalletIds.length, selectedDiscountList.length]);
	const discountDisabled = (0, react.useMemo)(() => {
		if (Number(orderInfo === null || orderInfo === void 0 ? void 0 : orderInfo.totalAmount) === 0) return true;
		return !!(paymentItems === null || paymentItems === void 0 ? void 0 : paymentItems.some((item) => {
			return !require_constants.WALLET_PASS_CODE_ARRAY.includes(item.code) && !item.isSynced;
		}));
	}, [paymentItems, orderInfo]);
	if (cardData.length === 0 && availableDiscountList.length === 0 && disabledWalletData.length === 0) return null;
	return /* @__PURE__ */ react.default.createElement("div", { className: "wallet-pass-module-container" }, /* @__PURE__ */ react.default.createElement(_pisell_materials.PisellLoading, {
		delay: 300,
		loading,
		fullscreen: false,
		mask: {
			backgroundColor: "rgba(255, 255, 255, 0.5)",
			width: "100%",
			height: "100%"
		}
	}, /* @__PURE__ */ react.default.createElement("div", { className: "wallet-pass-module" }, /* @__PURE__ */ react.default.createElement(require_index.default, {
		customerName,
		selectedCount,
		expiredCount: expiredWalletIds.length,
		isCardMode,
		onExpiredClick: handleExpiredClick,
		onBestSaving: handleBestSaving,
		onClearAll: clearAllSelectedWallet,
		onToggleDisplayMode: toggleDisplayMode
	}), /* @__PURE__ */ react.default.createElement(require_index$1.default, {
		isCardMode,
		cardData,
		selectedWalletIds,
		onSelectWallet: handleSelectWallet,
		onDetail: handleDetail,
		onConfirmEditAmount: handleConfirmEditAmount,
		onBeforeEditAmount: handleBeforeEditAmount
	}), /* @__PURE__ */ react.default.createElement(require_index$3.default, {
		isCardMode,
		cardData: availableDiscountList,
		selectedWalletIds: selectedDiscountList,
		onSelectWallet: handleSelectDiscount,
		onDetail: handleDetail,
		discountDisabled
	}), /* @__PURE__ */ react.default.createElement(require_index$2.default, {
		visible: expiredModalVisible,
		walletData: expiredWalletData,
		onClose: handleExpiredModalClose,
		title: (_locales$getText = _pisell_utils.locales.getText("wallet-pass-module-expired-modal-title")) === null || _locales$getText === void 0 ? void 0 : _locales$getText((expiredWalletData === null || expiredWalletData === void 0 ? void 0 : expiredWalletData.length) || 0)
	}), /* @__PURE__ */ react.default.createElement(require_index$2.default, {
		visible: disabledModalVisible,
		walletData: formattedDisabledWalletData,
		onClose: handleDisabledModalClose,
		title: disabledTitle
	}))));
});
//#endregion
exports.default = WalletPassModule;
