import type { SimulationFunction } from '@x12i/api-simulator'; import { listBooks } from '../state/checkout-ledger.js'; /** * GET /books/search?author=&available= * * `relative` behavior can only echo data back verbatim -- it has no concept of * conditionals. Filtering by query parameters needs a `simulation` handler. */ export const searchBooks: SimulationFunction = ({ request }) => { const author = typeof request.query.author === 'string' ? request.query.author.toLowerCase() : undefined; const onlyAvailable = request.query.available === 'true'; const items = listBooks().filter((book) => { if (author && !String(book.author).toLowerCase().includes(author)) return false; if (onlyAvailable && Number(book.copiesAvailable) < 1) return false; return true; }); return { status: 200, body: { count: items.length, items } }; };