{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "core-toaster",
  "type": "registry:lib",
  "title": "core/toaster",
  "description": "Framework-free behaviour shared by the ui-* components (toaster); no DOM, no lit. Copied from @purpurabog/ui 2.3.0; the package is the maintained source, this copy is yours to change. Icons from the Carbon Design System (Apache-2.0, © IBM Corp.; see THIRD-PARTY-NOTICES.md in the package).",
  "dependencies": [],
  "registryDependencies": [
    "@kanok-ui/core-notification"
  ],
  "files": [
    {
      "path": "src/core/toaster.ts",
      "type": "registry:lib",
      "target": "components/ui/core/toaster.ts",
      "content": "/**\n * Toast rules (Carbon toast guidance; Carbon has no toast manager): how long a toast stays, which\n * toasts a new one pushes out, and a small queue a renderer can subscribe to. Shared by\n * `toast()` / `<ui-toaster>` and the React `toast()` / `<Toaster>`.\n */\nimport type { NotificationKind } from './notification.ts';\n\nexport type ToasterPlacement = 'top-end' | 'bottom-end';\n\n/** Kinds that need reading: they stay until closed. */\nconst LASTING: ReadonlySet<NotificationKind> = new Set(['error', 'warning', 'warning-alt']);\n\n/** A toast's auto-close time: the one asked for, else 5s for info / success and 0 (stays) for error / warning. */\nexport function toastTimeout(kind: NotificationKind, timeout?: number): number {\n    return timeout ?? (LASTING.has(kind) ? 0 : 5000);\n}\n\n/** How many of the oldest open toasts a toaster holding `count` must close to keep at most `max`. */\nexport function toastsOverMax(count: number, max: number): number {\n    return Math.max(0, count - max);\n}\n\nexport interface QueuedToast<T> {\n    /** Unique per toast shown. */\n    key: number;\n    /** The caller's id: a new toast with the same id replaces the old one. */\n    id?: string;\n    data: T;\n}\n\n/** The open toasts in the order they came, oldest first; at most `max` of them. */\nexport class ToastQueue<T> {\n    private items: QueuedToast<T>[] = [];\n    private next = 1;\n    private readonly listeners = new Set<() => void>();\n\n    max = 3;\n\n    /** The current toasts (a new array after each change, for change detection). */\n    get toasts(): readonly QueuedToast<T>[] {\n        return this.items;\n    }\n\n    /** Adds a toast (replacing an open one with the same `id`, dropping the oldest beyond `max`); returns its key. */\n    add(data: T, id?: string): number {\n        const key = this.next++;\n        const kept = id === undefined ? this.items : this.items.filter((t) => t.id !== id);\n        const all = [...kept, { key, id, data }];\n        this.items = all.slice(toastsOverMax(all.length, this.max));\n        this.emit();\n        return key;\n    }\n\n    remove(key: number) {\n        if (!this.items.some((t) => t.key === key)) return;\n        this.items = this.items.filter((t) => t.key !== key);\n        this.emit();\n    }\n\n    clear() {\n        if (!this.items.length) return;\n        this.items = [];\n        this.emit();\n    }\n\n    subscribe(listener: () => void): () => void {\n        this.listeners.add(listener);\n        return () => this.listeners.delete(listener);\n    }\n\n    private emit() {\n        for (const listener of this.listeners) listener();\n    }\n}\n"
    }
  ]
}
