import Vue from "vue";
import Vuex from "vuex";
import getters from "./getters";

Vue.use(Vuex);

// 全局状态
const modulesFiles = require.context("./modules", true, /\.js$/);
const modules = modulesFiles.keys().reduce((modules, modulePath) => {
  const moduleName = modulePath.replace(/^\.\/(.*)\.\w+$/, "$1");
  modules[moduleName] = modulesFiles(modulePath).default;
  return modules;
}, {});

// 项目状态
const storeFiles = require.context(
  "@/modules",
  true,
  /\/store\/([a-zA-Z0-9_-])+\.js$/
);
const store = storeFiles.keys().reduce((modules, modulePath) => {
  const moduleName = modulePath
    .replace(/^\.\/(.*)\.\w+$/, "$1")
    .split("/store/")[1];
  modules[moduleName] = storeFiles(modulePath).default;
  return modules;
}, {});

export default new Vuex.Store({
  modules: { ...modules, ...store },
  getters
});
