import { useEffect as x } from "react";
function h(f) {
  const {
    top: e,
    height: c
  } = f.getBoundingClientRect(), u = window.innerHeight - e, n = window.innerHeight + c;
  return u / n;
}
function S(f, e, c) {
  const {
    top: u,
    left: n,
    width: d,
    height: g
  } = f.getBoundingClientRect(), o = (e - n) / d, l = (c - u) / g;
  return {
    x: o,
    y: l
  };
}
const A = ({
  wrapperRef: f,
  animationItem: e,
  mode: c,
  actions: u
}) => {
  x(() => {
    const n = f.current;
    if (!n || !e || !u.length)
      return;
    e.stop();
    const d = () => {
      let o = null;
      const l = () => {
        const a = h(n), r = u.find(({
          visibility: i
        }) => i && a >= i[0] && a <= i[1]);
        if (r) {
          if (r.type === "seek" && r.visibility && r.frames.length === 2) {
            const i = r.frames[0] + Math.ceil((a - r.visibility[0]) / (r.visibility[1] - r.visibility[0]) * r.frames[1]);
            e.goToAndStop(i - e.firstFrame - 1, !0);
          }
          r.type === "loop" && (o === null || o !== r.frames || e.isPaused) && (e.playSegments(r.frames, !0), o = r.frames), r.type === "play" && e.isPaused && (e.resetSegments(!0), e.play()), r.type === "stop" && e.goToAndStop(r.frames[0] - e.firstFrame - 1, !0);
        }
      };
      return document.addEventListener("scroll", l), () => {
        document.removeEventListener("scroll", l);
      };
    }, g = () => {
      const o = (r, i) => {
        let y = r, p = i;
        if (y !== -1 && p !== -1) {
          const s = S(n, y, p);
          y = s.x, p = s.y;
        }
        const t = u.find(({
          position: s
        }) => s && Array.isArray(s.x) && Array.isArray(s.y) ? y >= s.x[0] && y <= s.x[1] && p >= s.y[0] && p <= s.y[1] : s && !Number.isNaN(s.x) && !Number.isNaN(s.y) ? y === s.x && p === s.y : !1);
        if (t) {
          if (t.type === "seek" && t.position && Array.isArray(t.position.x) && Array.isArray(t.position.y) && t.frames.length === 2) {
            const s = (y - t.position.x[0]) / (t.position.x[1] - t.position.x[0]), v = (p - t.position.y[0]) / (t.position.y[1] - t.position.y[0]);
            e.playSegments(t.frames, !0), e.goToAndStop(Math.ceil((s + v) / 2 * (t.frames[1] - t.frames[0])), !0);
          }
          t.type === "loop" && e.playSegments(t.frames, !0), t.type === "play" && (e.isPaused && e.resetSegments(!1), e.playSegments(t.frames)), t.type === "stop" && e.goToAndStop(t.frames[0], !0);
        }
      }, l = (r) => {
        o(r.clientX, r.clientY);
      }, a = () => {
        o(-1, -1);
      };
      return n.addEventListener("mousemove", l), n.addEventListener("mouseout", a), () => {
        n.removeEventListener("mousemove", l), n.removeEventListener("mouseout", a);
      };
    };
    switch (c) {
      case "scroll":
        return d();
      case "cursor":
        return g();
    }
  }, [c, e]);
}, w = ({
  actions: f,
  mode: e,
  lottieObj: c
}) => {
  const {
    animationItem: u,
    View: n,
    animationContainerRef: d
  } = c;
  return A({
    actions: f,
    animationItem: u,
    mode: e,
    wrapperRef: d
  }), n;
};
export {
  S as getContainerCursorPosition,
  h as getContainerVisibility,
  A as useInitInteractivity,
  w as useLottieInteractivity
};
