import { Body, GET, HTML, POST, PUT, Query, TaonBaseController, TaonController } from 'taon/lib-prod';
import { Helpers__NS__removeFileIfExists } from 'tnp-core/lib-prod';
import { BaseCliWorkerConfig } from './base-cli-worker-config';
import { BaseCliWorkerUtils__NS__getPathToProcessLocalInfoJson } from './base-cli-worker.utils';
let TaonBaseCliWorkerController = class TaonBaseCliWorkerController extends 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}"...`);
                Helpers__NS__removeFileIfExists(BaseCliWorkerUtils__NS__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 = BaseCliWorkerConfig.from(checkingProcessConfig);
            const currentConfig = 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 = BaseCliWorkerConfig.from(checkingProcessConfig);
            const currentWorkerConfig = 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
    }
};
__decorate([
    PUT(),
    __param(0, Body('serviceId')),
    __param(1, Body('serviceVersion')),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, String]),
    __metadata("design:returntype", Object)
], TaonBaseCliWorkerController.prototype, "baseCLiWorkerCommand_initializeMetadata", null);
__decorate([
    GET(),
    __param(0, Query('reason')),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String]),
    __metadata("design:returntype", Object)
], TaonBaseCliWorkerController.prototype, "baseCLiWorkerCommand_kill", null);
__decorate([
    HTML({
        pathIsGlobal: true,
        path: '/info',
    }),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", Object)
], TaonBaseCliWorkerController.prototype, "info", null);
__decorate([
    POST(),
    __param(0, Body()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [BaseCliWorkerConfig]),
    __metadata("design:returntype", Object)
], TaonBaseCliWorkerController.prototype, "baseCLiWorkerCommand_isHealthy", null);
__decorate([
    POST(),
    __param(0, Body()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [BaseCliWorkerConfig]),
    __metadata("design:returntype", Object)
], TaonBaseCliWorkerController.prototype, "baseCLiWorkerCommand_hasUpToDateVersion", null);
TaonBaseCliWorkerController = __decorate([
    TaonController({
        className: 'TaonBaseCliWorkerController',
    })
], TaonBaseCliWorkerController);
export { TaonBaseCliWorkerController };
//# sourceMappingURL=c:/Users/darek/projects/npm/taon-dev/tnp-helpers/dist-nocutsrc-prod/lib/base/classes/base-cli-worker/base-cli-worker-controller.js.map