import Vue from 'vue'
import Cookies from 'js-cookie'

// import 'normalize.css/normalize.css' // a modern alternative to CSS resets
import Element from 'element-ui'
import VueHighlightJS from 'vue-highlightjs'
// import enLang from 'element-ui/lib/locale/lang/en'// 如果使用中文语言包请默认支持，无需额外引入，请删除该依赖
import permission from '@/permission' // permission control
// import 'font-awesome/css/font-awesome.min.css'
import 'element-ui/lib/theme-chalk/index.css';
import App from './App'
import store from './store'
import router from './router'
import * as filters from './filters' // global filters

/**
 * If you don't want to use mock-server
 * you want to use MockJs for mock api
 * you can execute: mockXHR()
 *
 * Currently MockJs will be used in the production environment,
 * please remove it before going online ! ! !
 */
if (process.env.NODE_ENV === 'production') {
  const { mockXHR } = require('../mock')
  mockXHR()
}
Vue.directive('permission', permission)
Vue.use(Element, {
  size: Cookies.get('size') || 'medium' // set element-ui default size
  // locale: enLang // 如果使用中文，无需设置，请删除
})
Vue.use(VueHighlightJS)

// register global utility filters
Object.keys(filters).forEach(key => {
  Vue.filter(key, filters[key])
})

Vue.config.productionTip = false
Vue.prototype.$alert = function(message, ...values) {
  Element.MessageBox.alert(this['$tc'](message, ...values), '系统提示')
}
Vue.prototype.returnUrl = function (url) {
  const urlRegex = /^(https?:\/\/)?([\da-z.-]+)\.([a-z.]{2,6})(:[0-9]{1,5})?(\/.*)?$/i;
  // 测试字符串是否匹配正则表达式
  if (!urlRegex.test(url)) {
    return location.origin + '/' + url
  }
  return url;
}
Vue.prototype.strLen = function(str) {
  if (typeof str === 'string') {
    return str.replace(/[^\x00-\xff]/g, '01').length
  }
}
Vue.prototype.cutByte = function(str, len, endstr) {
  var len = +len
  var endstr = typeof (endstr) === 'undefined' ? '' : endstr.toString()
  var endstrBl = Vue.prototype.strLen(endstr)

  function n2(a) {
    var n = a / 2 | 0
    return (n > 0 ? n : 1)
  }// 用于二分法查找
  if (!(str + '').length || !len || len <= 0) {
    return ''
  }
  if (len < endstrBl) {
    endstr = ''
    endstrBl = 0
  }
  var lenS = len - endstrBl
  var _lenS = 0
  var _strl = 0
  while (_strl <= lenS) {
    var _lenS1 = n2(lenS - _strl)
    var addn = Vue.prototype.strLen(str.substr(_lenS, _lenS1))
    if (addn == 0) {
      return str
    }
    _strl += addn
    _lenS += _lenS1
  }
  if (str.length - _lenS > endstrBl || Vue.prototype.strLen(str.substring(_lenS - 1)) > endstrBl) {
    return str.substr(0, _lenS - 1) + endstr
  } else {
    return str
  }
}
Vue.prototype.$info = function(message, ...values) {
  this['$message'].info(this['$tc'](message, ...values))
}
Vue.prototype.$error = function(message, ...values) {
  this['$message'].error(this['$tc'](message, ...values))
}
Vue.prototype.$warning = function(message, ...values) {
  this['$message'].warning(this['$tc'](message, ...values))
}
Vue.prototype.$success = function(message, ...values) {
  this['$message'].success(this['$tc'](message, ...values))
}
Vue.prototype.generateSlug = function(title) {
  return title.trim()
    .split(/[^\w\u4e00-\u9fa5]/) // 更简洁的正则
    .filter(Boolean)
    .map(segment => segment.toLowerCase())
    .join('-')
    .replace(/-+/g, '-')
    .replace(/^-|-$/g, '') // 新增首尾连字符清理
}

let isInit = false
Vue.prototype.$getIsInit = function() { return isInit }
Vue.prototype.$setIsInit = function() { isInit = true }
Vue.prototype.$bus = new Vue()

export const root = new Vue({
  el: '#app',
  store,
  router,
  render: h => h(App)
})

