"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.TaonBaseCliWorkerController = void 0;
const lib_1 = require("taon/lib");
const lib_2 = require("tnp-core/lib");
const base_cli_worker_config_1 = require("./base-cli-worker-config");
const base_cli_worker_utils_1 = require("./base-cli-worker.utils");
let TaonBaseCliWorkerController = class TaonBaseCliWorkerController extends lib_1.TaonBaseController {
    /**
     * service id
     */
    cliWorkerServiceId = null;
    cliWorkerServiceVersion = null;
    //#region api methods / initialize metadata
    baseCLiWorkerCommand_initializeMetadata(serviceId, serviceVersion) {
        //#region @backendFunc
        return async () => {
            this.cliWorkerServiceId = serviceId;
            this.cliWorkerServiceVersion = serviceVersion;
        };
        //#endregion
    }
    //#endregion
    //#region api methods / kill
    baseCLiWorkerCommand_kill(reason) {
        //#region @backendFunc
        return async () => {
            console.log(`Killing worker "${this.cliWorkerServiceId}"...`);
            console.log(`Reason ${reason}`);
            setTimeout(async () => {
                console.log(`Destroying context worker "${this.cliWorkerServiceId}"...`);
                lib_2.Helpers.removeFileIfExists(base_cli_worker_utils_1.BaseCliWorkerUtils.getPathToProcessLocalInfoJson(this.cliWorkerServiceId));
                process.exit(0);
            }); // TODO may be change to 0
        };
        //#endregion
    }
    //#endregion
    //#region api methods / info
    info() {
        //#region @backendFunc
        return async () => {
            return `
<html>
<head><title>Service Info</title></head>
<body>
    <h1>Service "${this.cliWorkerServiceId}" is</h1>
    <h1>running healthy on port ${this.ctx.uriPort} </h1>
    <h4>version: ${this.cliWorkerServiceVersion}</h4>
    <h4>pid: ${process.pid}</h4><br>
</body>
<html>
      `;
        };
        //#endregion
    }
    //#endregion
    //#region api methods / is healthy
    baseCLiWorkerCommand_isHealthy(checkingProcessConfig) {
        //#region @backendFunc
        return async (req, res) => {
            checkingProcessConfig = base_cli_worker_config_1.BaseCliWorkerConfig.from(checkingProcessConfig);
            const currentConfig = base_cli_worker_config_1.BaseCliWorkerConfig.from({
                pid: process.pid,
                serviceID: this.cliWorkerServiceId,
                port: Number(this.ctx.uriPort),
                version: this.cliWorkerServiceVersion,
            });
            // console.log('configWorker', configWorker);
            // console.log('currentConfig', currentConfig);
            return checkingProcessConfig.isEquals(currentConfig);
        };
        //#endregion
    }
    //#endregion
    //#region api methods / has up to date version
    baseCLiWorkerCommand_hasUpToDateVersion(checkingProcessConfig) {
        //#region @backendFunc
        return async (req, res) => {
            checkingProcessConfig = base_cli_worker_config_1.BaseCliWorkerConfig.from(checkingProcessConfig);
            const currentWorkerConfig = base_cli_worker_config_1.BaseCliWorkerConfig.from({
                pid: process.pid,
                serviceID: this.cliWorkerServiceId,
                port: Number(this.ctx.uriPort),
                version: this.cliWorkerServiceVersion,
            });
            // console.log('checkingProcessConfig', checkingProcessConfig);
            // console.log('currentConfig', currentWorkerConfig);
            return currentWorkerConfig.hasBiggerOrEqualWorkerVersionThan(checkingProcessConfig);
        };
        //#endregion
    }
};
exports.TaonBaseCliWorkerController = TaonBaseCliWorkerController;
__decorate([
    (0, lib_1.PUT)(),
    __param(0, (0, lib_1.Body)('serviceId')),
    __param(1, (0, lib_1.Body)('serviceVersion')),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, String]),
    __metadata("design:returntype", Object)
], TaonBaseCliWorkerController.prototype, "baseCLiWorkerCommand_initializeMetadata", null);
__decorate([
    (0, lib_1.GET)(),
    __param(0, (0, lib_1.Query)('reason')),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String]),
    __metadata("design:returntype", Object)
], TaonBaseCliWorkerController.prototype, "baseCLiWorkerCommand_kill", null);
__decorate([
    (0, lib_1.HTML)({
        pathIsGlobal: true,
        path: '/info',
    }),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", Object)
], TaonBaseCliWorkerController.prototype, "info", null);
__decorate([
    (0, lib_1.POST)(),
    __param(0, (0, lib_1.Body)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [base_cli_worker_config_1.BaseCliWorkerConfig]),
    __metadata("design:returntype", Object)
], TaonBaseCliWorkerController.prototype, "baseCLiWorkerCommand_isHealthy", null);
__decorate([
    (0, lib_1.POST)(),
    __param(0, (0, lib_1.Body)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [base_cli_worker_config_1.BaseCliWorkerConfig]),
    __metadata("design:returntype", Object)
], TaonBaseCliWorkerController.prototype, "baseCLiWorkerCommand_hasUpToDateVersion", null);
exports.TaonBaseCliWorkerController = TaonBaseCliWorkerController = __decorate([
    (0, lib_1.TaonController)({
        className: 'TaonBaseCliWorkerController',
    })
], TaonBaseCliWorkerController);
//# sourceMappingURL=base-cli-worker-controller.js.map