import ky from 'ky';
import { type Book, bookSchema, booksSchema } from './Book';

const booksAPI = ky.create({
  prefixUrl: `${import.meta.env.VITE_API_SERVER}/books`,
});

export async function getAllBooks(): Promise<Book[]> {
  const data = await booksAPI.get('').json();
  return booksSchema.parse(data);
}

export async function deleteBook(id: number): Promise<void> {
  await booksAPI.delete(`${id}`);
}

export async function editBook(book: Book): Promise<Book> {
  const data = await booksAPI.put(`${book.id}`, { json: book }).json();
  return bookSchema.parse(data);
}
