/**
 * Lazy compilation middleware. Using the same logic as HMR middleware
 */
import { relative } from 'node:path';
import { VIRTUAL_FARM_DYNAMIC_IMPORT_SUFFIX, bold, clearScreen, cyan, green } from '../../index.js';
import { existsSync } from 'node:fs';
import { logError } from '../error.js';
export function lazyCompilation(devSeverContext) {
    const compiler = devSeverContext.getCompiler();
    if (!compiler.config.config?.lazyCompilation) {
        return;
    }
    return async (ctx, next) => {
        if (ctx.path === '/__lazy_compile') {
            const paths = ctx.query.paths.split(',');
            const pathsStr = paths
                .map((p) => {
                if (p.startsWith('/') &&
                    !p.endsWith(VIRTUAL_FARM_DYNAMIC_IMPORT_SUFFIX) &&
                    !existsSync(p)) {
                    return p;
                }
                const resolvedPath = compiler.transformModulePath(compiler.config.config.root, p);
                return relative(compiler.config.config.root, resolvedPath);
            })
                .join(', ');
            clearScreen();
            devSeverContext.logger.info(`Lazy compiling ${bold(cyan(pathsStr))}`);
            const start = Date.now();
            // sync update when node is true
            let result;
            try {
                // sync regenerate resources
                result = await compiler.update(paths, true, false, false);
            }
            catch (e) {
                logError(e);
            }
            if (!result) {
                return;
            }
            if (ctx.query.node) {
                compiler.writeResourcesToDisk();
            }
            devSeverContext.logger.info(`${bold(green(`✓`))} Lazy compilation done(${bold(cyan(pathsStr))}) in ${bold(green(`${Date.now() - start}ms`))}.`);
            if (result) {
                let dynamicResourcesMap = null;
                if (result.dynamicResourcesMap) {
                    for (const [key, value] of Object.entries(result.dynamicResourcesMap)) {
                        if (!dynamicResourcesMap) {
                            dynamicResourcesMap = {};
                        }
                        dynamicResourcesMap[key] = value.map((r) => ({
                            path: r[0],
                            type: r[1]
                        }));
                    }
                }
                const returnObj = `{
          "dynamicResourcesMap": ${JSON.stringify(dynamicResourcesMap)}
        }`;
                const code = !ctx.query.node
                    ? `export default ${returnObj}`
                    : returnObj;
                ctx.type = !ctx.query.node
                    ? 'application/javascript'
                    : 'application/json';
                ctx.body = code;
            }
            else {
                throw new Error(`Lazy compilation result not found for paths ${paths}`);
            }
        }
        else {
            await next();
        }
    };
}
//# sourceMappingURL=lazy-compilation.js.map