"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.ToastProvider = exports.TOAST_THROTTLE_TIME = exports.TOAST_DURATION_TIME = exports.MAX_TOAST_STACK_SIZE = void 0;
var _react = require("react");
var _reactNative = require("react-native");
var _reactNativeReanimated = _interopRequireWildcard(require("react-native-reanimated"));
var _reactNativeSafeAreaContext = require("react-native-safe-area-context");
var _core = require("../../core");
var _functions = require("../../functions");
var _throttle = require("../../utils/throttle");
var _templates = require("../templates");
var _ToastNotification = require("./ToastNotification");
var _context = require("./context");
var _useIOToast = require("./useIOToast");
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); }
/**
 * The maximum number of toasts that can be displayed at the same time
 * If the number of the toasts exceeds this number, the oldest one will be removed
 */
const MAX_TOAST_STACK_SIZE = exports.MAX_TOAST_STACK_SIZE = 3;

/**
 * The time in milliseconds that a toast notification will be displayed
 */
const TOAST_DURATION_TIME = exports.TOAST_DURATION_TIME = 5000;

/**
 * This is the time in milliseconds between two toast notifications.
 * This will throttle the toast notifications to avoid displaying too many of them at the same time
 * and causing visual glitches.
 */
const TOAST_THROTTLE_TIME = exports.TOAST_THROTTLE_TIME = 500;
/**
 * A toast notification item that can be swiped to the right to dismiss it, with enter and exit animations
 */
const ToastNotificationStackItem = ({
  onDismiss,
  ...toast
}) => /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNativeReanimated.default.View, {
  entering: _reactNativeReanimated.SlideInUp.springify().damping(16).mass(0.9).stiffness(90),
  exiting: _reactNativeReanimated.SlideOutUp.duration(700).easing(_reactNativeReanimated.Easing.inOut(_reactNativeReanimated.Easing.exp)),
  layout: _reactNativeReanimated.SequencedTransition.duration(500).delay(50),
  style: {
    paddingBottom: 8
  },
  children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_templates.Dismissable, {
    onDismiss: onDismiss,
    children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_ToastNotification.ToastNotification, {
      ...toast
    })
  })
});
const ToastProvider = ({
  children
}) => {
  const toastId = (0, _react.useRef)(1);
  const [toasts, setToasts] = (0, _react.useState)([]);
  const addToast = (0, _react.useCallback)(toast => {
    const id = (toastId.current ?? 0) + 1;
    // eslint-disable-next-line functional/immutable-data
    toastId.current = id;
    setToasts(prevToasts => {
      if (prevToasts.length >= MAX_TOAST_STACK_SIZE) {
        const mostRecentToasts = prevToasts.slice(0, -1);
        return [{
          id,
          ...toast
        }, ...mostRecentToasts];
      }
      return [{
        id,
        ...toast
      }, ...prevToasts];
    });
    setTimeout(() => {
      setToasts(prevToasts => prevToasts.filter(t => t.id !== id));
    }, TOAST_DURATION_TIME);
    if (toast.hapticFeedback) {
      (0, _functions.triggerHaptic)(toast.hapticFeedback);
    }
    _reactNative.AccessibilityInfo.announceForAccessibilityWithOptions(toast.message, {
      queue: true
    });
    return id;
  }, []);
  const removeToast = (0, _react.useCallback)(id => {
    setToasts(prevToasts => prevToasts.filter(t => t.id !== id));
  }, []);
  const removeAllToasts = (0, _react.useCallback)(() => {
    setToasts([]);
  }, []);
  const contextValue = (0, _react.useMemo)(() => ({
    addToast: (0, _throttle.throttle)(addToast, TOAST_THROTTLE_TIME),
    removeToast: (0, _throttle.throttle)(removeToast, TOAST_THROTTLE_TIME),
    removeAllToasts
  }), [addToast, removeToast, removeAllToasts]);
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_context.ToastContext.Provider, {
    value: contextValue,
    children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(InitializeToastRef, {}), /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNativeSafeAreaContext.SafeAreaView, {
      style: styles.container,
      pointerEvents: "box-none",
      children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, {
        style: {
          padding: _core.IOVisualCostants.appMarginDefault
        },
        pointerEvents: "box-none",
        children: toasts.map(toast => /*#__PURE__*/(0, _jsxRuntime.jsx)(ToastNotificationStackItem, {
          ...toast,
          onDismiss: () => removeToast(toast.id)
        }, toast.id))
      })
    }), children]
  });
};
exports.ToastProvider = ToastProvider;
const InitializeToastRef = () => {
  const toast = (0, _useIOToast.useIOToast)();
  // eslint-disable-next-line functional/immutable-data
  _useIOToast.IOToastRef.current = toast;
  return null;
};
const styles = _reactNative.StyleSheet.create({
  container: {
    zIndex: 1000,
    position: "absolute",
    bottom: 0,
    left: 0,
    right: 0,
    top: 0,
    overflow: "visible"
  }
});
//# sourceMappingURL=ToastProvider.js.map