import type { Book } from '../shared/types.ts'; import { cardLines, formatCallNumber, parseCallNumber, withYear } from '../shared/callnumber.ts'; import { displayAuthors } from '../shared/names.ts'; import { cutterFor, mainEntry } from '../shared/cutter.ts'; /** A call number for `authors`/`title` that shelves next to `neighbour`: its class part, a new cutter, then the year. */ export function composeNumber(neighbour: string, authors: string[], title: string, year: number | null): string | null { const cn = parseCallNumber(neighbour); if (!cn) return null; const classPart = parseCallNumber(cardLines(cn)[0]); const cutter = parseCallNumber(`A1 ${cutterFor(mainEntry(authors, title))}`)?.cutters[0]; if (!classPart || !cutter) return null; const next = { ...classPart, cutters: [...classPart.cutters] }; if (next.cutters.length < 3) next.cutters.push(cutter); return formatCallNumber(year ? withYear(next, year) : next); } /** Up to `limit` of the library's books with a readable call number whose title or author matches. */ export function findNeighbours(books: Book[], text: string, limit = 8): Book[] { const needle = text.trim().toLowerCase(); if (!needle) return []; const out: Book[] = []; for (const b of books) { if (!b.callNumber || !parseCallNumber(b.callNumber)) continue; if ([b.title, b.authors.join(' '), displayAuthors(b.authors).join(' ')].some(s => s.toLowerCase().includes(needle))) out.push(b); if (out.length >= limit) break; } return out; }