import {
  HomeTabUsingPostResponseTabMenus,
  WxLoginUsingGetResponse,
} from "@/apis/userApi";
import StorageUtil, { EStorage } from "@/utils/storageUtil";
import { createSlice } from "@reduxjs/toolkit";

export interface IReduxStore {
  userInfo: WxLoginUsingGetResponse;
  tabMenu: HomeTabUsingPostResponseTabMenus[];
}

const initialState = (): IReduxStore => ({
  userInfo: StorageUtil.get(EStorage.userInfo),
  tabMenu: StorageUtil.get(EStorage.tabMenu) || [
    {
      code: "home",
      icon: require("@/assets/icon_bottom_bar_apply.png"),
      iconActive: require("@/assets/icon_bottom_bar_apply_active.png"),
      name: "首页",
    },
    // {
    //   code: "official_account",
    //   icon: require("@/assets/official-account/icon_bottom_bar_follow.svg"),
    //   iconActive: require("@/assets/official-account/icon_bottom_bar_follow_active.svg"),
    //   name: "关注公众号",
    // },
    {
      code: "user_center",
      icon: require("@/assets/icon_bottom_bar_mine.png"),
      iconActive: require("@/assets/icon_bottom_bar_mine_active.png"),
      name: "我的",
    },
  ],
});

export const reducersSlice = createSlice({
  name: "reducer",
  initialState: initialState(),
  reducers: {
    changeUserInfo: (state, action) => {
      state.userInfo = action.payload;
    },
    clearState: (state) => {
      let initState = initialState();
      for (const key in state) {
        state[key] = initState[key];
      }
      // state = initialState();
    },
    changeTabMenu: (state, action) => {
      state.tabMenu = action.payload;
    },
    /**更改地址 */
    // changeAddress: (state, action) => {
    //     let obj = { ...action.payload };
    //     state.wallet = obj;
    // },

    // changeTokenList: (state, action) => {
    //     state.tokenList = action.payload;
    // }
  },
});

export const reduxActions = reducersSlice.actions;
