import { getArticles, getArticleCategories } from "$lib/services";

const LIMIT = 24;

export async function load({ fetch, url }) {
  const search = url.searchParams.get("search") || undefined;
  const sort = url.searchParams.get("sort") || "recent";
  const category = url.searchParams.get("category") || undefined;
  const page = Math.max(1, parseInt(url.searchParams.get("page") || "1", 10));
  const offset = (page - 1) * LIMIT;

  const [result, categories] = await Promise.all([
    getArticles({ search, sortBy: sort, category, offset, limit: LIMIT }, fetch),
    getArticleCategories(fetch),
  ]);
  return { ...result, search, sort, category, page, limit: LIMIT, categories };
}
