import * as b from "react";
var V = Object.defineProperty, w = (e, t, i) => t in e ? V(e, t, { enumerable: !0, configurable: !0, writable: !0, value: i }) : e[t] = i, u = (e, t, i) => w(e, typeof t != "symbol" ? t + "" : t, i), p = /* @__PURE__ */ new Map(), l = /* @__PURE__ */ new WeakMap(), f = 0, _ = void 0;
function I(e) {
  return e ? (l.has(e) || (f += 1, l.set(e, f.toString())), l.get(e)) : "0";
}
function y(e) {
  return Object.keys(e).sort().filter(
    (t) => e[t] !== void 0
  ).map((t) => `${t}_${t === "root" ? I(e.root) : e[t]}`).toString();
}
function m(e) {
  const t = y(e);
  let i = p.get(t);
  if (!i) {
    const r = /* @__PURE__ */ new Map();
    let d;
    const n = new IntersectionObserver((o) => {
      o.forEach((s) => {
        var h;
        const c = s.isIntersecting && d.some((a) => s.intersectionRatio >= a);
        e.trackVisibility && typeof s.isVisible > "u" && (s.isVisible = c), (h = r.get(s.target)) == null || h.forEach((a) => {
          a(c, s);
        });
      });
    }, e);
    d = n.thresholds || (Array.isArray(e.threshold) ? e.threshold : [e.threshold || 0]), i = {
      id: t,
      observer: n,
      elements: r
    }, p.set(t, i);
  }
  return i;
}
function M(e, t, i = {}, r = _) {
  if (typeof window.IntersectionObserver > "u" && r !== void 0) {
    const h = e.getBoundingClientRect();
    return t(r, {
      isIntersecting: r,
      target: e,
      intersectionRatio: typeof i.threshold == "number" ? i.threshold : 0,
      time: 0,
      boundingClientRect: h,
      intersectionRect: h,
      rootBounds: h
    }), () => {
    };
  }
  const { id: d, observer: n, elements: o } = m(i), s = o.get(e) || [];
  return o.has(e) || o.set(e, s), s.push(t), n.observe(e), function() {
    s.splice(s.indexOf(t), 1), s.length === 0 && (o.delete(e), n.unobserve(e)), o.size === 0 && (n.disconnect(), p.delete(d));
  };
}
function O(e) {
  return typeof e.children != "function";
}
var R = class extends b.Component {
  constructor(e) {
    super(e), u(this, "node", null), u(this, "_unobserveCb", null), u(this, "handleNode", (t) => {
      this.node && (this.unobserve(), !t && !this.props.triggerOnce && !this.props.skip && this.setState({ inView: !!this.props.initialInView, entry: void 0 })), this.node = t || null, this.observeNode();
    }), u(this, "handleChange", (t, i) => {
      t && this.props.triggerOnce && this.unobserve(), O(this.props) || this.setState({ inView: t, entry: i }), this.props.onChange && this.props.onChange(t, i);
    }), this.state = {
      inView: !!e.initialInView,
      entry: void 0
    };
  }
  componentDidMount() {
    this.unobserve(), this.observeNode();
  }
  componentDidUpdate(e) {
    (e.rootMargin !== this.props.rootMargin || e.root !== this.props.root || e.threshold !== this.props.threshold || e.skip !== this.props.skip || e.trackVisibility !== this.props.trackVisibility || e.delay !== this.props.delay) && (this.unobserve(), this.observeNode());
  }
  componentWillUnmount() {
    this.unobserve();
  }
  observeNode() {
    if (!this.node || this.props.skip) return;
    const {
      threshold: e,
      root: t,
      rootMargin: i,
      trackVisibility: r,
      delay: d,
      fallbackInView: n
    } = this.props;
    this._unobserveCb = M(
      this.node,
      this.handleChange,
      {
        threshold: e,
        root: t,
        rootMargin: i,
        // @ts-ignore
        trackVisibility: r,
        // @ts-ignore
        delay: d
      },
      n
    );
  }
  unobserve() {
    this._unobserveCb && (this._unobserveCb(), this._unobserveCb = null);
  }
  render() {
    const { children: e } = this.props;
    if (typeof e == "function") {
      const { inView: g, entry: C } = this.state;
      return e({ inView: g, entry: C, ref: this.handleNode });
    }
    const {
      as: t,
      triggerOnce: i,
      threshold: r,
      root: d,
      rootMargin: n,
      onChange: o,
      skip: s,
      trackVisibility: h,
      delay: c,
      initialInView: a,
      fallbackInView: N,
      ...v
    } = this.props;
    return b.createElement(
      t || "div",
      { ref: this.handleNode, ...v },
      e
    );
  }
};
export {
  R as InView,
  M as observe
};
//# sourceMappingURL=index.js.map
