"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.GenericService = void 0;
const Utils_js_1 = require("../common/Utils.js");
const LevelLogs_js_1 = require("../common/LevelLogs.js");
const API_ID = "API_CALL - ";
class GenericService extends LevelLogs_js_1.LevelLogs {
    get startConfig() {
        return this._startConfig;
    }
    getAccessorName() { return "GenericService"; }
    constructor(_logger, logId = "UNDF/SVCE - ", _eventEmitter) {
        super();
        this.ready = false;
        this.startingInfos = {
            constructorDate: new Date(),
            startDate: new Date(),
            startedDate: new Date(),
            initilizedDate: new Date(),
            readyDate: new Date()
        };
        this._eventEmitter = _eventEmitter;
        this.setLogLevels(this);
        let that = this;
        that._started = false;
        that._logger = _logger;
        if (logId) {
            that._logId = logId;
        }
        // that._logger.log(that.DEBUG, that._logId + "(GenericService::constructor) " );
        that.setConstructed();
    }
    initStartDate() {
        this.startingInfos.startDate = this._core?.startDate;
    }
    cleanMemoryCache() {
        let that = this;
        // that._logger.log(that.DEBUG, that._logId + "(GenericService::cleanMemoryCache) Not defined for this module." );
    }
    get startedDuration() {
        return Math.round(this.startingInfos.startedDate.getTime() - this.startingInfos.startDate.getTime());
    }
    get initializedDuration() {
        return Math.round(this.startingInfos.initilizedDate.getTime() - this.startingInfos.startDate.getTime());
    }
    setConstructed() {
        let that = this;
        that.startingInfos.constructorDate = new Date();
        that._logger.log(that.INFO, that._logId + `=== CONSTRUCTED at (${that.startingInfos.constructorDate} ===`);
        that._logger.log("info", that._logId + `=== CONSTRUCTED at (${that.startingInfos.constructorDate} ===`);
        /*
        that._logger.log(that.DEBUG, that._logId + `=== CONSTRUCTED at (${that.startingInfos.constructorDate} ===`);
        that._logger.log("debug", that._logId + `=== CONSTRUCTED at (${that.startingInfos.constructorDate} ===`);
        that._logger.log(that.ERROR, that._logId + `=== CONSTRUCTED at (${that.startingInfos.constructorDate} ===`);
        that._logger.log("error", that._logId + `=== CONSTRUCTED at (${that.startingInfos.constructorDate} ===`);
        that._logger.log(that.WARN, that._logId + `=== CONSTRUCTED at (${that.startingInfos.constructorDate} ===`);
        that._logger.log("warn", that._logId + `=== CONSTRUCTED at (${that.startingInfos.constructorDate} ===`);
        // */
    }
    setStarted() {
        let that = this;
        that.startingInfos.startedDate = new Date();
        let timeSinceStart = (0, Utils_js_1.msToTime)(that.startedDuration);
        that._logger.log(that.INFO, that._logId + `=== STARTED (${that.startedDuration} ms => ${timeSinceStart} ) ===`);
        that._started = true;
    }
    setInitialized() {
        let that = this;
        that.startingInfos.initilizedDate = new Date();
        let timeSinceStart = (0, Utils_js_1.msToTime)(that.initializedDuration);
        that._logger.log(that.INFO, that._logId + `=== INITIALIZED (${that.initializedDuration} ms => ${timeSinceStart} ) ===`);
        that._initialized = true;
        that._eventEmitter.emit("evt_internal_serviceinitialized", { "name": that.getAccessorName(), "infos": { "msSinceStart": that.initializedDuration, "timeSinceStart": timeSinceStart } });
    }
    setStopped() {
        let that = this;
        that._started = false;
        that._initialized = false;
        that._logger.log(that.INFO, that._logId + `=== STOPPED () ===`);
    }
    callRestMethod(methodName = "methodNameUnknown", lesarguments) {
        let that = this;
        that._logger.log(that.INFOAPI, that._logId + API_ID + "(" + methodName + ") "); //, that._logger.stripStringForLogs(companyId));
        return new Promise(function (resolve, reject) {
            try {
                that._core._rest[methodName](...lesarguments).then((result) => {
                    that._logger.log(that.DEBUG, that._logId + "(" + methodName + ") Successfully created.");
                    resolve(result);
                }).catch((err) => {
                    that._logger.log(that.ERROR, that._logId + "(" + methodName + ") ErrorManager .");
                    that._logger.log(that.INTERNALERROR, that._logId + "(" + methodName + ") ErrorManager  : ", err);
                    return reject(err);
                });
            }
            catch (err) {
                that._logger.log(that.INTERNALERROR, that._logId + "(" + methodName + ") error : ", err);
                return reject(err);
            }
        });
    }
}
exports.GenericService = GenericService;
module.exports = { 'GenericService': GenericService };
//# sourceMappingURL=GenericService.js.map