import type { PayloadAction } from '@reduxjs/toolkit';
import { createAppSlice } from '../../app/createAppSlice';
import booksData from './booksData';

export const booksSlice = createAppSlice({
  name: 'books',
  initialState: booksData,
  reducers: {
    remove(state, action: PayloadAction<number>) {
      return state.filter((book) => book.id !== action.payload);
    },
  },
  selectors: {
    selectAllBooks: (state) => state,
    selectBookById: (state) => (id: number) => state.find((b) => b.id === id),
  },
});

export const { selectAllBooks, selectBookById } = booksSlice.selectors;
export const { remove } = booksSlice.actions;
