import { proxyCustomElement, HTMLElement, createEvent, h, Host } from '@stencil/core/internal/client';

var __classPrivateFieldGet = (undefined && undefined.__classPrivateFieldGet) || function (receiver, state, kind, f) {
  if (kind === "a" && !f)
    throw new TypeError("Private accessor was defined without a getter");
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver))
    throw new TypeError("Cannot read private member from an object whose class did not declare it");
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var __classPrivateFieldSet = (undefined && undefined.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
  if (kind === "m")
    throw new TypeError("Private method is not writable");
  if (kind === "a" && !f)
    throw new TypeError("Private accessor was defined without a setter");
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver))
    throw new TypeError("Cannot write private member to an object whose class did not declare it");
  return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var _KlevuUtilInfiniteScroll_instances, _KlevuUtilInfiniteScroll_loadMoreCount, _KlevuUtilInfiniteScroll_observer, _KlevuUtilInfiniteScroll_loadMore, _KlevuUtilInfiniteScroll_attachIntersectionObserver, _KlevuUtilInfiniteScroll_detachIntersectionObserver;
const KlevuUtilInfiniteScroll = /*@__PURE__*/ proxyCustomElement(class KlevuUtilInfiniteScroll extends HTMLElement {
  constructor() {
    super();
    this.__registerHost();
    this.klevuLoadMore = createEvent(this, "klevuLoadMore", 7);
    this.klevuInfiniteScrollingPaused = createEvent(this, "klevuInfiniteScrollingPaused", 7);
    _KlevuUtilInfiniteScroll_instances.add(this);
    _KlevuUtilInfiniteScroll_loadMoreCount.set(this, 1);
    _KlevuUtilInfiniteScroll_observer.set(this, null);
    _KlevuUtilInfiniteScroll_loadMore.set(this, () => {
      var _a;
      if (this.infiniteScrollPauseThreshold === 0) {
        this.klevuLoadMore.emit();
        return;
      }
      if (__classPrivateFieldGet(this, _KlevuUtilInfiniteScroll_loadMoreCount, "f") % this.infiniteScrollPauseThreshold === 0) {
        this.klevuInfiniteScrollingPaused.emit();
      }
      else {
        this.klevuLoadMore.emit();
      }
      __classPrivateFieldSet(this, _KlevuUtilInfiniteScroll_loadMoreCount, (_a = __classPrivateFieldGet(this, _KlevuUtilInfiniteScroll_loadMoreCount, "f"), _a++, _a), "f");
    });
    this.infiniteScrollPauseThreshold = 3;
    this.enabled = false;
  }
  onEnableToggled() {
    if (this.enabled) {
      __classPrivateFieldGet(this, _KlevuUtilInfiniteScroll_instances, "m", _KlevuUtilInfiniteScroll_attachIntersectionObserver).call(this);
    }
    else {
      __classPrivateFieldGet(this, _KlevuUtilInfiniteScroll_instances, "m", _KlevuUtilInfiniteScroll_detachIntersectionObserver).call(this);
    }
  }
  disconnectedCallback() {
    __classPrivateFieldGet(this, _KlevuUtilInfiniteScroll_instances, "m", _KlevuUtilInfiniteScroll_detachIntersectionObserver).call(this);
  }
  connectedCallback() {
    if (this.enabled) {
      // To avoid triggering load more immediately
      setTimeout(() => {
        __classPrivateFieldGet(this, _KlevuUtilInfiniteScroll_instances, "m", _KlevuUtilInfiniteScroll_attachIntersectionObserver).call(this);
      }, 100);
    }
  }
  render() {
    return (h(Host, null, h("div", null)));
  }
  get el() { return this; }
  static get watchers() { return {
    "enabled": ["onEnableToggled"]
  }; }
}, [0, "klevu-util-infinite-scroll", {
    "infiniteScrollPauseThreshold": [2, "infinite-scroll-pause-threshold"],
    "enabled": [4]
  }, undefined, {
    "enabled": ["onEnableToggled"]
  }]);
_KlevuUtilInfiniteScroll_loadMoreCount = new WeakMap(), _KlevuUtilInfiniteScroll_observer = new WeakMap(), _KlevuUtilInfiniteScroll_loadMore = new WeakMap(), _KlevuUtilInfiniteScroll_instances = new WeakSet(), _KlevuUtilInfiniteScroll_attachIntersectionObserver = function _KlevuUtilInfiniteScroll_attachIntersectionObserver() {
  __classPrivateFieldGet(this, _KlevuUtilInfiniteScroll_instances, "m", _KlevuUtilInfiniteScroll_detachIntersectionObserver).call(this);
  __classPrivateFieldSet(this, _KlevuUtilInfiniteScroll_observer, new IntersectionObserver((entries) => {
    if (entries[0].isIntersecting) {
      __classPrivateFieldGet(this, _KlevuUtilInfiniteScroll_loadMore, "f").call(this);
    }
  }), "f");
  if (this.el && __classPrivateFieldGet(this, _KlevuUtilInfiniteScroll_observer, "f")) {
    __classPrivateFieldGet(this, _KlevuUtilInfiniteScroll_observer, "f").observe(this.el);
  }
}, _KlevuUtilInfiniteScroll_detachIntersectionObserver = function _KlevuUtilInfiniteScroll_detachIntersectionObserver() {
  if (this.el && __classPrivateFieldGet(this, _KlevuUtilInfiniteScroll_observer, "f")) {
    __classPrivateFieldGet(this, _KlevuUtilInfiniteScroll_observer, "f").unobserve(this.el);
  }
};
function defineCustomElement() {
  if (typeof customElements === "undefined") {
    return;
  }
  const components = ["klevu-util-infinite-scroll"];
  components.forEach(tagName => { switch (tagName) {
    case "klevu-util-infinite-scroll":
      if (!customElements.get(tagName)) {
        customElements.define(tagName, KlevuUtilInfiniteScroll);
      }
      break;
  } });
}
defineCustomElement();

export { KlevuUtilInfiniteScroll as K, defineCustomElement as d };

//# sourceMappingURL=klevu-util-infinite-scroll2.js.map