import { listParts, categoriesWithParts } from '$lib/server/parts.js';

export function load({ url }) {
  const q    = url.searchParams.get('q')    ?? '';
  const sort = url.searchParams.get('sort') ?? 'sku';
  const dir  = url.searchParams.get('dir')  ?? 'asc';
  const cat  = url.searchParams.get('category') ?? '';
  const categoryIds = cat
    .split(',')
    .map((s) => Number(s))
    .filter((n) => Number.isInteger(n) && n > 0);
  return {
    parts: listParts({ q, sort, dir, categoryIds }),
    categories: categoriesWithParts(),
    q, sort, dir, categoryIds,
  };
}
