"use strict";

// src/context/ToastContext.tsx
import React, { createContext, useContext, useEffect, useState } from 'react';
import { Toast } from '../components/Toast';
import { View } from 'react-native';
import { toastEmitter } from '../utils/toastEmitter';
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
const ToastContext = /*#__PURE__*/createContext(undefined);
export const ToastProvider = ({
  children
}) => {
  const [toasts, setToasts] = useState([]);

  // Subscribe to global toast emitter so non-React code can trigger toasts
  useEffect(() => {
    const unsubscribe = toastEmitter.subscribe(toast => showToast(toast));
    return unsubscribe;
  }, []);
  const showToast = toast => {
    setToasts(prev => {
      const next = [...prev, toast];
      return next.length > 5 ? next.slice(1) : next;
    });
    setTimeout(() => {
      setToasts(prev => prev.filter(t => t.id !== toast.id));
    }, toast.duration || 3000);
  };
  return /*#__PURE__*/_jsxs(ToastContext.Provider, {
    value: {
      showToast
    },
    children: [children, /*#__PURE__*/_jsx(View, {
      style: {
        position: 'absolute',
        top: 70,
        left: 0,
        right: 0,
        flexDirection: 'column',
        alignItems: 'center',
        zIndex: 9999
      },
      children: toasts.map(toast => /*#__PURE__*/_jsx(Toast, {
        ...toast
      }, toast.id))
    })]
  });
};
export const useToast = () => {
  const context = useContext(ToastContext);
  if (!context) {
    throw new Error('useToast must be used within a ToastProvider');
  }
  return context;
};
//# sourceMappingURL=ToastContext.js.map