import { createSlice } from "@reduxjs/toolkit";
import { fetchHomeMenu } from "./menuThunks";

const initialState = {
  title: "",
  items: {},
};

export const menuSlice = createSlice({
  name: "menu",
  initialState,
  reducers: {},
  extraReducers: (builder) => {
    builder
      .addCase(fetchHomeMenu.pending, (state, action) => {})
      .addCase(fetchHomeMenu.fulfilled, (state, action) => {
        state.title = action.payload.gnb.map((titles) => {
          return titles.menuNm;
        });
        state.items = action.payload.gnb.map((items) => {
          return items;
        });
      })

      .addCase(fetchHomeMenu.rejected, (state, action) => {});
  },
});

export const selectorMenuId = (state) => state.menu.menuId;
export const selectorMenuTitle = (state) => state.menu.title;
export const selectorMenuItem = (state) => state.menu.items;

export default menuSlice.reducer;
