import {initScreenListener, disableScreenListener} from "./stateHelpers.js";

const initState = {
  height:               0,
  width:                0,
  isOpenMenu:           false,
  isOpenedMobileMenu:   false,
  isEnabledMenu:        true,
  isShowMenu:           true,
  initedScreenListener: false,
};


const state = {
  ...initState
};

const getters = {
  height:                  (state) => state.height,
  width:                   (state) => state.width,
  isOpenMenu:              (state) => state.isOpenMenu,
  isShowMenu:              (state) => state.isShowMenu,
  isOpenedMobileMenu:      (state) => state.isOpenedMobileMenu,
  isEnabledMenu:           (state) => state.isEnabledMenu,
  initedScreenListener:    (state) => state.initedScreenListener,
};

const mutations = {
  enableScreenListener: (state) => {
    const inited = state.initedScreenListener
    if (!inited) {
      initScreenListener(state)
      state.initedScreenListener = true
    }
  },
  disableScreenListener: (state) => {
    disableScreenListener(state)
  },
  toggleMenu: (state) => {
    state.isShowMenu = !state.isShowMenu
  },
  saveIsMobileMenuOpened: (state, upd) => {
    state.isOpenedMobileMenu = upd
  },
  saveIsShowMenu: (state, upd) => {
    state.isShowMenu = upd
  },
  resetStore: (state) => {
    for (let key in state) {
      state[key] = initState[key]
    }
  },
  saveIsEnabledMenu: (state, upd) => {
    state.isEnabledMenu = upd
  },
  saveIsOpenMenu: (state, upd) => {
    state.isOpenMenu = upd
  },
};

const actions = {
  initScreenSizeListener: ({commit}) => {
    commit("enableScreenListener")
  },
  disableScreenListener: ({commit}) => {
    commit("disableScreenListener")
  },
  resetStore: ({commit}) => {
    commit("resetStore")
  },
  toggleMenu: ({commit}) => {
    commit("toggleMenu")
  },
  setIsShowMenu: ({commit}, upd) => {
    commit("saveIsShowMenu", upd)
  },
  setIsMobileMenuOpened: ({commit}, upd) => {
    commit("saveIsMobileMenuOpened", upd)
  },
  setIsEnabledMenu: ({commit}, upd) => {
    commit("saveIsEnabledMenu", upd)
  },
  setIsOpenMenu: ({commit}, upd) => {
    commit("saveIsOpenMenu", upd)
  },
};

export const store = {
  namespaced: true,
  state,
  getters,
  mutations,
  actions,
};
