import Vue from 'vue'
import Vuex from 'vuex'
import VuexPersistence from 'vuex-persist'
import LocalModule from '@/store/modules/local'

Vue.use(Vuex)

const vuexLocal = new VuexPersistence({
  key: 'local:snt-template',
  storage: window.localStorage,
  modules: ['local', 'cart']
})

const store = new Vuex.Store({
  modules: {
    local: LocalModule
  },
  plugins: [
    vuexLocal.plugin
  ]
})

if (window === window.top) {
  window._$store = store
} else {
  store.replaceState({
    local: window.parent._$store.state.local,
    cart: window.parent._$store.state.cart,
    salePage: window.parent._$store.state.salePage
  })
}

export default store
