import { r as n, h as e, H as r, g as c } from "./index.61732cef.js";
import "react";
import "react-dom";
const l = () => ".toast-provider__stack{position:fixed;inset:unset;bottom:var(--s-space-16);left:0;right:0;pointer-events:none;margin:0 var(--s-space-24);border:none;padding:0;background:none;width:auto;overflow:unset}.toast-provider__stack::backdrop{display:none}.toast-provider__stack>*{width:auto;pointer-events:auto}", h = class {
  constructor(t) {
    n(this, t), this.toasts = [], this.activeDialogStack = [], this.originalParent = null, this.onAction = (s) => {
      var o;
      const i = this.toasts.find(({ toastId: a }) => a === s.detail);
      !i || ((o = i.onAction) == null || o.call(i, i.toastId), i.dismissOnAction && this.dismiss(s.detail));
    }, this.onDismiss = (s) => {
      this.dismiss(s.detail);
    };
  }
  async clearAll() {
    this.toasts = [];
  }
  async dismiss(t) {
    var i;
    const s = this.toasts.find((o) => t === o.toastId);
    !s || ((i = s.onDismiss) == null || i.call(s, s.toastId), this.toasts = [...this.toasts].filter((o) => o.toastId !== t));
  }
  async toast(t) {
    if (this.toasts.some((i) => i.toastId === t.toastId))
      return;
    const s = {
      ...t,
      createdAt: new Date(),
      duration: t.duration || this.globalDuration || void 0,
      toastId: t.toastId || String(Math.round(Math.random() * 1e4))
    };
    return this.toasts = [...this.toasts, s], s;
  }
  componentWillLoad() {
    this.originalParent = this.el.parentElement;
  }
  disconnectedCallback() {
    this.el.isConnected || (this.activeDialogStack = this.activeDialogStack.filter((t) => t.isConnected), this.ensureCorrectPosition());
  }
  handleDialogToggle(t) {
    const { newState: s, dialog: i } = t.detail;
    s === "open" ? this.onDialogOpen(i) : this.onDialogClose(i);
  }
  onToastsChange() {
    this.toasts.length > 0 ? this.ensureCorrectPosition() : this.popoverEl.hidePopover();
  }
  onDialogOpen(t) {
    this.activeDialogStack.includes(t) || this.activeDialogStack.push(t), this.toasts.length > 0 && this.ensureCorrectPosition();
  }
  onDialogClose(t) {
    this.activeDialogStack = this.activeDialogStack.filter((s) => s != t), this.toasts.length > 0 && this.ensureCorrectPosition();
  }
  ensureCorrectPosition() {
    this.activeDialogStack = this.activeDialogStack.filter((s) => s.isConnected);
    const t = this.activeDialogStack[this.activeDialogStack.length - 1] || this.originalParent;
    this.el.parentElement !== t && t.appendChild(this.el), this.popoverEl.hidePopover(), this.popoverEl.showPopover();
  }
  render() {
    return e(r, { key: "b814bfac33e13fa660a10ec5d29f5f45fbac8f8d", role: "status" }, e("swirl-stack", { key: "03972a88936dfd3697335295381889095f6fb3a8", class: "toast-provider__stack", part: "toast-provider__stack", popover: "manual", spacing: "12", align: "center", ref: (t) => this.popoverEl = t }, this.toasts.map((t) => {
      const s = {
        ...t,
        content: void 0,
        onDismiss: void 0,
        onAction: void 0
      };
      return e("swirl-toast", { key: t.toastId, ...s, onDismiss: this.onDismiss, onAction: this.onAction }, t.content);
    })));
  }
  get el() {
    return c(this);
  }
  static get watchers() {
    return {
      toasts: [{
        onToastsChange: 0
      }]
    };
  }
};
h.style = l();
export {
  h as swirl_toast_provider
};
