import { env as I } from "../../../utils/process.es.js";
import { useRef as b, useEffect as d } from "react";
import { AnimationType as m } from "../../../render/utils/types.es.js";
import { warnOnce as h } from "../../../utils/warn-once.es.js";
import { observeIntersection as g } from "./observers.es.js";
function S(e) {
  var o = e.visualElement, t = e.whileInView, a = e.onViewportEnter, n = e.onViewportLeave, s = e.viewport, i = s === void 0 ? {} : s, r = b({
    hasEnteredView: !1,
    isInView: !1
  }), u = Boolean(t || a || n);
  i.once && r.current.hasEnteredView && (u = !1);
  var l = typeof IntersectionObserver > "u" ? k : O;
  l(u, r.current, o, i);
}
var E = {
  some: 0,
  all: 1
};
function O(e, o, t, a) {
  var n = a.root, s = a.margin, i = a.amount, r = i === void 0 ? "some" : i, u = a.once;
  d(function() {
    if (!!e) {
      var l = {
        root: n == null ? void 0 : n.current,
        rootMargin: s,
        threshold: typeof r == "number" ? r : E[r]
      }, V = function(w) {
        var c, v = w.isIntersecting;
        if (o.isInView !== v && (o.isInView = v, !(u && !v && o.hasEnteredView))) {
          v && (o.hasEnteredView = !0), (c = t.animationState) === null || c === void 0 || c.setActive(m.InView, v);
          var p = t.getProps(), f = v ? p.onViewportEnter : p.onViewportLeave;
          f == null || f(w);
        }
      };
      return g(t.getInstance(), l, V);
    }
  }, [e, n, s, r]);
}
function k(e, o, t, a) {
  var n = a.fallback, s = n === void 0 ? !0 : n;
  d(function() {
    !e || !s || (I !== "production" && h(!1, "IntersectionObserver not available on this device. whileInView animations will trigger on mount."), requestAnimationFrame(function() {
      var i;
      o.hasEnteredView = !0;
      var r = t.getProps().onViewportEnter;
      r == null || r(null), (i = t.animationState) === null || i === void 0 || i.setActive(m.InView, !0);
    }));
  }, [e]);
}
export {
  S as useViewport
};
//# sourceMappingURL=use-viewport.es.js.map
