import {createSlice} from "@reduxjs/toolkit";

import {useInjectReducer} from "../../../utils/@reduxjs/injectReducer";

import {getThemeFromStorage} from "../utils";

export const initialState  ={
  selected: getThemeFromStorage(),
}

/**
 * 主题Redux切片管理
 * use : dispatch(themeSlice.actions.changeTheme('dark'));
 */
const themeSlice = createSlice({
  name:'theme',
  initialState,
  reducers:{
    changeTheme(state, action) {
      state.selected = action.payload;
    },
  }
})

export default themeSlice;

export const { actions: themeActions, reducer } = themeSlice;

export const useThemeSlice = () => {
  useInjectReducer({ key: themeSlice.name, reducer: themeSlice.reducer });
  return { actions: themeSlice.actions };
};
