import Vue from 'vue'
import upperFirst from 'lodash/upperFirst'
import camelCase from 'lodash/camelCase'
import VueLogger from 'vuejs-logger'
import Loading from 'vue-loading-overlay'
import { FontAwesomeIcon } from '@fortawesome/vue-fontawesome'
import AllIcon from 'vue-ionicons/dist/ionicons'
import { library } from '@fortawesome/fontawesome-svg-core'
import { fab } from '@fortawesome/free-brands-svg-icons'
import { fas } from '@fortawesome/free-solid-svg-icons'
import { far } from '@fortawesome/free-regular-svg-icons'
import Buefy from 'buefy'
import VueAnime from 'vue-animejs'
import App from './App.vue'
import router from './router'
import store from './store/store'
import CKEditor from '@ckeditor/ckeditor5-vue'
import './assets/common.sass'

library.add(fab, fas, far)
Vue.component('font-awesome-icon', FontAwesomeIcon)
Vue.use(AllIcon)
Vue.use(VueAnime)
Vue.use(CKEditor)

// setup logging
const isProduction = process.env.NODE_ENV === 'production'
const options = {
  isEnabled: true,
  logLevel: isProduction ? 'error' : 'debug',
  stringifyArguments: false,
  showLogLevel: true,
  showMethodName: true,
  separator: '|',
  showConsoleColors: true
}
Vue.use(VueLogger, options)

// register global components
const requireComponent = require.context('./components', false, /Base[A-Z]\w+\.(vue|js)$/)
requireComponent.keys().forEach(fileName => {
  const componentConfig = requireComponent(fileName)
  const componentName = upperFirst(camelCase(fileName.replace(/^\.\/(.*)\.\w+$/, '$1')))
  Vue.component(componentName, componentConfig.default || componentConfig)
})
Vue.$log.debug('Registered global components: ', requireComponent.keys())

// register datasets
const datasets = []
const webpackContextDatasets = require.context('./datasets', true, /.+\.dataset\.js$/)
webpackContextDatasets.keys().forEach(fileName => {
  const mod = webpackContextDatasets(fileName)
  datasets.push({
    name: fileName.slice(2, -11),
    data: mod.__esModule && mod.default ? mod.default : mod
  })
})
Vue.$log.debug('Registered datasets: ', webpackContextDatasets.keys())
Vue.prototype.$datasets = datasets

// filters
Vue.filter('capitalize', function(value) {
  if (!value) return ''
  else value = value.toString()
  return value.charAt(0).toUpperCase() + value.slice(1)
})

Vue.use(Loading)
Vue.use(Buefy)

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
