import BarChartPlus from './bar-chart-plus/index'
import HorChartPlus from './hor-chart-plus/index'
import LineChartPlus from './line-chart-plus/index'
import PieChartPlus from './pie-chart-plus/index'
import MapMarkerListView from './map-marker-list-view/index'
import MapPointPickerDialog from './map-point-picker-dialog/index'
import InputPlus from './input-plus/index'
import FilePreview from './file-preview/index'

// 加载工具类
import tools from '../assets/utils/index.js' // 这个导出名称已指定,不用关注文件名称
import { localStoragePlus, sessionStoragePlus, cookiePlus } from '../assets/utils/storage.js'

// 将上述组件保存到一个数组
const components = [
  BarChartPlus, HorChartPlus, LineChartPlus, PieChartPlus,
  MapMarkerListView, MapPointPickerDialog,
  InputPlus, FilePreview,
];

// 定义install方法，它将作为export暴露的对象的方法被Vue.use调用
const install = function (Vue, opts = {}) {

  // 判断当前组件是否已被安装过，如果已安装过则不再安装
  if (install.installed) return;
  install.installed = true;

  // 遍历components数组，依次注册每个组件
  components.map(component => {
    Vue.component(component.name, component);
  })

  // 参数--命名空间等
  Vue.prototype.$compWrap = {
    namespace: opts.namespace,
  };
  // 更新 storage.js 命名空间
  localStorage.setItem('namespace', opts.namespace);

  // 挂载原型链---原型链加$
  Vue.prototype.$tools = tools;
  Vue.prototype.$localStoragePlus = localStoragePlus;
  Vue.prototype.$sessionStoragePlus = sessionStoragePlus;
  Vue.prototype.$cookiePlus = cookiePlus;

  // 挂载window/globalThis----约定不加$
  window.tools = tools;
  window.localStoragePlus = localStoragePlus;
  window.sessionStoragePlus = sessionStoragePlus;
  window.cookiePlus = cookiePlus;

  // 是否注入样式重置
  if (opts.injectReset) {
    require('../assets/styles/normalize.css');
  }
  // 是否注入基础样式
  if (opts.injectStyle) {
    require('../assets/styles/base.css');
  }
}

if (typeof window !== 'undefined' && window.Vue) {
  install(window.Vue);
}

export default {
  version: '2.0.0', // 版本号
  install, // 全局引入
  ...components,    // 按需引入
}
