import { createRequire } from "node:module";
const rstest_config_require = createRequire(import.meta.url);
function createDefaultRstestConfig() {
    return {
        testEnvironment: 'jsdom',
        setupFiles: [
            rstest_config_require.resolve('./setupFiles/rstest')
        ],
        globals: true
    };
}
function normalizeSetupFiles(setupFiles) {
    if (!setupFiles) return [];
    return Array.isArray(setupFiles) ? setupFiles : [
        setupFiles
    ];
}
async function applyRstestConfigModifier(config, modifyRstestConfig) {
    if (!modifyRstestConfig) return config;
    return await modifyRstestConfig(config);
}
function withDefaultConfig(options) {
    return async ()=>await applyRstestConfigModifier(createDefaultRstestConfig(), options?.modifyRstestConfig);
}
function withLynxConfig(options) {
    return async ()=>{
        const { loadConfig } = await import("@lynx-js/rspeedy");
        const lynxConfig = await loadConfig({
            cwd: options?.rootPath,
            configPath: options?.configPath
        });
        const { toRstestConfig } = await import("./0~@rstest/adapter-rsbuild.js");
        const rstestConfig = toRstestConfig({
            rsbuildConfig: lynxConfig.content
        });
        const defaultConfig = createDefaultRstestConfig();
        const setupFiles = Array.from(new Set([
            ...normalizeSetupFiles(rstestConfig.setupFiles),
            ...normalizeSetupFiles(defaultConfig.setupFiles)
        ]));
        const mergedConfig = {
            ...rstestConfig,
            ...defaultConfig,
            plugins: [
                ...rstestConfig.plugins || [],
                {
                    name: 'lynx-adapter:remove-useless-plugins',
                    remove: [
                        'lynx:rsbuild:qrcode'
                    ],
                    setup: ()=>{}
                }
            ],
            setupFiles
        };
        return await applyRstestConfigModifier(mergedConfig, options?.modifyRstestConfig);
    };
}
export { withDefaultConfig, withLynxConfig };
