import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
// A mock IntersectionObserver, which, when the `observe()` method is called,
// immediately calls the callback which was passed to the constructor, with mocked entries.
// Changing the MockEntries will cause the IntersectionObserverCallback to be called again
// with the new values.
export var MockIntersectionObserverFactory = function MockIntersectionObserverFactory(mockIntersectionObserverOpts) {
  return /*#__PURE__*/function () {
    function MockIntersectionObserver(callback) {
      var _this = this;
      _classCallCheck(this, MockIntersectionObserver);
      _defineProperty(this, "isDisconnected", false);
      _defineProperty(this, "previousMockEntries", []);
      _defineProperty(this, "callIntersectionObserverCallback", function () {
        _this.callback(mockIntersectionObserverOpts.getMockEntries(), _this);
      });
      _defineProperty(this, "checkIntersection", function () {
        var mockEntries = mockIntersectionObserverOpts.getMockEntries();
        if (_this.previousMockEntries !== mockEntries) {
          _this.previousMockEntries = mockEntries;
          _this.callIntersectionObserverCallback();
        }
        if (!_this.isDisconnected) {
          setTimeout(_this.checkIntersection, 100);
        }
      });
      _defineProperty(this, "disconnect", function () {
        _this.isDisconnected = true;
        mockIntersectionObserverOpts.disconnect();
      });
      _defineProperty(this, "takeRecords", mockIntersectionObserverOpts.takeRecords || jest.fn());
      _defineProperty(this, "unobserve", mockIntersectionObserverOpts.unobserve || jest.fn());
      this.callback = callback;
    }
    return _createClass(MockIntersectionObserver, [{
      key: "observe",
      value: function observe(_element) {
        this.previousMockEntries = mockIntersectionObserverOpts.getMockEntries();
        this.callIntersectionObserverCallback();
        setTimeout(this.checkIntersection, 100);
      }
    }]);
  }();
};
export var mockSimpleIntersectionObserver = function mockSimpleIntersectionObserver() {
  var MockIntersectionObserver = /*#__PURE__*/function () {
    function MockIntersectionObserver(callback) {
      _classCallCheck(this, MockIntersectionObserver);
      _defineProperty(this, "disconnect", jest.fn());
      _defineProperty(this, "takeRecords", jest.fn());
      _defineProperty(this, "unobserve", jest.fn());
      this.callback = callback;
    }
    return _createClass(MockIntersectionObserver, [{
      key: "observe",
      value: function observe(_element) {
        var entries = [{
          isIntersecting: true,
          intersectionRatio: 1
        }];
        this.callback(entries, this);
      }
    }]);
  }();
  Object.defineProperty(window, 'IntersectionObserver', {
    writable: true,
    configurable: true,
    value: MockIntersectionObserver
  });
};