import { getVideos } 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 duration = url.searchParams.get("duration") || "all";
  const page = Math.max(1, parseInt(url.searchParams.get("page") || "1", 10));
  const offset = (page - 1) * LIMIT;

  const result = await getVideos({ search, sortBy: sort, duration, offset, limit: LIMIT }, fetch);
  return { ...result, search, sort, duration, page, limit: LIMIT };
}
