//#region imports
import { Taon, TaonController, GET, Query, TaonBaseController, HTML, POST, } from 'taon/lib';
import { chalk, CoreModels, Helpers } from 'tnp-core/lib';
import { DevMode } from '../dev-mode/dev-mode.models';
import { DevBuildRepository } from './dev-build.repository';
//#endregion
let DevBuildController = class DevBuildController extends TaonBaseController {
    devBuildRepository = this.injectKvRepository(DevBuildRepository);
    //#region API / html info
    info() {
        //#region @backendFunc
        return async (req, res) => {
            const currentCommand = await this.devBuildRepository.get('currentCommand');
            const commandStatus = await this.devBuildRepository.get('commandStatus');
            // const jsonDbLocation = this.devBuildRepository.jsonDbLocation;
            // console.log({
            //   currentCommand,
            //   commandStatus,
            //   jsonDbLocation,
            // });
            const project = this.devBuildRepository.getProject();
            return `
<html>
<head><title>Action on project Info</title></head>
<body>
    <h1>Project path: "${project?.location}" is</h1>
    <h1>command name: ${currentCommand} </h1>
    <h1>command status: ${commandStatus} </h1>
    <h4>version: ${project?.packageJson?.version}</h4>
    <h4>pid: ${process.pid}</h4><br>
    <script>
     // setTimeout(() => {
     //   location.reload();
     // }, 2000);
    </script>
</body>
<html>
      `;
        };
        //#endregion
    }
    //#endregion
    //#region API / get all data
    getAllData() {
        //#region @websqlFunc
        return async (req, res) => {
            return await this.devBuildRepository.getAllData();
        };
        //#endregion
    }
    //#endregion
    //#region API / get status info
    getStatusInfo() {
        //#region @backenFunc
        return async (req, res) => {
            // console.log(`instance ${this[Symbols.taonInstanceId]}`);
            const project = this.devBuildRepository.getProject();
            return project.taonBuildObserver.buildStatusInfo;
        };
        //#endregion
    }
    //#endregion
    //#region API / get status info
    getProjectInfo() {
        //#region @backenFunc
        return async (req, res) => {
            // console.log(`instance ${this[Symbols.taonInstanceId]}`);
            const projectBuild = this.devBuildRepository.getProjectInfo();
            return projectBuild;
        };
        //#endregion
    }
    //#endregion
    //#region API / health heck
    healthCheck() {
        //#region @backenFunc
        return async (req, res) => {
            return true;
        };
        //#endregion
    }
    //#endregion
    //#region API / unlock leader build queue
    unlockLeaderQueue(buildType) {
        //#region @backenFunc
        return async (req, res) => {
            const project = this.devBuildRepository.getProject();
            const notifier = project.taonBuildObserver.leader.buildsNotifiers.get(buildType);
            Helpers.logInfo(`unlocking ${buildType} in leader`);
            notifier.next();
            return true;
        };
        //#endregion
    }
    //#endregion
    //#region API / unlock leader build queue
    setLeadBuildDirtyIfRunning() {
        //#region @backenFunc
        return async (req, res) => {
            const project = this.devBuildRepository.getProject();
            project.taonBuildObserver.leader.setLeadBuildDirtyIfRunning();
            return true;
        };
        //#endregion
    }
    //#endregion
    //#region API / display rebuild done message
    displayRebuildDoneMessage(startedBy) {
        //#region @backenFunc
        return async (req, res) => {
            const project = this.devBuildRepository.getProject();
            // UtilsTerminal.drawHorizontalLine();
            console.log(chalk.green(`

    REBUILD STARED BY ${startedBy}
    DONE FOR ${chalk.bold(project.nameForNpmPackage)}

        `));
            // UtilsTerminal.drawHorizontalLine();
            return true;
        };
        //#endregion
    }
    //#endregion
    //#region API / update taon build status in child
    updateTaonBuildStatus(buildType, status, leaderPort) {
        //#region @backenFunc
        return async (req, res) => {
            if (leaderPort) {
                leaderPort = Number(leaderPort);
            }
            const project = this.devBuildRepository.getProject();
            project.taonBuildObserver.toNotifyLeaderPort = Number(leaderPort);
            project.taonBuildObserver.states.get(buildType).set(status);
            return project.taonBuildObserver.buildStatusInfo;
        };
        //#endregion
    }
};
__decorate([
    HTML({
        pathIsGlobal: true,
        path: '/info',
    }),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", Object)
], DevBuildController.prototype, "info", null);
__decorate([
    GET(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", Object)
], DevBuildController.prototype, "getAllData", null);
__decorate([
    GET(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", Object)
], DevBuildController.prototype, "getStatusInfo", null);
__decorate([
    GET(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", Object)
], DevBuildController.prototype, "getProjectInfo", null);
__decorate([
    POST(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", Object)
], DevBuildController.prototype, "healthCheck", null);
__decorate([
    POST(),
    __param(0, Query('buildType')),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String]),
    __metadata("design:returntype", Object)
], DevBuildController.prototype, "unlockLeaderQueue", null);
__decorate([
    POST(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", Object)
], DevBuildController.prototype, "setLeadBuildDirtyIfRunning", null);
__decorate([
    POST(),
    __param(0, Query('startedBy')),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String]),
    __metadata("design:returntype", Object)
], DevBuildController.prototype, "displayRebuildDoneMessage", null);
__decorate([
    POST(),
    __param(0, Query('buildType')),
    __param(1, Query('status')),
    __param(2, Query('leaderPort')),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, String, Object]),
    __metadata("design:returntype", Object)
], DevBuildController.prototype, "updateTaonBuildStatus", null);
DevBuildController = __decorate([
    TaonController({
        className: 'DevBuildController',
    })
], DevBuildController);
export { DevBuildController };
//# sourceMappingURL=dev-build.controller.js.map