import Vue from 'vue'
import Queue from 'js-queue'

function beginTransition () {
  const queue = this
  queue.clear()
  queue.stop = true
}

function addTransitions (collection = [], iteratee, delay = 800) {
  const queue = this
  collection.forEach((value, index) => {
    queue.add(() => {
      Promise.resolve(iteratee(value, index))
        .then(() => {
          setTimeout(() => {
            queue.next()
          }, delay)
        })
    })
  })
}

function endTransition () {
  const queue = this
  queue.stop = false
  queue.next()
}

function SntQueue () {
  Object.defineProperties(
    this,
    {
      beginTransition: {
        enumerable: true,
        writable: false,
        value: beginTransition
      },
      forEach: {
        enumerable: true,
        writable: false,
        value: addTransitions
      },
      endTransition: {
        enumerable: true,
        writable: false,
        value: endTransition
      }
    }
  )
}

SntQueue.prototype = new Queue()
Vue.prototype.$queue = new SntQueue()
