"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.bundle = bundle;
const esbuild_1 = require("esbuild");
const fs_extra_1 = require("fs-extra");
const promises_1 = require("fs/promises");
const path = require("path");
const sync_1 = require("../script/sync");
const collectIdSet_1 = require("./collectIdSet");
const constants_1 = require("./constants");
async function bundle(unitPath, projectPath, outputFolder, includeSystem, opt) {
    const bundlePath = path.join(projectPath, 'bundle.json');
    const bootPath = path.join(projectPath, 'system.json');
    const systemPath = path.join(unitPath, 'src', 'system');
    const entrypoint = path.join(unitPath, 'src/client/platform/web/index.ts');
    const fallbackBootPath = path.join(unitPath, 'src/client/platform/web/system.json');
    const outputPath = path.join(outputFolder, 'index.js');
    const bootPathExists = await (0, fs_extra_1.pathExists)(bootPath);
    const bundle = await (0, fs_extra_1.readJSON)(bundlePath);
    const set = includeSystem ? undefined : (0, collectIdSet_1.collectIdSetFromBundle)(bundle);
    await (0, sync_1.sync)(systemPath, outputFolder, set);
    const resolvePlugin = {
        name: 'resolve',
        setup(build) {
            build.onResolve({ filter: /.*/ }, (args) => {
                const { resolveDir, importer } = args;
                if (importer === entrypoint) {
                    if (args.path === './bundle.json') {
                        return { path: bundlePath };
                    }
                    if (args.path === './system.json') {
                        if (bootPathExists) {
                            return { path: bootPath };
                        }
                        else {
                            return { path: fallbackBootPath };
                        }
                    }
                }
                if (importer.endsWith('_classes.ts')) {
                    const otherPath = `${path.join(systemPath, args.path, 'index.ts')}`;
                    return { path: otherPath };
                }
                if (importer.endsWith('_components.ts')) {
                    const otherPath = `${path.join(systemPath, args.path)}.ts`;
                    return { path: otherPath };
                }
                if (args.path.endsWith('/system/_specs') ||
                    args.path.endsWith('/system/_components') ||
                    args.path.endsWith('/system/_ids') ||
                    args.path.endsWith('/system/_classes')) {
                    const absolutePath = path
                        .join(resolveDir, args.path)
                        .replace(systemPath, '')
                        .replace('.ts', '');
                    const otherPath = path.join(outputFolder, `${absolutePath}.ts`);
                    return { path: otherPath };
                }
                return {};
            });
        },
    };
    const esbuildOptions = {
        ...constants_1.DEFAULT_BUILD_OPTIONS,
        entryPoints: [entrypoint],
        plugins: [resolvePlugin],
        outfile: outputPath,
        ...opt,
    };
    await (0, esbuild_1.build)(esbuildOptions);
    await (0, promises_1.unlink)(path.join(outputFolder, '_specs.ts'));
    await (0, promises_1.unlink)(path.join(outputFolder, '_ids.ts'));
    await (0, promises_1.unlink)(path.join(outputFolder, '_classes.ts'));
    await (0, promises_1.unlink)(path.join(outputFolder, '_components.ts'));
}
//# sourceMappingURL=bundle.js.map