import type { APIRoute } from 'astro';
import { getCategoryTree } from '../../../lib/categories/category-manager.js';

export const GET: APIRoute = async ({ url }) => {
  try {
    const dimension = url.searchParams.get('dimension') || undefined;
    const tree = await getCategoryTree(dimension);
    return new Response(JSON.stringify({ categories: tree }), {
      headers: { 'Content-Type': 'application/json' },
    });
  } catch (e: any) {
    return new Response(JSON.stringify({ error: e.message }), { status: 500 });
  }
};
