/**
 * Mixin for busy.
 * @class ApBusyMixin
 */

'use strict'

import React, {PropTypes as types} from 'react'
import Apemantimer from 'apemantimer'

/** @lends ApBusyMixin */
const ApBusyMixin = {

  // --------------------
  // Custom
  // --------------------
  $apBusyMixed: true,

  busy: false,

  isBusy () {
    const s = this
    return s.busy
  },

  setBusy (busy) {
    const s = this
    s.busy = busy
  },

  busyWhile (duration) {
    const s = this
    let { _busyTimer } = s
    _busyTimer.cancelAll()
    _busyTimer.timeout(() => s.setBusy(false), duration)
    s.setBusy(true)
  },

  clearBusyWhile () {
    const s = this
    let { _busyTimer } = s
    _busyTimer.cancelAll()
  },

  // --------------------
  // Specs
  // --------------------

  componentWillMount () {
    const s = this
    s._busyTimer = Apemantimer.create()
  },

  componentWillUnmount () {
    const s = this
    s.clearBusyWhile()
  }

  // --------------------
  // Lifecycle
  // --------------------

}

export default Object.freeze(ApBusyMixin)
