import { error } from '@sveltejs/kit';
import type { PageServerLoad } from './$types';
import { getCalculatorsByCategory, categories } from '$lib/data/calculators';

export const load: PageServerLoad = ({ params, setHeaders }) => {
  const cat = params.category;
  const meta = categories[cat];

  if (!meta) {
    throw error(404, { message: `Category "${cat}" not found` });
  }

  const calcs = getCalculatorsByCategory(cat);
  if (calcs.length === 0) {
    setHeaders({
      'X-Robots-Tag': 'noindex, follow'
    });
  }

  return {
    category: cat,
    label: meta.label,
    icon: meta.icon,
    calculators: calcs
  };
};
