import * as Solid from 'solid-js';
import { isServer } from '@tanstack/router-core/isServer';
import { Asset } from './Asset';
import { useRouter } from './useRouter';
export const Scripts = () => {
    const router = useRouter();
    const nonce = router.options.ssr?.nonce;
    const getAssetScripts = (matches) => {
        const assetScripts = [];
        const manifest = router.ssr?.manifest;
        if (!manifest) {
            return [];
        }
        for (const match of matches) {
            const scripts = manifest.routes[match.routeId]?.scripts;
            if (!scripts) {
                continue;
            }
            for (const asset of scripts) {
                assetScripts.push({
                    tag: 'script',
                    attrs: { ...asset.attrs, nonce },
                    children: asset.children,
                });
            }
        }
        return assetScripts;
    };
    const getScripts = (matches) => matches
        .map((match) => match.scripts)
        .flat(1)
        .filter(Boolean).map(({ children, ...script }) => ({
        tag: 'script',
        attrs: {
            ...script,
            nonce,
        },
        children,
    }));
    const activeMatches = Solid.createMemo(() => router.stores.matches.get());
    const assetScripts = Solid.createMemo(() => getAssetScripts(activeMatches()));
    const scripts = Solid.createMemo(() => getScripts(activeMatches()));
    return renderScripts(router, scripts(), assetScripts());
};
function renderScripts(router, scripts, assetScripts) {
    const allScripts = [...scripts, ...assetScripts];
    if ((isServer ?? router.isServer) && router.serverSsr) {
        const serverBufferedScript = router.serverSsr.takeBufferedScripts();
        if (serverBufferedScript) {
            allScripts.unshift(serverBufferedScript);
        }
    }
    return (<>
      {allScripts.map((asset) => (<Asset {...asset}/>))}
    </>);
}
//# sourceMappingURL=Scripts.jsx.map