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

interface IUserId {
  userId: string
}

const initialState: IUserId = {
  userId: ""
}

const userIdSlice = createSlice({
  name: 'userId',
  initialState,
  reducers: {
    update(state, action: PayloadAction<IUserId>) {
      state = action.payload
      return state
    },
  },
  extraReducers: (builder) => builder.addCase('reset', () => initialState)
})

export const { actions } = userIdSlice
export const selectUserId = createSelector(
  (state: { userId: IUserId }) => state.userId.userId,
  (userId) => userId
)
export default userIdSlice.reducer
