import { Taon, TaonBaseContext } from 'taon/lib';
import { crossPlatformPath, Helpers, Utils, _, config, tnpPackageName, } from 'tnp-core/lib';
import { DevBuildContext } from './dev-build.abstract.context';
import { DevBuildController } from './dev-build.controller';
import { DevBuildModels } from './dev-build.models';
import { DevBuildRepository } from './dev-build.repository';
export var DevBuildUtils;
(function (DevBuildUtils) {
    DevBuildUtils.context = Taon.createContext(() => ({
        contextName: `ProjectActionContext`,
        contexts: { TaonBaseContext, DevBuildContext },
        database: false,
        kv_database: {
            dbLocationFn: ({ cwd, port, baseLocation, contextName, repositoryClassName, }) => {
                return crossPlatformPath([
                    baseLocation,
                    _.kebabCase(cwd.replace(/\./g, '___')), // each cwd and proces has diffrent db
                    `kv_${contextName}__${repositoryClassName}___port${port}.json`,
                ]);
            },
        },
        logs: {
        // routes: true,
        // db: true,
        },
        disabledRealtime: true,
        skipWritingServerRoutes: true,
    }));
    const contexts = new Map();
    DevBuildUtils.startAcionWorker = async () => {
        //#region @backendFunc
        const currentActionPort = await Utils.getFreePort({
            startFrom: DevBuildModels.START_PORT_BUID_PROCESS,
        });
        const workerMainContext = DevBuildUtils.context.cloneAsNormal({
            overrideHost: `http://localhost:${currentActionPort}`,
        });
        const wokerMainContextRef = await workerMainContext.initialize({
        // onlyMigrationRun,
        });
        contexts.set(currentActionPort, wokerMainContextRef);
        Helpers.logInfo(`Server started`);
        const devBuildController = await wokerMainContextRef.getInstanceBy(DevBuildController);
        const devBuildRepository = await wokerMainContextRef.getInstanceBy(DevBuildRepository);
        const exitProgramCleaningFn = () => {
            return new Promise(async (resolve) => {
                const allWorkers = devBuildRepository.allProjectsInProcess();
                for (const buildProj of allWorkers) {
                    try {
                        Helpers.logInfo(`Deleting from pool ${buildProj.nameForNpmPackage} ${buildProj.port} ${buildProj.location}`);
                        await devBuildRepository.deleteFromPool(buildProj);
                    }
                    catch (error) {
                        config.frameworkName === tnpPackageName && console.error(error);
                        Helpers.logWarn(`Not able to delete project from main worker projecs pool`);
                    }
                }
                for (const buildProj of allWorkers) {
                    try {
                        await contexts.get(Number(buildProj.port)).destroy();
                    }
                    catch (error) {
                        config.frameworkName === tnpPackageName && console.error(error);
                        Helpers.logWarn(`Not able to delete destry context for ${buildProj.nameForNpmPackage} ` +
                            `(port=${buildProj.port})`);
                    }
                }
                Helpers.info('Taon Build Exit Cleaning Done');
                Helpers.removeFileIfExists(devBuildRepository.jsonDbLocation);
                resolve();
            });
        };
        return {
            devBuildController,
            devBuildRepository,
            exitProgramCleaningFn,
            currentActionPort,
        };
        //#endregion
    };
})(DevBuildUtils || (DevBuildUtils = {}));
//# sourceMappingURL=dev-build.utils.js.map