/**
 * 实现全局的请求 loading
 * 参考文章
 * https://www.jianshu.com/p/c66adc327553
 * 参考代码 https://github.com/woodccc/axios-global-loading-demo/blob/master/frontend/src/api/axiosInitHelper.js
 */
import IbpsToast from '@/components/ibps-toast'
import _ from 'lodash'
import I18n from '@/utils/i18n'

let needLoadingRequestCount = 0
let loading

/**
 * 加载
 * @param {*} config
 */
function startLoading(config = {}) {
  loading = IbpsToast.loading({
    mask: config.mask || false,
    forbidClick: config.forbidClick || true,
    duration: config.duration || 0,
    message: config.message || I18n.t('loading')
  })
}

/**
 * 清除loaing
 */
function clearLoading() {
  loading ? loading.clear() : loading = null
}

const tryClearLoading = () => {
  if (needLoadingRequestCount === 0) {
    clearLoading()
  }
}

export function showFullScreenLoading(config) {
  if (needLoadingRequestCount === 0) {
    startLoading(config)
  }
  needLoadingRequestCount++
}

export function tryHideFullScreenLoading() {
  if (needLoadingRequestCount <= 0) return
  needLoadingRequestCount--
  if (needLoadingRequestCount === 0) {
    _.debounce(tryClearLoading, 200)()
  }
}
