import { listBooks, listDuplicateBooks } from '$lib/api/book.remote.js';

export async function load({ params, depends }) {
	// Dismissing or merging a group re-runs this, so the card leaves the screen.
	depends('app:duplicate-books');

	const groups = await listDuplicateBooks(params.libraryId);

	// The groups carry only enough to render a card. Merging needs the whole record —
	// identifiers, description, publisher — so fetch them in one go rather than per
	// card, and let the dialog pick out the books for its own group.
	const ids = [...new Set(groups.flatMap((group) => group.books.map((book) => book.book_id)))];
	const books = ids.length ? await listBooks({ ids, pageSize: ids.length }) : { items: [] };

	return { groups, books: books.items };
}
