'use strict'
require('@babel/polyfill')

var bulkAddEventListener = function bulkAddEventListener (object, events, callback) {
  events.forEach(function (event) {
    object.addEventListener(event, callback)
  })
}

var bulkRemoveEventListener = function bulkRemoveEventListener (object, events, callback) {
  events.forEach(function (event) {
    object.removeEventListener(event, callback)
  })
}

class IdleJs {
  constructor (options) {
    this.defaults = {
      idle: 10000, // idle time in ms
      events: ['mousemove', 'keydown', 'mousedown', 'touchstart'], // events that will trigger the idle resetter
      onIdle: function () {}, // callback function to be executed after idle time
      onActive: function () {}, // callback function to be executed after back form idleness
      onHide: function () {}, // callback function to be executed when window become hidden
      onShow: function () {}, // callback function to be executed when window become visible
      keepTracking: true, // set it to false of you want to track only once
      startAtIdle: false, // set it to true if you want to start in the idle state
      recurIdleCall: false
    }
    this.settings = Object.assign({}, this.defaults, options)
    this.visibilityEvents = ['visibilitychange', 'webkitvisibilitychange', 'mozvisibilitychange', 'msvisibilitychange']
    this.lastId = null

    this.reset()

    this.stopListener = (event) => {
      this.stop()
    }
    this.idlenessEventsHandler = (event) => {
      this.lastId = this.resetTimeout(this.lastId, this.settings)
    }
    this.visibilityEventsHandler = (event) => {
      if (document.hidden || document.webkitHidden || document.mozHidden || document.msHidden) {
        if (this.visible) {
          this.visible = false
          this.settings.onHide.call()
        }
      } else {
        if (!this.visible) {
          this.visible = true
          this.settings.onShow.call()
        }
      }
    }
  }

  resetTimeout (id, settings, keepTracking = this.settings.keepTracking) {
    if (this.idle) {
      this.idle = false
      settings.onActive.call()
    }
    if (id) {
      clearTimeout(id)
    }
    if (keepTracking) {
      return this.timeout(this.settings)
    }
  }

  timeout (settings) {
    var timer = (this.settings.recurIdleCall) ? setInterval : setTimeout
    var id
    id = timer(function () {
      this.idle = true
      this.settings.onIdle.call()
    }.bind(this), this.settings.idle)
    return id
  }

  start () {
    window.addEventListener('idle:stop', this.stopListener)
    this.lastId = this.timeout(this.settings)

    bulkAddEventListener(window, this.settings.events, this.idlenessEventsHandler);

    if (this.settings.onShow || this.settings.onHide) {
      bulkAddEventListener(document, this.visibilityEvents, this.visibilityEventsHandler);
    }

    return this
  }

  stop () {
    window.removeEventListener('idle:stop', this.stopListener)

    bulkRemoveEventListener(window, this.settings.events, this.idlenessEventsHandler)
    this.lastId = this.resetTimeout(this.lastId, this.settings, false)

    if (this.settings.onShow || this.settings.onHide) {
      bulkRemoveEventListener(document, this.visibilityEvents, this.visibilityEventsHandler)
    }

    return this
  }

  reset ({ idle = this.settings.startAtIdle, visible = !this.settings.startAtIdle } = {}) {
    this.idle = idle
    this.visible = visible

    return this
  }
}

module.exports = IdleJs

@@ -1350,22 +1350,28 @@
    this.
-lastId
+clearTimeout
  = null%0A
@@ -1511,48 +1511,21 @@
 his.
-lastId = this.resetTimeout(this.lastId, 
+resetTimeout(
 this
@@ -1954,12 +1954,8 @@
 ut (
-id, 
 sett
@@ -2095,18 +2095,33 @@
     if (
-id
+this.clearTimeout
 ) %7B%0A    
@@ -2122,16 +2122,21 @@
 %7B%0A      
+this.
 clearTim
@@ -2144,11 +2144,40 @@
 out(
-id)
+)%0A      this.clearTimeout = null
 %0A   
@@ -2205,31 +2205,24 @@
 ng) %7B%0A      
-return 
 this.timeout
@@ -2323,20 +2323,81 @@
 ) ? 
-setInterval 
+%7B%0A      set: setInterval,%0A      clear: clearInterval,%0A    %7D : %7B%0A      set
 : se
@@ -2408,23 +2408,52 @@
 eout
+,
 %0A    
-var id%0A   
+  clear: clearTimeout,%0A    %7D;%0A%0A    var
  id 
@@ -2459,16 +2459,20 @@
  = timer
+.set
 (functio
@@ -2577,21 +2577,55 @@
 le)%0A
+%0A
     
-return id
+this.clearTimeout = () =%3E timer.clear(id);
 %0A  %7D
@@ -2698,38 +2698,24 @@
 istener)%0A   
- this.lastId =
  this.timeou
@@ -3167,48 +3167,21 @@
 his.
-lastId = this.resetTimeout(this.lastId, 
+resetTimeout(
 this

Replace lastId with a clearTimeout function