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MJ,YAAY,QAAoB,EAAE,KAAa,CAAE;aAJzC,YAA2B;QAKjC,IAAI,CAAC,SAAS,GAAG;QACjB,IAAI,CAAC,QAAQ,GAAG;IAClB;IAEA,MAAM,KAAa,EAAQ;QACzB,IAAI,CAAC,SAAS,GAAG;QACjB,IAAI,CAAC,MAAM;IACb;IAEA,QAAc;QACZ,IAAI,IAAI,CAAC,OAAO,IAAI,MAClB;QAGF,aAAa,IAAI,CAAC,OAAO;QACzB,IAAI,CAAC,OAAO,GAAG;QACf,IAAI,CAAC,SAAS,IAAI,KAAK,GAAG,KAAK,IAAI,CAAC,SAAS;IAC/C;IAEA,SAAe;QACb,IAAI,IAAI,CAAC,SAAS,IAAI,GACpB;QAGF,IAAI,CAAC,SAAS,GAAG,KAAK,GAAG;QACzB,IAAI,CAAC,OAAO,GAAG,WAAW;YACxB,IAAI,CAAC,OAAO,GAAG;YACf,IAAI,CAAC,SAAS,GAAG;YACjB,IAAI,CAAC,QAAQ;QACf,GAAG,IAAI,CAAC,SAAS;IACnB;AACF","sources":["packages/react-stately/src/toast/useToastState.ts"],"sourcesContent":["/*\n * Copyright 2020 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nimport {useCallback, useMemo} from 'react';\n// Shim to support React 17 and below.\nimport {useSyncExternalStore} from 'use-sync-external-store/shim/index.js';\n\ntype ToastAction = 'add' | 'remove' | 'clear';\nexport interface ToastStateProps {\n  /** The maximum number of toasts to display at a time. */\n  maxVisibleToasts?: number,\n  /** Function to wrap updates in (i.e. document.startViewTransition()). */\n  wrapUpdate?: (fn: () => void, action: ToastAction) => void\n}\n\nexport interface ToastOptions {\n  /** Handler that is called when the toast is closed, either by the user or after a timeout. */\n  onClose?: () => void,\n  /** A timeout to automatically close the toast after, in milliseconds. */\n  timeout?: number\n}\n\nexport interface QueuedToast<T> extends ToastOptions {\n  /** The content of the toast. */\n  content: T,\n  /** A unique key for the toast. */\n  key: string,\n  /** A timer for the toast, if a timeout was set. */\n  timer?: Timer\n}\n\nexport interface ToastState<T> {\n  /** Adds a new toast to the queue. */\n  add(content: T, options?: ToastOptions): string,\n  /**\n   * Closes a toast.\n   */\n  close(key: string): void,\n  /** Pauses the timers for all visible toasts. */\n  pauseAll(): void,\n  /** Resumes the timers for all visible toasts. */\n  resumeAll(): void,\n  /** The visible toasts. */\n  visibleToasts: QueuedToast<T>[]\n}\n\n/**\n * Provides state management for a toast queue. Toasts display brief, temporary notifications\n * of actions, errors, or other events in an application.\n */\nexport function useToastState<T>(props: ToastStateProps = {}): ToastState<T> {\n  let {maxVisibleToasts = 1, wrapUpdate} = props;\n  let queue = useMemo(() => new ToastQueue<T>({maxVisibleToasts, wrapUpdate}), [maxVisibleToasts, wrapUpdate]);\n  return useToastQueue(queue);\n}\n\n/**\n * Subscribes to a provided toast queue and provides methods to update it.\n */\nexport function useToastQueue<T>(queue: ToastQueue<T>): ToastState<T> {\n  let subscribe = useCallback(fn => queue.subscribe(fn), [queue]);\n  let getSnapshot = useCallback(() => queue.visibleToasts, [queue]);\n  let visibleToasts = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);\n\n  return {\n    visibleToasts,\n    add: (content, options) => queue.add(content, options),\n    close: key => queue.close(key),\n    pauseAll: () => queue.pauseAll(),\n    resumeAll: () => queue.resumeAll()\n  };\n}\n\n/**\n * A ToastQueue manages the order of toasts.\n */\nexport class ToastQueue<T> {\n  private queue: QueuedToast<T>[] = [];\n  private subscriptions: Set<() => void> = new Set();\n  private maxVisibleToasts: number;\n  private wrapUpdate?: (fn: () => void, action: ToastAction) => void;\n  /** The currently visible toasts. */\n  visibleToasts: QueuedToast<T>[] = [];\n\n  constructor(options?: ToastStateProps) {\n    this.maxVisibleToasts = options?.maxVisibleToasts ?? Infinity;\n    this.wrapUpdate = options?.wrapUpdate;\n  }\n\n  private runWithWrapUpdate(fn: () => void, action: ToastAction): void {\n    if (this.wrapUpdate) {\n      this.wrapUpdate(fn, action);\n    } else {\n      fn();\n    }\n  }\n\n  /** Subscribes to updates to the visible toasts. */\n  subscribe(fn: () => void): () => void {\n    this.subscriptions.add(fn);\n    return () => this.subscriptions.delete(fn);\n  }\n\n  /** Adds a new toast to the queue. */\n  add(content: T, options: ToastOptions = {}): string {\n    let toastKey = '_' + Math.random().toString(36).slice(2);\n    let toast: QueuedToast<T> = {\n      ...options,\n      content,\n      key: toastKey,\n      timer: options.timeout ? new Timer(() => this.close(toastKey), options.timeout) : undefined\n    };\n\n    this.queue.unshift(toast);\n\n    this.updateVisibleToasts('add');\n    return toastKey;\n  }\n\n  /**\n   * Closes a toast.\n   */\n  close(key: string): void {\n    let index = this.queue.findIndex(t => t.key === key);\n    if (index >= 0) {\n      this.queue[index].onClose?.();\n      this.queue.splice(index, 1);\n    }\n\n    this.updateVisibleToasts('remove');\n  }\n\n  private updateVisibleToasts(action: ToastAction) {\n    this.visibleToasts = this.queue.slice(0, this.maxVisibleToasts);\n\n    this.runWithWrapUpdate(() => {\n      for (let fn of this.subscriptions) {\n        fn();\n      }\n    }, action);\n  }\n\n  /** Pauses the timers for all visible toasts. */\n  pauseAll(): void {\n    for (let toast of this.visibleToasts) {\n      if (toast.timer) {\n        toast.timer.pause();\n      }\n    }\n  }\n\n  /** Resumes the timers for all visible toasts. */\n  resumeAll(): void {\n    for (let toast of this.visibleToasts) {\n      if (toast.timer) {\n        toast.timer.resume();\n      }\n    }\n  }\n\n  clear(): void {\n    this.queue = [];\n    this.updateVisibleToasts('clear');\n  }\n}\n\nclass Timer {\n  private timerId;\n  private startTime: number | null = null;\n  private remaining: number;\n  private callback: () => void;\n\n  constructor(callback: () => void, delay: number) {\n    this.remaining = delay;\n    this.callback = callback;\n  }\n\n  reset(delay: number): void {\n    this.remaining = delay;\n    this.resume();\n  }\n\n  pause(): void {\n    if (this.timerId == null) {\n      return;\n    }\n\n    clearTimeout(this.timerId);\n    this.timerId = null;\n    this.remaining -= Date.now() - this.startTime!;\n  }\n\n  resume(): void {\n    if (this.remaining <= 0) {\n      return;\n    }\n\n    this.startTime = Date.now();\n    this.timerId = setTimeout(() => {\n      this.timerId = null;\n      this.remaining = 0;\n      this.callback();\n    }, this.remaining);\n  }\n}\n"],"names":[],"version":3,"file":"useToastState.cjs.map"}