var b = Object.defineProperty;
var f = (s, e, t) => e in s ? b(s, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : s[e] = t;
var o = (s, e, t) => f(s, typeof e != "symbol" ? e + "" : e, t);
import { isEqual as a } from "es-toolkit";
import { nextTick as d } from "vue";
const n = Symbol("v-viewable");
class u {
  constructor(e, t, i = {}) {
    o(this, "observer");
    o(this, "el");
    o(this, "frozen");
    o(this, "options");
    o(this, "oldResult");
    o(this, "callback");
    o(this, "modifiers");
    this.el = e, this.modifiers = i, this.observer = null, this.callback = null, this.frozen = !1, this.options = {
      callback: () => {
      },
      intersection: {}
    }, this.createObserver(t);
  }
  /**
   * Default threshold of 100%.
   * Informs when the element is 100% visibile, or at the provided threshold(s)
   * Does not support an array of values.
   * @see: https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API#thresholds
   */
  get threshold() {
    return this.options.intersection && typeof this.options.intersection.threshold == "number" ? this.options.intersection.threshold : 1;
  }
  get intersectionObserverOptions() {
    return {
      threshold: this.threshold,
      ...this.options.intersection
    };
  }
  createObserver(e) {
    this.observer && this.destroyObserver(), !this.frozen && (this.options = this.processOptions(e), this.callback = (t, i) => {
      var r;
      this.options.callback(t, i), t && ((r = this.modifiers) != null && r.once) && (this.frozen = !0, this.destroyObserver());
    }, this.oldResult = void 0, "IntersectionObserver" in window && (this.observer = new IntersectionObserver((t) => {
      let i = t[0];
      if (t.length > 1) {
        const r = t.find((l) => l.isIntersecting);
        r && (i = r);
      }
      if (this.callback) {
        const r = i.isIntersecting && i.intersectionRatio >= this.threshold;
        if (r === this.oldResult)
          return;
        this.oldResult = r, this.callback(r, i);
      }
    }, this.intersectionObserverOptions)), d(() => {
      this.observer && this.observer.observe(this.el);
    }));
  }
  destroyObserver() {
    this.observer && (this.observer.unobserve(this.el), this.observer.disconnect(), this.observer = null);
  }
  processOptions(e) {
    return typeof e == "function" ? { callback: e } : e;
  }
}
function h(s, { modifiers: e = {}, value: t }) {
  var r;
  if (!t)
    return;
  if (((r = t.intersection) == null ? void 0 : r.threshold) instanceof Array)
    throw new Error("`threshold` must be a number. v-viewable does not support `threshold` as an array.");
  const i = new u(s, t, e);
  s[n] = i;
}
function p(s, { modifiers: e, value: t, oldValue: i }) {
  if (a(t, i) || t.toString() === i.toString())
    return;
  const r = s[n];
  if (!t) {
    c(s);
    return;
  }
  r ? r.createObserver(t) : h(s, { modifiers: e, value: t });
}
function c(s) {
  const e = s[n];
  e && (e.destroyObserver(), delete s[n]);
}
const y = {
  beforeMount: h,
  updated: p,
  unmounted: c
};
export {
  y as default
};
//# sourceMappingURL=viewable.js.map
