import Vue from 'vue'
import App from './App.vue'
import Router from './router'
import Store from './store'
import Filters from '@/filters'
import utils from '@/assets/js/common'
import './icons'
import FastClick from 'fastclick'
import '@/assets/js/generatedValidate/index' //表单校验
import validatorItem from '@/assets/js/generatedValidate/generatedValidateItem' //表单校验
import '@utils/compatible' //兼容性代码
import NoMoreClick from '@/directive/no-more-click'
import eruda from 'eruda'
//全局注册vant常用组件
import 'vant/lib/index.css'
import 'viewerjs/dist/viewer.css'
import 'quill/dist/quill.snow.css'
import Bus from '@utils/bus'
import { Toast, Form, Loading, Lazyload, Notify, Image, NavBar, Icon, Card, Field, Button, Popup, Dialog } from 'vant'
import generatedFormat from '@/assets/js/generatedFormat/index'
import generatedComponents from './generatedComponents'
//权限控制
import { permission } from '@/assets/js/utils/permission'

for (let item in generatedComponents) {
  Vue.component(item, generatedComponents[item])
}

Vue.use(Image)
  .use(Toast)
  .use(Form)
  .use(Loading)
  .use(Lazyload)
  .use(Notify)
  .use(Lazyload, {
    lazyComponent: true
  })
  .use(Icon)
  .use(NavBar)
  .use(Card)
  .use(Field)
  .use(Button)
  .use(Popup)
  .use(Dialog)

//router or bridge jump
Vue.prototype.$bus = new Bus()
Vue.prototype.$generatedFormat = generatedFormat
Vue.prototype.$generatedDictList = generatedFormat.formatList
Vue.prototype.$validatorItem = { ...validatorItem }
Vue.prototype.$computeNumber = function computeNumber(a, type, b) {
  /**
   * 获取数字小数点的长度
   * @param {number} n 数字
   */
  function getDecimalLength(n) {
    const decimal = n.toString().split('.')[1]
    return decimal ? decimal.length : 0
  }
  /**
   * 修正小数点
   * @description 防止出现 `33.33333*100000 = 3333332.9999999995` && `33.33*10 = 333.29999999999995` 这类情况做的处理
   * @param {number} n
   */
  const amend = (n, precision = 15) => parseFloat(Number(n).toPrecision(precision))
  const power = Math.pow(10, Math.max(getDecimalLength(a), getDecimalLength(b)))
  let result = 0

  a = amend(a * power)
  b = amend(b * power)

  switch (type) {
    case '+':
      result = (a + b) / power
      break
    case '-':
      result = (a - b) / power
      break
    case '*':
      result = (a * b) / (power * power)
      break
    case '/':
      result = a / b
      break
  }

  result = amend(result)

  return {
    /** 计算结果 */
    result,
    /**
     * 继续计算
     * @param {"+"|"-"|"*"|"/"} nextType 继续计算方式
     * @param {number} nextValue 继续计算的值
     */
    next(nextType, nextValue) {
      return computeNumber(result, nextType, nextValue)
    }
  }
}

// 全局 防重复点击
Vue.directive('no-more-click', NoMoreClick)

Vue.prototype.$utils = utils
//混合开发调试工具
// 注册过滤器
Object.keys(Filters).forEach(function(k) {
  Vue.filter(k, Filters[k])
})
let envFlag = process.env.VUE_APP_FLAG
if (envFlag != 'prd') {
  // eslint-disable-next-line no-undef
  eruda.init()
  Vue.config.devtools = true
} else {
  Vue.config.devtools = false
  // eslint-disable-next-line no-undef
  // eruda.init() //TODO 生产环境验证，打开调试工具
}

permission()
//ios点击300毫秒时延
FastClick.attach(document.body)
// components下的文件全部转化成组件
const files = require.context('@/components/', true, /\.vue$/)
files.keys().map(item => {
  Vue.component(files(item).default.name, files(item).default)
})
Vue.config.productionTip = false
const vm = new Vue({
  router: Router,
  store: Store,
  render: h => h(App)
}).$mount('#app')
window.page = vm
