var __classPrivateFieldGet = (this && this.__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 = (this && this.__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;
import { Host, h } from "@stencil/core";
/**
 * Component that triggers event when intercepted on scroll of page.
 */
export class KlevuUtilInfiniteScroll {
  constructor() {
    _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)));
  }
  static get is() { return "klevu-util-infinite-scroll"; }
  static get properties() {
    return {
      "infiniteScrollPauseThreshold": {
        "type": "number",
        "mutable": false,
        "complexType": {
          "original": "number",
          "resolved": "number",
          "references": {}
        },
        "required": false,
        "optional": false,
        "docs": {
          "tags": [],
          "text": "The number of pages after which triggers infiniteScrollingPaused event.\nListen to this event to allow further loading on user input."
        },
        "attribute": "infinite-scroll-pause-threshold",
        "reflect": false,
        "defaultValue": "3"
      },
      "enabled": {
        "type": "boolean",
        "mutable": false,
        "complexType": {
          "original": "boolean",
          "resolved": "boolean",
          "references": {}
        },
        "required": false,
        "optional": false,
        "docs": {
          "tags": [],
          "text": "Whether infinite scrolling is enabled"
        },
        "attribute": "enabled",
        "reflect": false,
        "defaultValue": "false"
      }
    };
  }
  static get events() {
    return [{
        "method": "klevuLoadMore",
        "name": "klevuLoadMore",
        "bubbles": true,
        "cancelable": true,
        "composed": true,
        "docs": {
          "tags": [],
          "text": "Event emitted when infinite scroll element is intercepted"
        },
        "complexType": {
          "original": "void",
          "resolved": "void",
          "references": {}
        }
      }, {
        "method": "klevuInfiniteScrollingPaused",
        "name": "klevuInfiniteScrollingPaused",
        "bubbles": true,
        "cancelable": true,
        "composed": true,
        "docs": {
          "tags": [],
          "text": "Event emitted when infinite loading reaches a multiple of infiniteScrollPauseThreshold"
        },
        "complexType": {
          "original": "void",
          "resolved": "void",
          "references": {}
        }
      }];
  }
  static get elementRef() { return "el"; }
  static get watchers() {
    return [{
        "propName": "enabled",
        "methodName": "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);
  }
};
//# sourceMappingURL=klevu-util-infinite-scroll.js.map
