import type { BibleBook, Testament, BibleSection } from '$lib/types/bible';
import { bibleBooks } from '$lib/types/bible';

// Book number (1-66) to book ID mapping derived from bibleBooks order property
export const bookNumberToId: Record<number, string> = bibleBooks.reduce((acc, book) => {
	acc[book.order] = book.id;
	return acc;
}, {} as Record<number, string>);

// Book ID to book number mapping (reverse lookup)
export const bookIdToNumber: Record<string, number> = bibleBooks.reduce((acc, book) => {
	acc[book.id] = book.order;
	return acc;
}, {} as Record<string, number>);

export function getBookById(id: string): BibleBook | undefined {
	return bibleBooks.find((book) => book.id === id);
}

export function getBookByNumber(number: number): BibleBook | undefined {
	return bibleBooks.find((book) => book.order === number);
}

export function getBooksByTestament(testament: Testament): BibleBook[] {
	return bibleBooks.filter((book) => book.testament === testament);
}

export function getBooksBySection(section: BibleSection): BibleBook[] {
	return bibleBooks.filter((book) => book.section === section);
}

export function isAdjacent(bookId1: string, bookId2: string): boolean {
	const book1 = getBookById(bookId1);
	const book2 = getBookById(bookId2);
	if (!book1 || !book2) return false;
	return Math.abs(book1.order - book2.order) === 1;
}

export { bibleBooks };
