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 const MockIntersectionObserverFactory = mockIntersectionObserverOpts => class MockIntersectionObserver {
  constructor(callback) {
    _defineProperty(this, "isDisconnected", false);
    _defineProperty(this, "previousMockEntries", []);
    _defineProperty(this, "callIntersectionObserverCallback", () => {
      this.callback(mockIntersectionObserverOpts.getMockEntries(), this);
    });
    _defineProperty(this, "checkIntersection", () => {
      const mockEntries = mockIntersectionObserverOpts.getMockEntries();
      if (this.previousMockEntries !== mockEntries) {
        this.previousMockEntries = mockEntries;
        this.callIntersectionObserverCallback();
      }
      if (!this.isDisconnected) {
        setTimeout(this.checkIntersection, 100);
      }
    });
    _defineProperty(this, "disconnect", () => {
      this.isDisconnected = true;
      mockIntersectionObserverOpts.disconnect();
    });
    _defineProperty(this, "takeRecords", mockIntersectionObserverOpts.takeRecords || jest.fn());
    _defineProperty(this, "unobserve", mockIntersectionObserverOpts.unobserve || jest.fn());
    this.callback = callback;
  }
  observe(_element) {
    this.previousMockEntries = mockIntersectionObserverOpts.getMockEntries();
    this.callIntersectionObserverCallback();
    setTimeout(this.checkIntersection, 100);
  }
};
export const mockSimpleIntersectionObserver = () => {
  class MockIntersectionObserver {
    constructor(callback) {
      _defineProperty(this, "disconnect", jest.fn());
      _defineProperty(this, "takeRecords", jest.fn());
      _defineProperty(this, "unobserve", jest.fn());
      this.callback = callback;
    }
    observe(_element) {
      const entries = [{
        isIntersecting: true,
        intersectionRatio: 1
      }];
      this.callback(entries, this);
    }
  }
  Object.defineProperty(window, 'IntersectionObserver', {
    writable: true,
    configurable: true,
    value: MockIntersectionObserver
  });
};