import * as Solid from 'solid-js';
import { _getAssetMatches, replaceEqualDeep } from '@tanstack/router-core';
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 scripts = Solid.createMemo((previous) => {
        const matches = _getAssetMatches(router.stores.matches.get());
        const next = [];
        const assets = [];
        const manifest = router.ssr?.manifest;
        for (const match of matches) {
            for (const script of match.scripts ?? []) {
                if (!script) {
                    continue;
                }
                const { children, ...attrs } = script;
                next.push({
                    tag: 'script',
                    attrs: { ...attrs, nonce },
                    children: children,
                });
            }
            for (const asset of manifest?.routes[match.routeId]?.scripts ?? []) {
                assets.push({
                    tag: 'script',
                    attrs: { ...asset.attrs, nonce },
                    children: asset.children,
                });
            }
        }
        next.push(...assets);
        return previous ? replaceEqualDeep(previous, next) : next;
    });
    const serverBufferedScript = (isServer ?? router.isServer) && router.serverSsr
        ? router.serverSsr.takeBufferedScripts()
        : undefined;
    return (<>
      {serverBufferedScript && <Asset {...serverBufferedScript}/>}
      <Solid.For each={scripts()}>{(asset) => <Asset {...asset}/>}</Solid.For>
    </>);
};
//# sourceMappingURL=Scripts.jsx.map