import { createSlice, createSelector } from '@reduxjs/toolkit'
import type { PayloadAction } from '@reduxjs/toolkit'
import { ESourceAuthorization } from "@/api/resources/User";

type TSessions = {
  [key in ESourceAuthorization]: string
}

const initialState: TSessions = {
  "aura.main": "",
  "aura.palmistry": "",
  "aura.chat": "",
  "aura.moons": "",
  "aura.main.new": "",
  "aura.palmistry.new": "",
  "aura.chats": "",
  "aura.compatibility.v2": "",
  "aura.compatibility.v3": "",
  "aura.compatibility.v4": "",
  "aura.test.payment": ""
}

const sessionSlice = createSlice({
  name: 'session',
  initialState,
  reducers: {
    update(state, action: PayloadAction<{
      source: ESourceAuthorization
      session: string
    }>) {
      return { ...state, [action.payload.source]: action.payload.session }
    },
  },
  extraReducers: (builder) => builder.addCase('reset', () => initialState)
})

export const { actions } = sessionSlice
export const selectSession = createSelector(
  (state: { session: TSessions }) => state.session,
  (session) => session
)
export default sessionSlice.reducer
