const require_runtime = require("../../../../_virtual/_rolldown/runtime.js");
const require_defineProperty = require("../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/defineProperty.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_index = require("./index.js");
let react = require("react");
react = require_runtime.__toESM(react);
let react_dom = require("react-dom");
(0, react.createContext)(null);
const DEFAULT_TOAST_CONFIG = {
	width: 680,
	mask: false,
	maskClosable: false,
	closable: true
};
var GlobalToastManager = class {
	constructor() {
		require_defineProperty._defineProperty(this, "instances", /* @__PURE__ */ new Map());
		require_defineProperty._defineProperty(this, "nextId", 1);
		require_defineProperty._defineProperty(this, "TOAST_HEIGHT", 160);
		require_defineProperty._defineProperty(this, "TOAST_MARGIN", 8);
		require_defineProperty._defineProperty(this, "BOTTOM_OFFSET", 110);
		require_defineProperty._defineProperty(this, "LEFT_OFFSET", 96);
		require_defineProperty._defineProperty(this, "cachedBottomOffset", null);
		require_defineProperty._defineProperty(this, "cachedCenterOffset", null);
		require_defineProperty._defineProperty(this, "tillButton", null);
	}
	setTillButton(tillButton) {
		this.tillButton = tillButton;
	}
	generateId() {
		return `toast-${this.nextId++}`;
	}
	calculateAndCacheBottomOffset() {
		var _this$tillButton;
		if (this.cachedBottomOffset !== null) return this.cachedBottomOffset;
		if ((_this$tillButton = this.tillButton) === null || _this$tillButton === void 0 ? void 0 : _this$tillButton.y) {
			console.log("使用钱箱按钮位置", this.tillButton);
			this.cachedBottomOffset = window.innerHeight - this.tillButton.y + 8 + this.tillButton.size;
			console.log("计算钱箱按钮位置得到的bottom位置", this.cachedBottomOffset);
			return this.cachedBottomOffset;
		}
		const ticketBookingElement = document.querySelector(".ticket-booking-left-panel-content");
		if (ticketBookingElement) {
			const rect = ticketBookingElement.getBoundingClientRect();
			this.cachedBottomOffset = window.innerHeight - rect.bottom + 16;
		} else this.cachedBottomOffset = this.BOTTOM_OFFSET;
		return this.cachedBottomOffset;
	}
	calculateAndCacheCenterOffset() {
		if (this.cachedCenterOffset !== null) return this.cachedCenterOffset;
		const ticketBookingElement = document.querySelector(".ticket-booking-left-panel-content");
		if (ticketBookingElement) {
			const rect = ticketBookingElement.getBoundingClientRect();
			this.cachedCenterOffset = {
				x: rect.width / 2,
				y: rect.height / 2
			};
		} else this.cachedCenterOffset = {
			x: 0,
			y: 0
		};
		return this.cachedCenterOffset;
	}
	resetBottomOffsetCache() {
		this.cachedBottomOffset = null;
	}
	calculateBottomPosition(offsetIndex, totalCount) {
		const reversedIndex = totalCount - 1 - offsetIndex;
		return this.calculateAndCacheBottomOffset() + (this.TOAST_HEIGHT + this.TOAST_MARGIN) * reversedIndex;
	}
	updatePositions() {
		const instances = Array.from(this.instances.values());
		const totalCount = instances.length;
		instances.forEach((instance, index) => {
			instance.offsetIndex = index;
			const bottomPosition = this.calculateBottomPosition(index, totalCount);
			if (instance.container) {
				if (instance.container.getAttribute("data-mask") === "true") return;
				instance.container.classList.add("toast-moving");
				instance.container.style.position = "fixed";
				instance.container.style.left = `${this.LEFT_OFFSET}px`;
				instance.container.style.bottom = `${bottomPosition}px`;
				instance.container.style.top = "auto";
				instance.container.style.right = "auto";
				instance.container.style.transition = "all 0.4s cubic-bezier(0.25, 0.46, 0.45, 0.94)";
				instance.container.style.pointerEvents = "none";
				instance.container.style.width = "auto";
				instance.container.style.height = "auto";
				instance.container.style.transform = "none";
				instance.container.style.margin = "0";
				setTimeout(() => {
					if (instance.container) instance.container.classList.remove("toast-moving");
				}, 400);
			}
		});
	}
	addToast(config) {
		const id = this.generateId();
		this.resetBottomOffsetCache();
		const finalConfig = require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, DEFAULT_TOAST_CONFIG), {}, {
			visible: true,
			statusClassName: config.statusClassName,
			title: config.title,
			subtitle: config.subtitle,
			changeDueText: config.changeDueText,
			actionButtons: config.actionButtons || [],
			autoClose: config.autoClose,
			autoCloseDelay: config.autoCloseDelay || 30,
			showCountdownProgress: config.showCountdownProgress,
			clickOutsideToClose: config.clickOutsideToClose,
			onCountdownComplete: config.onCountdownComplete,
			onClose: () => {
				var _config$onClose2;
				(_config$onClose2 = config.onClose) === null || _config$onClose2 === void 0 || _config$onClose2.call(config);
				this.removeToast(id);
			},
			closable: config.closable !== void 0 ? config.closable : DEFAULT_TOAST_CONFIG.closable,
			mask: config.mask || false
		});
		const container = document.createElement("div");
		container.setAttribute("data-toast-id", id);
		if (config.mask) container.setAttribute("data-mask", "true");
		const dynamicBottomOffset = this.calculateAndCacheBottomOffset();
		container.style.position = "fixed";
		if (config.mask) {
			container.style.left = "50%";
			container.style.top = "50%";
			container.style.transform = "translate(-50%, -50%)";
			container.style.bottom = "auto";
			container.style.right = "auto";
		} else {
			container.style.left = `${this.LEFT_OFFSET}px`;
			container.style.bottom = `${dynamicBottomOffset}px`;
			container.style.top = "auto";
			container.style.right = "auto";
			container.style.transform = "none";
		}
		container.style.pointerEvents = "none";
		container.style.width = "auto";
		container.style.height = "auto";
		container.style.margin = "0";
		document.body.appendChild(container);
		const instance = {
			id,
			container,
			props: finalConfig,
			offsetIndex: this.instances.size
		};
		this.instances.set(id, instance);
		(0, react_dom.render)(/* @__PURE__ */ react.default.createElement(require_index.default, finalConfig), container);
		return id;
	}
	removeToast(id) {
		const instance = this.instances.get(id);
		if (instance) if (instance.container) {
			instance.container.classList.add("toast-exiting");
			setTimeout(() => {
				if (instance.container) {
					(0, react_dom.unmountComponentAtNode)(instance.container);
					if (document.body.contains(instance.container)) document.body.removeChild(instance.container);
				}
				this.instances.delete(id);
				this.updatePositions();
			}, 300);
		} else {
			this.instances.delete(id);
			this.updatePositions();
		}
	}
	clearAll() {
		this.instances.forEach((instance) => {
			if (instance.container) {
				(0, react_dom.unmountComponentAtNode)(instance.container);
				if (document.body.contains(instance.container)) document.body.removeChild(instance.container);
			}
		});
		this.instances.clear();
	}
	getCount() {
		return this.instances.size;
	}
	initializeResizeListener() {
		const handleResize = () => {
			this.resetBottomOffsetCache();
			this.updatePositions();
		};
		window.addEventListener("resize", handleResize);
		return () => {
			window.removeEventListener("resize", handleResize);
		};
	}
};
const globalToastManager = new GlobalToastManager();
if (typeof window !== "undefined") globalToastManager.initializeResizeListener();
const renderGlobalToast = (config) => {
	return globalToastManager.addToast(config);
};
const showPaymentResultToastSuccess = function() {
	var _ref = require_asyncToGenerator._asyncToGenerator(function* (config) {
		return renderGlobalToast(require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, config), {}, { statusClassName: "success" }));
	});
	return function showPaymentResultToastSuccess(_x) {
		return _ref.apply(this, arguments);
	};
}();
const showPaymentResultToastFailed = function() {
	var _ref2 = require_asyncToGenerator._asyncToGenerator(function* (config) {
		return renderGlobalToast(require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, config), {}, { statusClassName: "failed" }));
	});
	return function showPaymentResultToastFailed(_x2) {
		return _ref2.apply(this, arguments);
	};
}();
(function() {
	var _ref3 = require_asyncToGenerator._asyncToGenerator(function* (config) {
		return renderGlobalToast(require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, config), {}, { statusClassName: "wallet-pass-failed" }));
	});
	return function showPaymentResultToastWalletPassFailed(_x3) {
		return _ref3.apply(this, arguments);
	};
})();
(function() {
	var _ref4 = require_asyncToGenerator._asyncToGenerator(function* (config) {
		return renderGlobalToast(require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, config), {}, { statusClassName: "partially-paid" }));
	});
	return function showPaymentResultToastPartiallyPaid(_x4) {
		return _ref4.apply(this, arguments);
	};
})();
(function() {
	var _ref5 = require_asyncToGenerator._asyncToGenerator(function* (config) {
		return renderGlobalToast(config);
	});
	return function showPaymentResultToast(_x5) {
		return _ref5.apply(this, arguments);
	};
})();
const closePaymentResultToast = (id) => {
	if (id) globalToastManager.removeToast(id);
	else globalToastManager.clearAll();
};
const setTillButton = (tillButton) => {
	globalToastManager.setTillButton(tillButton);
};
//#endregion
exports.closePaymentResultToast = closePaymentResultToast;
exports.globalToastManager = globalToastManager;
exports.setTillButton = setTillButton;
exports.showPaymentResultToastFailed = showPaymentResultToastFailed;
exports.showPaymentResultToastSuccess = showPaymentResultToastSuccess;
