import {createSlice} from '@reduxjs/toolkit';
import type {PayloadAction} from '@reduxjs/toolkit';

import {appStatus, non_save_vars} from './appNonSaveVar';
import {User} from '@caj/user/types';
import {UserId} from './types';

export const appNonSaveVariablesSlice = createSlice({
  name: 'non_save_vars',
  initialState: non_save_vars,
  reducers: {
    setAppStatus: (state, action: PayloadAction<appStatus>) => {
      state.appStatus = action.payload;
    },
    setCachedUser: (state, action: PayloadAction<User>) => {
      state.cachedUsers[action.payload.UserId] = action.payload;
    },
    removeCachedUser: (state, action: PayloadAction<UserId>) => {
      delete state.cachedUsers[action.payload];
    },
  },
});

// Action creators are generated for each case reducer function
const {actions} = appNonSaveVariablesSlice;
export const appNonSaveVarActions = actions;

export default appNonSaveVariablesSlice.reducer;
