import type { PayloadAction } from "@reduxjs/toolkit"

import { createSlice } from "@reduxjs/toolkit"

import { UserEntity, UserState } from "./types"

const initialState: UserState = {
	user: null,
}

const userSlice = createSlice({
	name: "user",
	initialState,
	reducers: {
		setAuthData(
			state,
			action: PayloadAction<{
				user: UserEntity
			}>,
		) {
			state.user = action.payload.user
		},
		setUser(state, action: PayloadAction<UserEntity | null>) {
			state.user = action.payload
		},
		reset(state) {
			state.user = initialState.user
		},
	},
})

export const { reset: resetUser, setAuthData, setUser } = userSlice.actions
export const userReducer = userSlice.reducer
