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_useEngineContext = require("../../../../hooks/useEngineContext.js");
let react = require("react");
react = require_runtime.__toESM(react);
let antd = require("antd");
let _pisell_utils = require("@pisell/utils");
let classnames = require("classnames");
classnames = require_runtime.__toESM(classnames);
let _ant_design_icons = require("@ant-design/icons");
require("./index.less");
//#region src/components/booking/info/header/index.tsx
const header = ({ state, dispatch }) => {
	var _context$appHelper, _state$service2, _bookingDetail$bookin;
	const context = require_useEngineContext.default();
	const [modalContent, setModalContent] = (0, react.useState)({
		open: false,
		children: "",
		footer: null,
		title: /* @__PURE__ */ react.default.createElement("div", {
			className: "",
			style: { height: 14 }
		})
	});
	const { isTerminal, interaction, store, storage } = (_context$appHelper = context.appHelper) === null || _context$appHelper === void 0 ? void 0 : _context$appHelper.utils;
	const bookingDetail = (0, react.useMemo)(() => {
		return require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, state.preliminaryBookingDetail || {}), state.bookingDetail || {});
	}, [state.bookingDetail, state.preliminaryBookingDetail]);
	const statusList = [
		{
			label: _pisell_utils.locales.getText("pisell2.text.status.new"),
			key: "new",
			color: "#91caff"
		},
		{
			label: _pisell_utils.locales.getText("pisell2.text.status.confirmed"),
			key: "confirmed",
			color: "#b7eb8f"
		},
		{
			label: _pisell_utils.locales.getText("pisell2.text.status.arrived"),
			key: "arrived",
			color: "#d3adf7"
		},
		{
			label: _pisell_utils.locales.getText("pisell2.text.status.started"),
			key: "started",
			color: "#fffb8f"
		},
		{
			label: /* @__PURE__ */ react.default.createElement("span", { style: { color: "#D92D20" } }, _pisell_utils.locales.getText("pisell2.text.status.cancelled")),
			key: "cancelled",
			color: "#bfbfbf"
		},
		{
			label: /* @__PURE__ */ react.default.createElement("span", { style: { color: "#D92D20" } }, _pisell_utils.locales.getText("pisell2.text.status.no_show")),
			key: "no_show",
			color: "#ffa39e"
		},
		{
			label: _pisell_utils.locales.getText("pisell2.text.status.completed"),
			key: "completed",
			color: "#ffd591"
		}
	];
	const orderSalesChannel = {
		online_store: _pisell_utils.locales.getText("pisell2.text.order.sales.channel.online"),
		pos: _pisell_utils.locales.getText("pisell2.text.order.sales.channel.pos-terminal"),
		boxlocal: _pisell_utils.locales.getText("pisell2.text.order.sales.channel.boxcocal"),
		"table-order": _pisell_utils.locales.getText("pisell2.text.order.sales.channel.tableorder"),
		google_reserve: _pisell_utils.locales.getText("pisell2.text.order.sales.channel.reserve")
	};
	/**
	* @title: 预约状态字典
	* @description:
	* @return {*}
	* @Author: zhiwei.Wang
	* @Date: 2024-08-22 09:31
	*/
	const getStatusMap = () => {
		let obj = {};
		for (let i = 0; i < statusList.length; i++) obj[statusList[i].key] = statusList[i];
		return obj;
	};
	const statusMps = getStatusMap();
	const items = (0, react.useMemo)(() => {
		return statusList.filter((item) => item.key !== state.appointment_status);
	}, [statusList]);
	/**
	* @title: 当前状态
	* @description:
	* @param {*} useMemo
	* @return {*}
	* @Author: zhiwei.Wang
	* @Date: 2024-08-22 09:32
	*/
	const currentStatus = (0, react.useMemo)(() => {
		return statusMps[state.appointment_status] || statusMps["new"];
	}, [state.appointment_status]);
	/**
	* @title: 设置预约状态
	* @description:
	* @param {string} status
	* @return {*}
	* @Author: zhiwei.Wang
	* @Date: 2024-08-22 09:30
	*/
	const setBookingStatus = function() {
		var _ref = require_asyncToGenerator._asyncToGenerator(function* (status) {
			dispatch({
				type: "setStatus",
				payload: status
			});
			yield state.apis.editBookingStatus({ appointment_status: status });
			antd.message.success(_pisell_utils.locales.getText("pisell1.text.edit-status-success"));
			state === null || state === void 0 || state.callback({ order_id: state.bookingId });
		});
		return function setBookingStatus(_x) {
			return _ref.apply(this, arguments);
		};
	}();
	/**
	* @title: 修改预约状态回调
	* @description:
	* @param {any} e
	* @return {*}
	* @Author: zhiwei.Wang
	* @Date: 2024-08-22 09:30
	*/
	const onSelect = function() {
		var _ref2 = require_asyncToGenerator._asyncToGenerator(function* (e) {
			if (e.key === "cancelled") {
				handleCancel();
				return;
			}
			yield setBookingStatus(e.key);
		});
		return function onSelect(_x2) {
			return _ref2.apply(this, arguments);
		};
	}();
	const setModalContentState = (values) => {
		setModalContent(require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, modalContent), values));
	};
	/**
	* @title: 跳转到支付页面
	* @description:
	* @return {*}
	* @Author: zhiwei.Wang
	* @Date: 2024-08-22 09:32
	*/
	const goCheckout = function() {
		var _ref3 = require_asyncToGenerator._asyncToGenerator(function* () {
			if (isTerminal === null || isTerminal === void 0 ? void 0 : isTerminal()) {
				var _interaction$utils, _interaction$utils$po;
				return interaction === null || interaction === void 0 || (_interaction$utils = interaction.utils) === null || _interaction$utils === void 0 || (_interaction$utils$po = _interaction$utils.postMessageToApp) === null || _interaction$utils$po === void 0 ? void 0 : _interaction$utils$po.call(_interaction$utils, {
					module: "booking",
					key: "booking_checkout",
					data: {
						order_id: state.bookingId,
						callbackType: "pisell2"
					}
				});
			}
			state.action({
				type: "pisell1.handlePayment",
				data: { orderId: state.bookingId },
				callback: () => {
					state === null || state === void 0 || state.callback();
				}
			});
		});
		return function goCheckout() {
			return _ref3.apply(this, arguments);
		};
	}();
	/**
	* @title: 是否已全额支付
	*/
	const isPaid = (0, react.useMemo)(() => {
		return (bookingDetail === null || bookingDetail === void 0 ? void 0 : bookingDetail.payment_status) === "paid";
	}, [bookingDetail === null || bookingDetail === void 0 ? void 0 : bookingDetail.payment_status]);
	/**
	* @title: 是否包含票务商品
	* @description:
	* @param {*} useMemo
	* @return {*}
	* @Author: zhiwei.Wang
	* @Date: 2024-08-22 09:25
	*/
	const isTicket = (0, react.useMemo)(() => {
		var _state$service;
		return (((_state$service = state.service) === null || _state$service === void 0 ? void 0 : _state$service.value) || []).some((d) => [
			"appointment_ticket",
			"product_package_ticket",
			"ticket"
		].includes(d.extension_type));
	}, [(_state$service2 = state.service) === null || _state$service2 === void 0 ? void 0 : _state$service2.value]);
	/**
	* @title: 是否存在待确认的支付
	* @description:
	* @param {*} useMemo
	* @return {*}
	* @Author: zhiwei.Wang
	* @Date: 2024-08-22 09:32
	*/
	const paymentProcessingPayment = (type) => {
		const payments = (bookingDetail === null || bookingDetail === void 0 ? void 0 : bookingDetail.payments) || [];
		const status = ["payment_processing"];
		const payment_method = ["BANK_DEPOSIT"];
		if (type === "bank") return !!(payments === null || payments === void 0 ? void 0 : payments.find((item) => status.includes(item === null || item === void 0 ? void 0 : item.status) && payment_method.includes(item === null || item === void 0 ? void 0 : item.payment_method)));
		return !!(payments === null || payments === void 0 ? void 0 : payments.find((item) => status.includes(item === null || item === void 0 ? void 0 : item.status)));
	};
	/**
	* @title: 取消预约
	* @description:
	* @return {*}
	* @Author: zhiwei.Wang
	* @Date: 2024-08-22 09:33
	*/
	const handleCancel = () => {
		if (paymentProcessingPayment("bank")) return showPaymentModal();
		if (paymentProcessingPayment()) return showCancelCalendarModal();
		return showCancelledModal();
	};
	/**
	* @title: 退款
	* @description:
	* @return {*}
	* @Author: zhiwei.Wang
	* @Date: 2024-01-30 16:57
	*/
	const handleRefund = () => {
		if (isTerminal === null || isTerminal === void 0 ? void 0 : isTerminal()) {
			var _interaction$utils2, _interaction$utils2$p;
			return interaction === null || interaction === void 0 || (_interaction$utils2 = interaction.utils) === null || _interaction$utils2 === void 0 || (_interaction$utils2$p = _interaction$utils2.postMessageToApp) === null || _interaction$utils2$p === void 0 ? void 0 : _interaction$utils2$p.call(_interaction$utils2, {
				module: "booking",
				key: "booking_refund",
				data: { order_id: state.bookingId },
				origin: "/booking"
			});
		}
		state.action({
			type: "pisell1.handleRefund",
			data: { orderId: state.bookingId },
			callback: () => {
				state === null || state === void 0 || state.callback({ order_id: state.bookingId });
			}
		});
	};
	/**
	* @title: 显示去支付的弹窗
	* @description:
	* @return {*}
	* @Author: zhiwei.Wang
	* @Date: 2024-08-22 09:33
	*/
	const showPaymentModal = () => {
		setModalContentState({
			className: "go-payments-modal-wrap",
			open: true,
			width: 480,
			onCancel: () => {
				setModalContentState({ open: false });
			},
			children: /* @__PURE__ */ react.default.createElement("div", { className: "go-payments-modal-content" }, /* @__PURE__ */ react.default.createElement("div", { className: "go-payments-modal-content-title" }, _pisell_utils.locales.getText("page.booking.text.unconfirmed-payments")), /* @__PURE__ */ react.default.createElement("div", { className: "go-payments-modal-content-desc" }, _pisell_utils.locales.getText("page.booking.text.unconfirmed-payments.msg")), /* @__PURE__ */ react.default.createElement("div", { className: "go-payments-footer" }, /* @__PURE__ */ react.default.createElement(antd.Button, {
				className: "go-payments-footer-btn",
				size: "large",
				onClick: () => {
					setModalContentState({ open: false });
				}
			}, _pisell_utils.locales.getText("page.booking.text.go-back")), /* @__PURE__ */ react.default.createElement(antd.Button, {
				className: "go-payments-footer-btn",
				type: "primary",
				size: "large",
				onClick: () => {
					goCheckout();
					setModalContentState({ open: false });
				}
			}, _pisell_utils.locales.getText("page.booking.text.go-to-payments")))),
			footer: null
		});
	};
	/**
	* @title: 显示去支付的弹窗
	* @description:
	* @return {*}
	* @Author: zhiwei.Wang
	* @Date: 2024-08-22 09:33
	*/
	const showCancelCalendarModal = () => {
		setModalContentState({
			className: "go-payments-modal-wrap",
			open: true,
			width: 480,
			onCancel: () => {
				setModalContentState({ open: false });
			},
			children: /* @__PURE__ */ react.default.createElement("div", { className: "go-payments-modal-content" }, /* @__PURE__ */ react.default.createElement("div", { className: "go-payments-modal-content-title" }, _pisell_utils.locales.getText("page.booking.text.cancel-booking")), /* @__PURE__ */ react.default.createElement("div", { className: "go-payments-modal-content-desc" }, _pisell_utils.locales.getText("page.booking.text.cancel.calendar.msg")), /* @__PURE__ */ react.default.createElement("div", { className: "go-payments-footer" }, /* @__PURE__ */ react.default.createElement(antd.Button, {
				className: "go-payments-footer-btn",
				size: "large",
				onClick: () => {
					setModalContentState({ open: false });
				}
			}, _pisell_utils.locales.getText("page.booking.text.go-back")), /* @__PURE__ */ react.default.createElement(antd.Button, {
				className: "go-payments-footer-btn",
				type: "primary",
				size: "large",
				onClick: () => {
					setBookingStatus("cancelled");
					setModalContentState({ open: false });
				}
			}, _pisell_utils.locales.getText("page.booking.text.cancel-booking")))),
			footer: null
		});
	};
	/**
	* @title: 显示取消预约弹窗
	* @description:
	* @return {*}
	* @Author: zhiwei.Wang
	* @Date: 2024-08-22 09:34
	*/
	const showCancelledModal = () => {
		const cancelBtn = /* @__PURE__ */ react.default.createElement(antd.Button, {
			className: "go-payments-footer-btn",
			size: "large",
			type: "primary",
			onClick: () => {
				setBookingStatus("cancelled");
				setModalContentState({ open: false });
			}
		}, _pisell_utils.locales.getText("page.booking.text.cancel-only"));
		const refundBtn = /* @__PURE__ */ react.default.createElement(antd.Button, {
			className: "go-payments-footer-btn",
			size: "large",
			danger: true,
			ghost: true,
			onClick: () => {
				handleRefund();
				setModalContentState({ open: false });
			}
		}, _pisell_utils.locales.getText("page.booking.text.refund"));
		const okBtn = /* @__PURE__ */ react.default.createElement(antd.Button, {
			className: "go-payments-footer-btn",
			size: "large",
			type: "primary",
			onClick: () => {
				setBookingStatus("cancelled");
				setModalContentState({ open: false });
			}
		}, _pisell_utils.locales.getText("page.booking.text.confirm"));
		const backBtn = /* @__PURE__ */ react.default.createElement(antd.Button, {
			className: "go-payments-footer-btn",
			size: "large",
			onClick: () => {
				setModalContentState({ open: false });
			}
		}, _pisell_utils.locales.getText("page.booking.text.go-back"));
		setModalContentState({
			className: "go-payments-modal-wrap",
			open: true,
			width: 480,
			onCancel: () => {
				setModalContentState({ open: false });
			},
			children: /* @__PURE__ */ react.default.createElement("div", { className: "go-payments-modal-content" }, /* @__PURE__ */ react.default.createElement("div", { className: "cancelled-modal-content-title" }, _pisell_utils.locales.getText("page.booking.text.cancel-booking")), /* @__PURE__ */ react.default.createElement("div", { className: "cancelled-modal-content-desc cancelled-modal-content-desc-red" }, _pisell_utils.locales.getText("page.booking.text.cancel-booking.msg")), isTicket ? /* @__PURE__ */ react.default.createElement("div", { className: "cancelled-modal-content-desc" }, _pisell_utils.locales.getText("page.booking.text.disabled-tickets")) : null, isPaid ? /* @__PURE__ */ react.default.createElement("div", { className: "cancelled-modal-content-list-wrap" }, /* @__PURE__ */ react.default.createElement("div", { className: "cancelled-modal-content-list-title" }, _pisell_utils.locales.getText("page.booking.text.paid.msg")("paid"), " ", _pisell_utils.locales.getText("page.booking.text.please-choose-an-option")), /* @__PURE__ */ react.default.createElement("ul", { className: "cancelled-modal-content-ul" }, /* @__PURE__ */ react.default.createElement("li", { className: "cancelled-modal-content-li" }, _pisell_utils.locales.getText("page.booking.text.please-choose-an-option-2")), /* @__PURE__ */ react.default.createElement("li", { className: "cancelled-modal-content-li" }, _pisell_utils.locales.getText("page.booking.text.please-choose-an-option-1")))) : null, /* @__PURE__ */ react.default.createElement("div", { className: (0, classnames.default)("go-payments-footer", { "go-payments-footer-column": isPaid }) }, isPaid ? /* @__PURE__ */ react.default.createElement(react.default.Fragment, null, cancelBtn, refundBtn, backBtn) : /* @__PURE__ */ react.default.createElement(react.default.Fragment, null, backBtn, okBtn))),
			footer: null
		});
	};
	return /* @__PURE__ */ react.default.createElement(react.default.Fragment, null, /* @__PURE__ */ react.default.createElement("div", {
		className: "booking-header",
		style: { backgroundColor: currentStatus.color }
	}, /* @__PURE__ */ react.default.createElement("div", { className: "booking-order-sales-channel" }, orderSalesChannel[bookingDetail === null || bookingDetail === void 0 ? void 0 : bookingDetail.order_sales_channel] || ""), /* @__PURE__ */ react.default.createElement("div", { className: "booking-header-line-2" }, /* @__PURE__ */ react.default.createElement("div", { className: "booking-header-left" }, _pisell_utils.locales.getText("pisell2.text.status.booking_id"), ":", " ", (bookingDetail === null || bookingDetail === void 0 || (_bookingDetail$bookin = bookingDetail.bookings) === null || _bookingDetail$bookin === void 0 ? void 0 : _bookingDetail$bookin.booking_id) || "-"), /* @__PURE__ */ react.default.createElement("div", { className: "booking-header-right" }, /* @__PURE__ */ react.default.createElement(antd.Dropdown, {
		menu: {
			items,
			onClick: onSelect
		},
		disabled: state.appointment_status === "cancelled" || !state.bookingDetail
	}, /* @__PURE__ */ react.default.createElement("div", { className: "booking-header-right-status" }, currentStatus.label, state.appointment_status !== "cancelled" ? /* @__PURE__ */ react.default.createElement(_ant_design_icons.DownOutlined, { className: "booking-header-right-status-icon" }) : null))))), /* @__PURE__ */ react.default.createElement(antd.Modal, modalContent));
};
//#endregion
exports.default = header;
