import warning from "../_utils/warning.js";
import { ref } from "vue";
//#region components/notification/index.ts
var uuid = 0;
var sharedPermissionRef = ref(globalThis?.Notification?.permission ?? "denied");
var XNotification = class XNotification {
	static permissionMap = /* @__PURE__ */ new Map();
	static permissible;
	constructor() {
		XNotification.permissible = !!globalThis?.Notification;
		warning(XNotification.permissible, "XNotification", "Notification API is not supported in this environment.");
	}
	get permission() {
		if (!XNotification.permissible) return "denied";
		return globalThis.Notification?.permission;
	}
	open(arg) {
		if (!XNotification.permissible) return;
		const { title, tag, onClick, duration, onClose, onError, onShow, ...config } = arg || {};
		if (tag && XNotification.permissionMap.has(tag)) return;
		uuid += 1;
		const mergeKey = tag || `x_notification_${uuid}`;
		const notification = new globalThis.Notification(title, config || {});
		const close = notification.close.bind(notification);
		if (typeof duration === "number") {
			const timeoutId = setTimeout(() => {
				clearTimeout(timeoutId);
				close();
			}, duration * 1e3);
		}
		notification.onclick = (event) => {
			onClick?.(event, close);
		};
		notification.onshow = (event) => {
			onShow?.(event);
			XNotification.permissionMap.set(mergeKey, { close });
		};
		notification.onclose = (event) => {
			onClose?.(event);
			XNotification.permissionMap.delete(mergeKey);
		};
		notification.onerror = (event) => {
			onError?.(event);
		};
	}
	async requestPermission() {
		if (!XNotification.permissible) return "denied";
		const permissionRes = await globalThis.Notification.requestPermission();
		sharedPermissionRef.value = permissionRes;
		return permissionRes;
	}
	useNotification() {
		return [{ permission: sharedPermissionRef }, {
			open: (arg) => this.open(arg),
			close: (tags) => this.close(tags),
			requestPermission: () => this.requestPermission()
		}];
	}
	close(tags) {
		if (!XNotification.permissible) return;
		Array.from(XNotification.permissionMap.keys()).forEach((key) => {
			if (tags === void 0) XNotification.permissionMap.get(key)?.close?.();
			if (tags?.includes(key)) XNotification.permissionMap.get(key)?.close?.();
		});
	}
};
var notification_default = new XNotification();
//#endregion
export { XNotification, notification_default as default };
