'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.CountdownNotifier = undefined;

var _notifCenter = require('views/env-parts/notif-center');

var _notifCenter2 = _interopRequireDefault(_notifCenter);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

// Notification that supports equalKey.
// completeTime: unix ms.
// preemptTime: seconds.
class CountdownNotifier {
  constructor() {
    this.tryNotify = o => {
      if (o.completeTime != null && this._lastCompleteTime != null && o.completeTime === this._lastCompleteTime) {
        return;
      }
      // 1 second more to preemptTime bc notifications are throttled by 1 sec
      const preemptTime = Math.max((o.preemptTime || 0) + 1, 0);
      if (o.completeTime <= Date.now() + preemptTime * 1000) {
        this._lastCompleteTime = o.completeTime;
        if (!this._justNotified) {
          _notifCenter2.default.notify(o);
          this._justNotified = true;
        }
      } else {
        this._justNotified = false;
      }
    };

    this._lastCompleteTime = null;
    // Two notif must be separated by at least one non-notif call to tryNotify
    this._justNotified = false;
  }
}
exports.CountdownNotifier = CountdownNotifier;