import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
// packages/ui/src/internal/layout/components/FontPreloadLinks.tsx
/**
 * @fileoverview FontPreloadLinks - Server-only component for Next.js
 * @description Reads public/dndev-font-preloads.json (written at build by FontPreloadWebpackPlugin) and renders preload link tags for Lighthouse-friendly LCP. Include in root layout so initial HTML contains the links.
 *
 * @version 0.0.1
 * @since 0.0.1
 * @author AMBROISE PARK Consulting
 */
import fs from 'node:fs/promises';
import path from 'node:path';
const MANIFEST_FILENAME = 'dndev-font-preloads.json';
const FONTS_CSS_FILENAME = 'dndev-fonts.css';
/**
 * Server component: renders preload links for fonts listed in public/dndev-font-preloads.json.
 * No-op if manifest is missing or empty. Use in root layout.tsx for SSR preload in initial HTML.
 */
export async function FontPreloadLinks() {
    const publicDir = path.join(process.cwd(), 'public');
    // Load preload manifest (optional — may not exist on first build or static export)
    let entries = [];
    try {
        const content = await fs.readFile(path.join(publicDir, MANIFEST_FILENAME), 'utf8');
        const parsed = JSON.parse(content);
        entries = Array.isArray(parsed) ? parsed : [];
    }
    catch {
        // No manifest — preload links won't render, but font stylesheet still can
    }
    // Check if public/dndev-fonts.css exists (generated by copyFontsToPublic for Next.js)
    let hasFontsCss = false;
    try {
        await fs.access(path.join(publicDir, FONTS_CSS_FILENAME));
        hasFontsCss = true;
    }
    catch {
        // No generated font CSS — fonts come from @fontsource CSS imports (Vite)
    }
    if (entries.length === 0 && !hasFontsCss)
        return null;
    return (_jsxs(_Fragment, { children: [hasFontsCss && _jsx("link", { rel: "stylesheet", href: `/${FONTS_CSS_FILENAME}` }), entries.map((e) => (_jsx("link", { rel: "preload", as: "font", href: e.href, type: e.type ?? 'font/woff2', crossOrigin: "anonymous" }, e.href)))] }));
}
export default FontPreloadLinks;
