import { readdir } from 'fs/promises';
import { join } from 'path';
import { fileURLToPath } from 'url';
const REPO_ROOT = fileURLToPath(new URL('../../..', import.meta.url));
export async function makeDemoEnv(cwd = process.cwd()) {
    const iconsDir = join(cwd, 'elements', 'pfe-icon', 'icons');
    const iconSetNames = await readdir(iconsDir);
    const iconSets = await Promise.all(iconSetNames.reverse().map(async (set) => {
        const files = await readdir(join(iconsDir, set));
        const icons = files.map(x => x.replace('.js', ''));
        return [set, icons];
    }));
    return `
export const elements = ${JSON.stringify(await readdir(join(cwd, 'elements')))};
export const core = ${JSON.stringify(await readdir(join(cwd, 'core')))};
export const iconSets = new Map(${JSON.stringify(iconSets)});
`;
}
/**
 * Generates the contents of `environment.js`,
 * a file which contains metadata about the monorepo.
 */
export function pfeEnvPlugin({ cwd = REPO_ROOT, } = {}) {
    return {
        name: 'pfe-env',
        setup(build) {
            build.onResolve({ filter: /@patternfly\/pfe-tools\/environment\.js/ }, () => ({
                path: '@patternfly/pfe-tools/environment.js',
                namespace: 'pfe-env',
            }));
            build.onLoad({ filter: /.*/, namespace: 'pfe-env' }, async () => ({
                contents: await makeDemoEnv(cwd),
                loader: 'js'
            }));
        },
    };
}
//# sourceMappingURL=pfe-env.js.map