"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.mockSimpleIntersectionObserver = exports.MockIntersectionObserverFactory = void 0;
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _defineProperty2 = _interopRequireDefault(require("@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.
var MockIntersectionObserverFactory = exports.MockIntersectionObserverFactory = function MockIntersectionObserverFactory(mockIntersectionObserverOpts) {
  return /*#__PURE__*/function () {
    function MockIntersectionObserver(callback) {
      var _this = this;
      (0, _classCallCheck2.default)(this, MockIntersectionObserver);
      (0, _defineProperty2.default)(this, "isDisconnected", false);
      (0, _defineProperty2.default)(this, "previousMockEntries", []);
      (0, _defineProperty2.default)(this, "callIntersectionObserverCallback", function () {
        _this.callback(mockIntersectionObserverOpts.getMockEntries(), _this);
      });
      (0, _defineProperty2.default)(this, "checkIntersection", function () {
        var mockEntries = mockIntersectionObserverOpts.getMockEntries();
        if (_this.previousMockEntries !== mockEntries) {
          _this.previousMockEntries = mockEntries;
          _this.callIntersectionObserverCallback();
        }
        if (!_this.isDisconnected) {
          setTimeout(_this.checkIntersection, 100);
        }
      });
      (0, _defineProperty2.default)(this, "disconnect", function () {
        _this.isDisconnected = true;
        mockIntersectionObserverOpts.disconnect();
      });
      (0, _defineProperty2.default)(this, "takeRecords", mockIntersectionObserverOpts.takeRecords || jest.fn());
      (0, _defineProperty2.default)(this, "unobserve", mockIntersectionObserverOpts.unobserve || jest.fn());
      this.callback = callback;
    }
    return (0, _createClass2.default)(MockIntersectionObserver, [{
      key: "observe",
      value: function observe(_element) {
        this.previousMockEntries = mockIntersectionObserverOpts.getMockEntries();
        this.callIntersectionObserverCallback();
        setTimeout(this.checkIntersection, 100);
      }
    }]);
  }();
};
var mockSimpleIntersectionObserver = exports.mockSimpleIntersectionObserver = function mockSimpleIntersectionObserver() {
  var MockIntersectionObserver = /*#__PURE__*/function () {
    function MockIntersectionObserver(callback) {
      (0, _classCallCheck2.default)(this, MockIntersectionObserver);
      (0, _defineProperty2.default)(this, "disconnect", jest.fn());
      (0, _defineProperty2.default)(this, "takeRecords", jest.fn());
      (0, _defineProperty2.default)(this, "unobserve", jest.fn());
      this.callback = callback;
    }
    return (0, _createClass2.default)(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
  });
};