import { createContext, useContext } from 'react';

/**
 * Internal context that exposes the pending-removal lifecycle for web toasts.
 *
 * Web toasts stay mounted after `hide()` is dispatched so the CSS exit
 * transition can play. The toast component reads `pendingRemovalIds` to
 * flip its `data-state="closed"` attribute and calls `remove(id)` when
 * the CSS transition completes (or a safety timeout fires).
 *
 * On native this context is provided but effectively unused — Reanimated's
 * `exiting` layout animation plays before unmount, so nothing lingers.
 *
 * @internal
 */
export interface ToastLifecycleContextValue {
  /** Force-remove a toast from the array once its exit transition has played. */
  remove: (id: string) => void;
  /** IDs of toasts that have been marked closed but are still mounted. */
  pendingRemovalIds: ReadonlySet<string>;
  /** ID of the front-most (last non-pending) toast, or null if none. */
  frontmostId: string | null;
}

export const ToastLifecycleContext = createContext<ToastLifecycleContextValue | null>(null);

/**
 * Access the toast lifecycle context.
 *
 * Returns `null` when the styled Toast is rendered outside a `ToastProvider`
 * (e.g. in isolated unit tests) so callers must handle the null case.
 *
 * @internal
 */
export function useToastLifecycle(): ToastLifecycleContextValue | null {
  return useContext(ToastLifecycleContext);
}
