import { createStore, createLogger } from 'vuex'
import Presistent from './plugins/persistent'
const debug = process.env.NODE_ENV !== 'production'

const files = require.context("./modules/", true, /\.js$/)
const map = {}
for (const key of files.keys()) {
  map[key] = files(key)
}

let modules = {}
Object.keys(map).forEach((c) => {
  const module = map[c].default
  const moduleName = ('./modules/'+c.replace('./','')).replace(/^\.\/(.*)\/(.*)\.\w+$/, '$2')
  modules[moduleName] = module
})

const presistent = Presistent({ key: 'vuex', modules, modulesKeys: {
  local: Object.keys(modules),
  session: []
} })

export default createStore({
  modules: {
    ...modules
  },
  strict: debug,
  plugins: debug ? [createLogger(), presistent] : [presistent]
})