import { jsx } from 'react/jsx-runtime';
import { forwardRef, useRef, useState, useEffect } from 'react';
import { T as Toaster$1 } from './Toaster-Bq42x6Gx.js';
import { u as useMergeRefs } from './useMergeRefs-Dfmtq9cI.js';

const SHADOW_HOST_DATA_ATTRS = "[data-mfe-portal-name], [data-mfe-name]";
const Toaster = forwardRef(
  ({ onToastEvent, ...props }, ref) => {
    const { id } = props;
    const toasterRef = useRef(null);
    const mergedToasterRef = useMergeRefs([toasterRef, ref]);
    const [shouldDisableEvents, setShouldDisableEvents] = useState(false);
    useEffect(() => {
      const toasters = document.querySelectorAll("[data-anv='toaster']");
      const shadowToasters = toasterRef.current?.getRootNode()?.querySelectorAll("[data-anv='toaster']") || [];
      if (toasters.length && id !== toasters[0].getAttribute("data-toaster-id")) {
        setShouldDisableEvents(true);
      } else if (
        // Toaster(s) exist in the shadow dom, bail out if this toaster is not the first one
        shadowToasters.length && id !== shadowToasters[0].getAttribute("data-toaster-id")
      ) {
        setShouldDisableEvents(true);
      }
    }, [id]);
    const a1ToastGroupObserver = useRef(null);
    const a1ToastGroupObserverConnected = useRef(false);
    const [a1ToastGroups, setA1ToastGroups] = useState([]);
    const [toastCount, setToastCount] = useState(0);
    const handleToastEvent = (event) => {
      const { toastCount: newToastCount } = event;
      setToastCount(newToastCount);
      onToastEvent?.(event);
    };
    const updateA1ToastGroups = () => {
      const mfeRoots = document.querySelectorAll(SHADOW_HOST_DATA_ATTRS);
      const a1ToastGroupsLocal = [
        ...document.getElementsByClassName("ToastGroup")
      ];
      if (mfeRoots.length > 0) {
        mfeRoots.forEach((el) => {
          setTimeout(() => {
            if (el.shadowRoot) {
              a1ToastGroupsLocal.push(
                ...el.shadowRoot.querySelectorAll(".ToastGroup")
              );
            }
            setA1ToastGroups(a1ToastGroupsLocal);
          });
        });
      } else {
        setA1ToastGroups(a1ToastGroupsLocal);
      }
    };
    useEffect(() => {
      a1ToastGroupObserver.current = new MutationObserver(updateA1ToastGroups);
    }, []);
    useEffect(() => {
      if (a1ToastGroupObserver.current !== null) {
        if (toastCount > 0 && a1ToastGroupObserverConnected.current === false) {
          a1ToastGroupObserver.current.observe(document.body, {
            childList: true,
            subtree: true
          });
          a1ToastGroupObserverConnected.current = true;
          updateA1ToastGroups();
        } else if (toastCount === 0 && a1ToastGroupObserverConnected.current === true) {
          a1ToastGroupObserver.current.disconnect();
          a1ToastGroupObserverConnected.current = false;
        }
      }
    }, [toastCount]);
    useEffect(() => {
      if (a1ToastGroups && a1ToastGroups.length > 0) {
        const toasterHeight = toasterRef.current?.firstElementChild?.getBoundingClientRect().height;
        a1ToastGroups.forEach((group) => {
          group.style.top = toastCount === 0 ? "16px" : `calc(${toasterHeight}px + 2.5rem)`;
        });
      }
    }, [a1ToastGroups.length, toastCount]);
    return /* @__PURE__ */ jsx(
      Toaster$1,
      {
        ...props,
        onToastEvent: handleToastEvent,
        disableEvents: shouldDisableEvents,
        ref: mergedToasterRef
      }
    );
  }
);
Toaster.displayName = "Toaster";

export { Toaster as T };
//# sourceMappingURL=Toaster-Dly3rfva.js.map
