import storage from "redux-persist/lib/storage";

export const userReducers = {
  LOG_OUT: (state, action) => {
    storage.removeItem("persist:root");
    state.authToken = null;
    state.isFirstLogin = true;
    state.typeActivitySelected = null;
    state = null;
    // window.location.reload();

    // Safe localStorage operations
    try {
      const dashboardOrder = localStorage.getItem("dashboard_custom_order");
      localStorage.clear();
      if (dashboardOrder) {
        localStorage.setItem("dashboard_custom_order", dashboardOrder);
      }
    } catch (e) {
      // Silent fail
    }
  },
  AUTO_SIGN_IN: (state, action) => {
    state.authToken = action.payload.accessToken;
    state.tokenExpiresIn = action.payload.expiresIn;
    state.role = action.payload.role;
    state.loginUserProfile = action.payload;
    // localStorage.clear();
  },
  IS_FIRST_LOGIN: (state, action) => {
    state.isFirstLogin = action.payload;
  },
  SET_MEDIATOR_TEXT: (state, action) => {
    state.mediatorText = action.payload;
  },
  SET_SUBMENU_TEXT: (state, action) => {
    state.subMenuText = action.payload;
  },
  SET_USER_PATH: (state, action) => {
    state.userPath = action.payload;
  },
  SET_ADMIN_TOKEN: (state, action) => {
    state.adminToken = action.payload;
  },
  SET_TYPE_ACTIVITY_SELECTED: (state, action) => {
    state.typeActivitySelected = action.payload;
  },
  SET_SELECTED_ROLE: (state, action) => {
    state.selectedRole = action.payload;
  },
  SET_SELECTED_SUB_USER: (state, action) => {
    state.selectedSubUser = action.payload;
  },
  SET_SUB_USERS: (state, action) => {
    state.subUsers = action.payload;
  },
};
