const require_runtime = require("../../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_asyncToGenerator = require("../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js");
const require_context = require("../../context.js");
let react = require("react");
react = require_runtime.__toESM(react);
let antd = require("antd");
let _pisell_utils = require("@pisell/utils");
let _pisell_materials = require("@pisell/materials");
let classnames = require("classnames");
classnames = require_runtime.__toESM(classnames);
let _pisell_icon_es_ChevronDown = require("@pisell/icon/es/ChevronDown");
_pisell_icon_es_ChevronDown = require_runtime.__toESM(_pisell_icon_es_ChevronDown);
require("../../../booking/components/voucher/index.less");
let _pisell_icon_es_UserCircle = require("@pisell/icon/es/UserCircle");
_pisell_icon_es_UserCircle = require_runtime.__toESM(_pisell_icon_es_UserCircle);
require("../../../appointmentBooking/components/Voucher/index.less");
//#region src/components/venueBooking/components/Voucher/index.tsx
/**
* venueBooking 优惠券/Promotion 组件
* 参照 appointmentBooking/Voucher 实现，适配 venueBooking 模块的 scanCode / getDiscountList / setDiscountSelected
* 包含：标题行折叠、PisellGoodPassCard 券列表、未登录登录卡片、Code 输入 + Apply
*/
/**
* VoucherCard -- 优惠券/Promotion 区块
* 嵌入购物车底部，提供券列表展示、勾选、扫码兑码、未登录引导等能力
*/
function VoucherCard() {
	const { isLogin, openLoginModal, discountList, runModuleAction, refreshViewModel, venueBookingModule } = require_context.useVenueBookingContext();
	const [showDetail, setShowDetail] = (0, react.useState)(true);
	const [code, setCode] = (0, react.useState)("");
	const availableData = (0, react.useMemo)(() => {
		return (discountList || []).filter((item) => !!item.isAvailable).map((discount) => require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, discount), {}, { isDisabled: discount.isDisabled || discount.isDisabledForProductUsed }));
	}, [discountList]);
	/** 从 OS 模块同步最新折扣列表并刷新购物车视图 */
	const syncAfterDiscountChange = (0, react.useCallback)(require_asyncToGenerator._asyncToGenerator(function* () {
		yield refreshViewModel();
	}), [refreshViewModel]);
	/** 扫码/兑码 Apply */
	const onScanCode = (0, react.useCallback)(require_asyncToGenerator._asyncToGenerator(function* () {
		if (!code.trim()) return;
		try {
			var _venueBookingModule$g;
			const tempOrder = venueBookingModule === null || venueBookingModule === void 0 || (_venueBookingModule$g = venueBookingModule.getTempOrder) === null || _venueBookingModule$g === void 0 ? void 0 : _venueBookingModule$g.call(venueBookingModule);
			const result = yield runModuleAction("scanCode", code, (tempOrder === null || tempOrder === void 0 ? void 0 : tempOrder.customer_id) || void 0);
			if (result === null || result === void 0 ? void 0 : result.isAvailable) {
				setCode("");
				yield syncAfterDiscountChange();
			} else {
				const content = { time_limit: _pisell_utils.locales.getText("pisell2.text.goodpass.code-invalid-expired") };
				antd.message.error({ content: String((result === null || result === void 0 ? void 0 : result.type) === "server" ? _pisell_utils.locales.getText("pisell2.text.goodpass.code-invalid") : content[result === null || result === void 0 ? void 0 : result.unavailableReason] || _pisell_utils.locales.getText("pisell2.text.goodpass.code-invalid-cart")) });
			}
		} catch (error) {
			console.warn("[venueBooking] scanCode failed", error);
			antd.message.error({ content: String(_pisell_utils.locales.getText("pisell2.text.goodpass.code-invalid") || "兑换码无效") });
		}
	}), [
		code,
		venueBookingModule,
		runModuleAction,
		syncAfterDiscountChange
	]);
	/** 勾选/取消勾选某张券 */
	const onDiscountChange = (0, react.useCallback)(function() {
		var _ref = require_asyncToGenerator._asyncToGenerator(function* (val) {
			try {
				yield runModuleAction("setDiscountSelected", {
					discountId: val.id,
					isSelected: val.isSelected
				});
				yield syncAfterDiscountChange();
			} catch (error) {
				console.warn("[venueBooking] setDiscountSelected failed", error);
			}
		});
		return function(_x) {
			return _ref.apply(this, arguments);
		};
	}(), [runModuleAction, syncAfterDiscountChange]);
	/** 排序：选中的券排前面 */
	const mixedSort = (0, react.useCallback)((val) => {
		return [...val].sort((a, b) => Number(b === null || b === void 0 ? void 0 : b.isSelected) - Number(a === null || a === void 0 ? void 0 : a.isSelected));
	}, []);
	/** 未登录时展示的登录引导卡片 */
	const renderLoginCard = () => {
		if (isLogin) return null;
		return /* @__PURE__ */ react.default.createElement("div", { className: "login-card" }, /* @__PURE__ */ react.default.createElement(_pisell_icon_es_UserCircle.default, { style: { fontSize: 28 } }), /* @__PURE__ */ react.default.createElement("div", {
			className: "text-wrapper",
			style: { maxWidth: "none" }
		}, /* @__PURE__ */ react.default.createElement(_pisell_materials.PisellText, {
			className: "login-btn",
			onClick: () => {
				openLoginModal({ callback: () => {} });
			}
		}, _pisell_utils.locales.getText("pisell2.text.goodpass.login")), " ", /* @__PURE__ */ react.default.createElement("span", null, _pisell_utils.locales.getText("pisell2.text.goodpass.login-tip"))));
	};
	return /* @__PURE__ */ react.default.createElement("div", { className: "pisell-lowcode__appointment-booking-voucher-container" }, /* @__PURE__ */ react.default.createElement("div", { className: "voucher-title" }, /* @__PURE__ */ react.default.createElement("span", { className: "voucher-title-left" }, _pisell_utils.locales.getText("pisell2.text.goodpass")), /* @__PURE__ */ react.default.createElement("span", {
		className: "voucher-title-right",
		onClick: () => setShowDetail(!showDetail)
	}, _pisell_utils.locales.getText("pisell2.text.details"), " ", /* @__PURE__ */ react.default.createElement(_pisell_icon_es_ChevronDown.default, { className: (0, classnames.default)("voucher-right-icon", { show: showDetail }) }))), /* @__PURE__ */ react.default.createElement("div", { className: (0, classnames.default)("voucher-detail", { show: showDetail }) }, availableData.length > 0 ? /* @__PURE__ */ react.default.createElement(_pisell_materials.PisellGoodPassCard, {
		style: { width: "100%" },
		dataSource: availableData,
		mixedSort,
		onChange: onDiscountChange
	}) : null, renderLoginCard(), /* @__PURE__ */ react.default.createElement("div", { className: "voucher-code-input-wrapper" }, /* @__PURE__ */ react.default.createElement("div", { className: "voucher-code-input" }, /* @__PURE__ */ react.default.createElement("div", { style: { whiteSpace: "nowrap" } }, _pisell_utils.locales.getText("pisell2.text.goodpass.code"), ":"), /* @__PURE__ */ react.default.createElement(_pisell_materials.PisellInput, {
		value: code,
		allowClear: true,
		onChange: (e) => {
			var _e$target;
			return setCode(((_e$target = e.target) === null || _e$target === void 0 ? void 0 : _e$target.value) || "");
		},
		size: "small",
		placeholder: _pisell_utils.locales.getText("pisell2.text.goodpass.enter-code")
	})), /* @__PURE__ */ react.default.createElement(antd.Button, {
		size: "small",
		onClick: onScanCode,
		disabled: !code.trim()
	}, _pisell_utils.locales.getText("pisell2.text.apply")))));
}
//#endregion
exports.default = VoucherCard;
