import { adminListVideos } from "$lib/services";

export async function load({ fetch, url, cookies }) {
  const token = cookies.get("session_token") || "";
  const search = url.searchParams.get("search") || undefined;
  const featuredParam = url.searchParams.get("featured");
  const premiumParam = url.searchParams.get("premium");
  const featured = featuredParam !== null ? featuredParam === "true" : undefined;
  const premium = premiumParam !== null ? premiumParam === "true" : undefined;
  const offset = parseInt(url.searchParams.get("offset") || "0", 10);
  const limit = 50;

  const result = await adminListVideos(
    { search, featured, premium, limit, offset },
    fetch,
    token,
  ).catch(() => ({ items: [], total: 0 }));

  return { ...result, search, featured, premium, offset, limit };
}
