text: import {isLoggedIn, logOut} from "./shared/utils/auth";
  
  export default {
      state: {
          lastSearch: {
              from: null,
              to: null
          },
          basket:{
              items: []
          },
          isLoggedIn: false,
          user: {
          }
      },
      mutations: {
          setLastSearch(state, payload) {
              state.lastSearch = payload;
          },
          addToBasket(state, payload) {
              state.basket.items.push(payload);
          },
  
          removeFromBasket(state, payload){
              state.basket.items = state.basket.items.filter(item => item.bookable.id !== payload)
          },
          setBasket(state, payload){
              state.basket = payload;
          },
          setUser(state, payload){
              state.user = payload;
          },
          setLoggedIn(state, payload){
              state.isLoggedIn = payload;
          }
      },
      actions: {
          setLastSearch(context, payload) {
              context.commit('setLastSearch', payload);
              localStorage.setItem('lastSearch', JSON.stringify(payload))
          },
          setUser(context, payload) {
              context.commit('setUser', payload);
              localStorage.setItem('user', JSON.stringify(payload))
          },
          loadStoredState(context) {
              const lastSearch = localStorage.getItem('lastSearch');
              if (lastSearch) {
                  context.commit('setLastSearch', JSON.parse(lastSearch));
              }
  
              const basket = localStorage.getItem('basket');
              if (basket){
                  context.commit('setBasket', JSON.parse(basket));
              }
              context.commit("setLoggedIn", isLoggedIn());
          },
          addToBasket({ commit, state }, payload){
              commit('addToBasket', payload);
              localStorage.setItem('basket', JSON.stringify(state.basket));
  
          },
          //context.commit, context.state
          removeFromBasket({ commit, state }, payload){
              commit('removeFromBasket', payload);
              localStorage.setItem('basket', JSON.stringify(state.basket));
          },
          clearBasket({commit, state}, payload){
              commit("setBasket", {items: [] });
              localStorage.setItem("basket", JSON.stringify(state.basket))
          },
          async loadUser({commit, dispatch}){
              if(isLoggedIn()){
                  try{
                      const user = (await axios.get('/user')).data;
                      commit("setUser", user);
                      commit("setLoggedIn", true);
                  }catch (error) {
                      dispatch("logout");
                  }
              }
          },
          logout({ commit }){
              commit("setUser", {});
              commit("setLoggedIn", false);
              logOut();
          }
      },
  
      getters : {
          itemsInBasket: (state) => state.basket.items.length,
          /*getTotal: state => {
              const totalItems = state.basket.items.length
              return state.basket.items.reduce(
                  (total, item) =>  total.price.total + item.price.total,
              );
          },*/
          inBasketAlready(state) {
              return function (id) {
                  return state.basket.items.reduce(
                      (result, item) => result || item.bookable.id === id, false
                  );
              };
          },
          currentUser:(state) => state.user
  
      }
  };
  