/** @license MIT | @cube-dev/ui-kit v0.0.0-canary-f1f3d22 | Cube Dev Team */
import { Notification } from "../components/overlays/Notification/Notification.js";
import { createRef } from "react";
import { jsx } from "react/jsx-runtime";
import { CSSTransition, TransitionGroup } from "react-transition-group";
import { createRoot } from "react-dom/client";

//#region src/services/notification.tsx
let ID = 0;
/**
* @deprecated consider using `useNotificationsApi` instead
*/
const notification = {
	root: null,
	defaultOptions: { duration: 5e3 },
	items: [],
	init() {
		if (this.root) return;
		this.root = document.createElement("div");
		this.root.classList.add("cube-notification-container");
		document.body.appendChild(this.root);
		this._render([]);
	},
	render() {
		this.init();
		this._render();
	},
	_render(items) {
		if (!items) items = this.items;
		if (!this.root) return;
		createRoot(this.root).render(/* @__PURE__ */ jsx(TransitionGroup, {
			className: "cube-notifications",
			children: items.map((item) => {
				const nodeRef = createRef();
				return /* @__PURE__ */ jsx(CSSTransition, {
					nodeRef,
					timeout: 400,
					classNames: "cube-notification",
					children: /* @__PURE__ */ jsx("div", {
						ref: nodeRef,
						children: /* @__PURE__ */ jsx(Notification, {
							type: item.type,
							onClose: () => this.close(item.id),
							children: item.message
						})
					})
				}, item.id);
			})
		}));
	},
	show(type, message, options = {}) {
		options = Object.assign({}, this.defaultOptions, options);
		const id = ++ID;
		this.items.push({
			id,
			type,
			message
		});
		this.render();
		setTimeout(() => {
			this.close(id);
		}, options.duration);
	},
	close(id) {
		this.items = this.items.filter((item) => item.id !== id);
		this.render();
	},
	success(message, options) {
		this.show("success", message, options);
	},
	danger(message, options) {
		this.show("danger", message, options);
	},
	info(message, options) {
		this.show("note", message, options);
	}
};

//#endregion
export { notification };
//# sourceMappingURL=notification.js.map