import Vue from 'vue';
import Vuex from 'vuex';
let path = require('path');
Vue.use(Vuex);

const moduleFn = require.context("@", true, /\/stores(.*)\/*\.js$/);
let modules = moduleFn.keys().reduce((p, c) => {
  let mod = moduleFn(c).default;
  mod = { ...mod, namespaced: true };
  let modName = path.basename(c, '.js');
  p[modName] = mod;
  return p;
}, {});

export function initStore(files) {
  let storeModules = files.keys().reduce((p, c) => {
    let mod = files(c).default;
    mod = { ...mod, namespaced: true };
    let modName = path.basename(c, '.js');
    p[modName] = mod;
    return p;
  }, {});
  // console.log(storeModules);
  modules = Object.assign(modules, storeModules);
  // console.log('initStore modules', modules);
  return new Vuex.Store({ modules });
}

export default new Vuex.Store({ modules });
