import { useEffect as u } from "react";
import { disposables as f } from "../utils/disposables.js";
import { useLatestValue as d } from "./use-latest-value.js";
function p(i, r, o) {
  let s = d((t) => {
    let e = t.getBoundingClientRect();
    e.x === 0 && e.y === 0 && e.width === 0 && e.height === 0 && o();
  });
  u(() => {
    if (!i) return;
    let t = r === null ? null : r instanceof HTMLElement ? r : r.current;
    if (!t) return;
    let e = f();
    if (typeof ResizeObserver < "u") {
      let n = new ResizeObserver(() => s.current(t));
      n.observe(t), e.add(() => n.disconnect());
    }
    if (typeof IntersectionObserver < "u") {
      let n = new IntersectionObserver(() => s.current(t));
      n.observe(t), e.add(() => n.disconnect());
    }
    return () => e.dispose();
  }, [r, s, i]);
}
export {
  p as useOnDisappear
};
