// Server-side artifact library loader. This path is synchronous because the
// render/export pipeline runs in Node and reads bundled library sources from
// disk before handing them to Playwright.
import { readFileSync } from 'fs';
import { createRequire } from 'module';
import { dirname, join } from 'path';
const require = createRequire(import.meta.url);
const LIBRARY_PATHS = {
    d3: 'd3/dist/d3.min.js',
    motion: 'motion/dist/motion.js',
    three: 'three/build/three.module.min.js',
    threeCore: 'three/build/three.core.min.js',
};
const cache = {};
function packageRoot(packageName) {
    let current = dirname(require.resolve(packageName));
    while (current !== dirname(current)) {
        try {
            const pkg = JSON.parse(readFileSync(join(current, 'package.json'), 'utf8'));
            if (pkg.name === packageName)
                return current;
        }
        catch {
        }
        current = dirname(current);
    }
    throw new Error(`Could not resolve package root for ${packageName}`);
}
function readLibrarySource(key) {
    const [packageName, ...parts] = LIBRARY_PATHS[key].split('/');
    cache[key] ??= readFileSync(join(packageRoot(packageName), ...parts), 'utf8');
    return cache[key];
}
export function artifactLibrarySourcesForBundle(bundle) {
    const sources = {};
    for (const key of bundle.libraries ?? []) {
        sources[key] = readLibrarySource(key);
        if (key === 'three') {
            sources.threeCore = readLibrarySource('threeCore');
        }
    }
    return sources;
}
//# sourceMappingURL=artifact-library-sources.js.map