import { getVariantPayload, loadManifest } from '../lib/background.js';

export async function onRequestGet(context) {
  const { request } = context;
  const manifest = await loadManifest(request);

  return Response.json(
    {
      generated_at: manifest?.generated_at || null,
      variants: {
        mobile: summarizeVariant(manifest, 'mobile'),
        desktop: summarizeVariant(manifest, 'desktop'),
      },
    },
    { headers: { 'Cache-Control': 'no-store, max-age=0', 'Access-Control-Allow-Origin': '*' } },
  );
}

function summarizeVariant(manifest, variant) {
  const { folder, images } = getVariantPayload(manifest, variant);
  return { folder, count: images.length, images };
}
