---
/**
 * Inline SVG icon from bootstrap-icons, read at build time (zero runtime JS / no icon font).
 * <Icon name="truck" size={20} class="..." />
 */
import fs from 'node:fs';
import path from 'node:path';

interface Props { name: string; size?: number | string; class?: string; label?: string }
const { name, size = '1em', class: cls = '', label } = Astro.props;

const cache: Map<string, string> = ((globalThis as any).__iconCache ??= new Map());
let inner = cache.get(name);
if (inner === undefined) {
  try {
    const file = path.join(process.cwd(), 'node_modules', 'bootstrap-icons', 'icons', `${name}.svg`);
    const raw = fs.readFileSync(file, 'utf8');
    inner = raw.replace(/^[\s\S]*?<svg[^>]*>/, '').replace(/<\/svg>\s*$/, '');
  } catch {
    console.warn(`[Icon] missing bootstrap icon "${name}"`);
    inner = '';
  }
  cache.set(name, inner);
}
---
<svg
  xmlns="http://www.w3.org/2000/svg"
  width={size}
  height={size}
  fill="currentColor"
  viewBox="0 0 16 16"
  class={`icon ${cls}`}
  aria-hidden={label ? undefined : 'true'}
  role={label ? 'img' : undefined}
  aria-label={label}
  set:html={inner}
/>
