/**
 * Mixin for scroll.
 * @mixin ApScrollMixin
 */

'use strict'

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

/** @lends ApScrollMixin */
let ApScrollMixin = {

  // --------------------
  // Custom
  // --------------------
  $apScrollMixed: true,

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

  /**
   * Refresh iscroll size.
   * @param {number} [delay=0]
   */
  refreshIScroll (delay) {
    const s = this
    clearTimeout(s._iScrollRefreshTimer)
    s._iScrollRefreshTimer = setTimeout(() => {
      let iScroll = s._iScroll

      if (iScroll) {
        iScroll.refresh()
      }
    }, delay || 0)
  },

  /**
   * Handle iscroll init callback.
   * @param {Event} e - Init event.
   */
  handleIScrollInit (e) {
    console.warn('[ApScrollMixin] registerIscroll is now deprecated. Use registerIscroll instead.')
    const s = this
    s.registerIScroll(e.iScroll)
  },

  registerIScroll (iScroll) {
    const s = this
    s._iScroll = iScroll
  },

  /**
   * Get i scroll instance.
   * @returns {?Object}
   */
  getIScroll () {
    const s = this
    return s._iScroll
  },

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

  componentWillUnmount () {
    const s = this

    clearTimeout(s._iScrollRefreshTimer)
  },

  // --------------------
  // Private
  // --------------------

  _iScrollRefreshTimer: -1

}

export default Object.freeze(ApScrollMixin)
