"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.ScrollAwayScheduleStrategy = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _mobx = require("mobx");
var _events = require("events");
class ScrollAwayScheduleStrategy {
  constructor(state) {
    (0, _defineProperty2.default)(this, "state", void 0);
    (0, _defineProperty2.default)(this, "events", new _events.EventEmitter());
    this.state = state;
    (0, _mobx.makeObservable)(this, {
      preventEnter: _mobx.action.bound,
      preventExit: _mobx.action.bound,
      onIntersectionRatioChanged: _mobx.action.bound
    });
  }
  _scheduleEnter() {
    const {
      topNotification
    } = this.state;
    const {
      nextData
    } = topNotification;
    this.events.removeAllListeners('enter');
    this.events.once('enter', (0, _mobx.action)(() => {
      if (nextData === topNotification.nextData) {
        topNotification.set(nextData);
      }
    }));
  }
  preventEnter() {
    const {
      topNotification,
      intersection,
      consideredVisibleRatio
    } = this.state;
    if (intersection.intersectionRatio < consideredVisibleRatio) {
      topNotification.show = false;
      // console.log('enter prevented');
      this._scheduleEnter();
    }
  }
  preventExit() {
    const {
      topNotification,
      intersection,
      consideredVisibleRatio
    } = this.state;
    if (intersection.intersectionRatio < consideredVisibleRatio) {
      topNotification.show = true;
      // console.log('exit prevented');
      this._scheduleEnter();
    }
  }
  onIntersectionRatioChanged(ratio) {
    const {
      consideredVisibleRatio,
      topNotification
    } = this.state;
    // when scrolling in trigger enter
    if (ratio >= consideredVisibleRatio) {
      this.events.emit('enter');
      topNotification.events.off('beforeExit', this.preventExit);
      topNotification.events.off('enter', this.preventEnter);
    }
    // when scrolling out schedule prevention
    else {
      topNotification.events.off('beforeExit', this.preventExit);
      topNotification.events.off('enter', this.preventEnter);
      if (topNotification.show) {
        topNotification.events.on('beforeExit', this.preventExit);
      } else {
        topNotification.events.on('enter', this.preventEnter);
      }
    }
  }
  init() {}
}
exports.ScrollAwayScheduleStrategy = ScrollAwayScheduleStrategy;
//# sourceMappingURL=ScrollAwayScheduleStrategy.js.map