"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useToast = exports.ToastProvider = void 0;
var _react = _interopRequireWildcard(require("react"));
var _Toast = require("../components/Toast");
var _reactNative = require("react-native");
var _toastEmitter = require("../utils/toastEmitter");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
// src/context/ToastContext.tsx

const ToastContext = /*#__PURE__*/(0, _react.createContext)(undefined);
const ToastProvider = ({
  children
}) => {
  const [toasts, setToasts] = (0, _react.useState)([]);

  // Subscribe to global toast emitter so non-React code can trigger toasts
  (0, _react.useEffect)(() => {
    const unsubscribe = _toastEmitter.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__*/(0, _jsxRuntime.jsxs)(ToastContext.Provider, {
    value: {
      showToast
    },
    children: [children, /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, {
      style: {
        position: 'absolute',
        top: 70,
        left: 0,
        right: 0,
        flexDirection: 'column',
        alignItems: 'center',
        zIndex: 9999
      },
      children: toasts.map(toast => /*#__PURE__*/(0, _jsxRuntime.jsx)(_Toast.Toast, {
        ...toast
      }, toast.id))
    })]
  });
};
exports.ToastProvider = ToastProvider;
const useToast = () => {
  const context = (0, _react.useContext)(ToastContext);
  if (!context) {
    throw new Error('useToast must be used within a ToastProvider');
  }
  return context;
};
exports.useToast = useToast;
//# sourceMappingURL=ToastContext.js.map