import type { Picture } from '@sveltejs/enhanced-img';

// In dev, imagetools can be painfully slow for large local assets.
// Fall back to the already-served static files and keep enhanced variants for production builds.
const modules: Record<string, { default: Picture }> = import.meta.env.DEV
  ? {}
  : (import.meta.glob('./images/**/*.{jpg,jpeg,png,webp,avif,gif}', {
      eager: true,
      query: { enhanced: true }
    }) as Record<string, { default: Picture }>);

export function getEnhancedImage(src: string | undefined | null): Picture | null {
  if (!src) return null;
  // '/images/foo.png' -> './images/foo.png' (relative to src/lib/)
  const key = '.' + src;
  return modules[key]?.default ?? null;
}
