import { createSlice } from '@reduxjs/toolkit'

const initialState = {
  theme: 'light',
  isDark: false,
}

const themeSlice = createSlice({
  name: 'theme',
  initialState,
  reducers: {
    setTheme: (state, action) => {
      state.theme = action.payload
      state.isDark = action.payload === 'dark'
    },
    toggleTheme: (state) => {
      state.theme = state.theme === 'light' ? 'dark' : 'light'
      state.isDark = state.theme === 'dark'
    },
  },
})

export const { setTheme, toggleTheme } = themeSlice.actions

export const selectTheme = (state) => state.theme.theme
export const selectIsDark = (state) => state.theme.isDark

export default themeSlice.reducer

