import { useEditorRef as b } from "@udecode/plate-core";
import { useFloatingLinkSelectors as I, floatingLinkActions as S } from "@udecode/plate-link";
import { useState as T, useCallback as f, useEffect as m } from "react";
import { FLOATING_MODAL_SELECTOR as h } from "./components/FloatingModal/FloatingModal.es.js";
const x = ({
  hideExternalFloatingModals: n
}) => {
  const e = b(), i = I().isOpen(e.id), [c, d] = T(!1), o = f(() => {
    i && S.hide(), n && n(e.id);
  }, [i, n, e]), l = f(() => {
    o(), e.deselect(), e.collapse();
  }, [e, o]);
  return m(() => {
    if (!c)
      return;
    const t = (r) => {
      const p = `[data-editor-id='${e.id}']`, v = "[role='toolbar']", E = "[data-is-underlay], [data-radix-popper-content-wrapper]", s = r.target, L = s.closest(E), O = s.closest(h), a = s.closest(p), u = s.closest(v);
      a && !O || u ? o() : !a && !u && !L && (l(), d(!1));
    };
    return document.addEventListener("pointerdown", t), document.addEventListener("focusin", t), () => {
      document.removeEventListener("focusin", t), document.removeEventListener("pointerdown", t);
    };
  }, [l, o, e.id, c]), m(() => {
    const t = document.querySelector(`[data-editor-id='${e.id}']`);
    if (!t)
      return;
    const r = () => {
      d(!0);
    };
    return t.addEventListener("focusin", r), () => {
      t.removeEventListener("focusin", r);
    };
  }, [e.id]), null;
};
export {
  x as BlurObserver
};
//# sourceMappingURL=BlurObserver.es.js.map
