"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DevBuildController = void 0;
//#region imports
const lib_1 = require("taon/lib");
const lib_2 = require("tnp-core/lib");
const dev_mode_models_1 = require("../dev-mode/dev-mode.models");
const dev_build_repository_1 = require("./dev-build.repository");
//#endregion
let DevBuildController = class DevBuildController extends lib_1.TaonBaseController {
    devBuildRepository = this.injectKvRepository(dev_build_repository_1.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);
            lib_2.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();
            lib_2.UtilsTerminal.drawHorizontalLine();
            console.log(lib_2.chalk.green(`

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

        `));
            lib_2.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
    }
};
exports.DevBuildController = DevBuildController;
__decorate([
    (0, lib_1.HTML)({
        pathIsGlobal: true,
        path: '/info',
    }),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", Object)
], DevBuildController.prototype, "info", null);
__decorate([
    (0, lib_1.GET)(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", Object)
], DevBuildController.prototype, "getAllData", null);
__decorate([
    (0, lib_1.GET)(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", Object)
], DevBuildController.prototype, "getStatusInfo", null);
__decorate([
    (0, lib_1.GET)(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", Object)
], DevBuildController.prototype, "getProjectInfo", null);
__decorate([
    (0, lib_1.POST)(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", Object)
], DevBuildController.prototype, "healthCheck", null);
__decorate([
    (0, lib_1.POST)(),
    __param(0, (0, lib_1.Query)('buildType')),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String]),
    __metadata("design:returntype", Object)
], DevBuildController.prototype, "unlockLeaderQueue", null);
__decorate([
    (0, lib_1.POST)(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", Object)
], DevBuildController.prototype, "setLeadBuildDirtyIfRunning", null);
__decorate([
    (0, lib_1.POST)(),
    __param(0, (0, lib_1.Query)('startedBy')),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String]),
    __metadata("design:returntype", Object)
], DevBuildController.prototype, "displayRebuildDoneMessage", null);
__decorate([
    (0, lib_1.POST)(),
    __param(0, (0, lib_1.Query)('buildType')),
    __param(1, (0, lib_1.Query)('status')),
    __param(2, (0, lib_1.Query)('leaderPort')),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, String, Object]),
    __metadata("design:returntype", Object)
], DevBuildController.prototype, "updateTaonBuildStatus", null);
exports.DevBuildController = DevBuildController = __decorate([
    (0, lib_1.TaonController)({
        className: 'DevBuildController',
    })
], DevBuildController);
//# sourceMappingURL=dev-build.controller.js.map