"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const jsxRuntime = require("react/jsx-runtime");
const RadixToast = require("@radix-ui/react-toast");
const React = require("react");
const ReactDOM = require("react-dom");
const config_context = require("../config/config.context.cjs");
const toast_component = require("./toast.component.cjs");
require("./toast.theme.css.cjs");
function _interopNamespaceDefault(e) {
  const n = Object.create(null, { [Symbol.toStringTag]: { value: "Module" } });
  if (e) {
    for (const k in e) {
      if (k !== "default") {
        const d = Object.getOwnPropertyDescriptor(e, k);
        Object.defineProperty(n, k, d.get ? d : {
          enumerable: true,
          get: () => e[k]
        });
      }
    }
  }
  n.default = e;
  return Object.freeze(n);
}
const RadixToast__namespace = /* @__PURE__ */ _interopNamespaceDefault(RadixToast);
const React__namespace = /* @__PURE__ */ _interopNamespaceDefault(React);
const ReactDOM__namespace = /* @__PURE__ */ _interopNamespaceDefault(ReactDOM);
const initialContext = {
  addToast: () => {
    throw new Error("Unable to dispatch toast, are you sure you've added either the <ArmstrongProvider> or <ToastProvider>?");
  },
  dismissToastByKey: () => {
    throw new Error("Unable to dismiss toast, are you sure you've added either the <ArmstrongProvider> or <ToastProvider>?");
  }
};
const ToastContext = React__namespace.createContext(initialContext);
const ToastProvider = ({ children, duration, position, closeButtonIcon, displayMode, ignorePredicate }) => {
  const globals = config_context.useArmstrongConfig({
    toastDuration: duration,
    toastPosition: position,
    toastCloseButtonIcon: closeButtonIcon,
    toastDisplayMode: displayMode,
    toastIgnorePredicate: ignorePredicate
  });
  const nextToastKey = React__namespace.useRef(1);
  const [toasts, setToasts] = React__namespace.useState([]);
  const swipeDirection = globals.toastPosition === "bottom-left" || globals.toastPosition === "top-left" ? "left" : "right";
  const addToast = React__namespace.useCallback((newToast) => {
    var _a;
    if ((_a = globals.toastIgnorePredicate) == null ? void 0 : _a.call(globals, toasts.filter((t) => !t.exited).map(({ key: key2, exited, ...toast2 }) => toast2), newToast)) {
      return void 0;
    }
    const key = `toast-${nextToastKey.current}`;
    nextToastKey.current += 1;
    const toast = { ...newToast, key, exited: false };
    setToasts((prevToasts) => {
      if (globals.toastDisplayMode === "add") {
        return [...prevToasts, toast];
      }
      return [toast];
    });
    return key;
  }, [globals, toasts]);
  const removeToastByKey = React__namespace.useCallback((key) => {
    setToasts((prevToasts) => prevToasts.map((toast) => toast.key === key ? { ...toast, exited: true } : toast));
  }, []);
  const dismissToastByKey = React__namespace.useCallback((key) => {
    setToasts((prevToasts) => prevToasts.filter((toast) => toast.key !== key));
  }, []);
  return jsxRuntime.jsx(ToastContext.Provider, { value: { addToast, dismissToastByKey }, children: jsxRuntime.jsxs(RadixToast__namespace.Provider, { swipeDirection, duration: globals.toastDuration, children: [children, toasts.map(({ key, ...toast }) => jsxRuntime.jsx(toast_component.Toast, { duration: globals.toastDuration, position: globals.toastPosition, closeButtonIcon: globals.toastCloseButtonIcon, onExit: () => removeToastByKey(key), ...toast }, key)), globals.globalPortalTo && ReactDOM__namespace.createPortal(jsxRuntime.jsx(RadixToast__namespace.Viewport, { className: "arm-toast-viewport", "data-position": globals.toastPosition }), globals.globalPortalTo)] }) });
};
ToastProvider.displayName = "ToastProvider";
exports.ToastContext = ToastContext;
exports.ToastProvider = ToastProvider;
//# sourceMappingURL=toast.context.cjs.map
