import { ICards } from "../../models/ICards";
import { createSlice, PayloadAction } from "@reduxjs/toolkit";
import { fetchCards } from "./ActionCreator";

interface CardState {
  cards: ICards[];
  isLoading: boolean;
  error: any;
  currentCard: ICards | null;
}
const initialState: CardState = {
  cards: [],
  isLoading: false,
  error: "",
  currentCard: null,
};

export const cardSlice = createSlice({
  name: "cards",
  initialState,
  reducers: {
    handleCurrentCard(state, action: PayloadAction<ICards>) {
      state.currentCard = action.payload;
    },
  },
  extraReducers: {
    
    [fetchCards.fulfilled.type]: (state, action: PayloadAction<ICards[]>) => {
      state.isLoading = false;
      state.error = "";
      state.cards = action.payload;
    },
    [fetchCards.pending.type]: (state) => {
      state.isLoading = true;
    },
    [fetchCards.rejected.type]: (state, action: PayloadAction<any>) => {
      state.isLoading = false;
      state.error = action.payload;
    },
  },
  
});

export default cardSlice.reducer;
