"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Prometheus = void 0;
var Config_1 = require("./Config");
var openflow_api_1 = require("@openiap/openflow-api");
var Logger_1 = require("./Logger");
var template = [
    '====BEGIN LICENSE====',
    '{{&licenseVersion}}',
    '{{&email}}',
    '{{&expirationDate}}',
    '{{&serial}}',
    '=====END LICENSE====='
].join('\n');
var Prometheus = /** @class */ (function () {
    function Prometheus() {
    }
    Prometheus.configure = function (logger) {
        this._logger = logger;
        if (openflow_api_1.NoderedUtil.IsNullEmpty(this.license_public_key)) {
            this._logger.info("Missing license_public_key");
            return;
        }
        if (openflow_api_1.NoderedUtil.IsNullEmpty(Config_1.Config.license_key)) {
            this._logger.info("Missing license_key");
            return;
        }
        var validlicense = false;
        var licenserror = "Missing license key";
        var license_public_key = Buffer.from(this.license_public_key, "base64").toString("utf8");
        var license_key = Buffer.from(Config_1.Config.license_key, "base64").toString("ascii");
        try {
            var data = Logger_1.Logger.License.parse({
                publicKey: license_public_key,
                licenseFile: license_key,
                template: template
            });
            if (data.valid !== true) {
                licenserror = "license is not valid";
            }
            else {
                var now = new Date();
                var valdt = new Date(data.data.expirationDate);
                if (valdt < now) {
                    licenserror = "License has expired at " + valdt;
                }
                else {
                    validlicense = true;
                }
            }
        }
        catch (error) {
            licenserror = "license validation failed " + error;
        }
        // const register = new client.Registry()
        // const hostname = Config.getEnv("HOSTNAME", null);
        // const defaultLabels: any = {};
        // if (!NoderedUtil.IsNullEmpty(hostname)) defaultLabels["hostname"] = hostname;
        // // if (!NoderedUtil.IsNullEmpty(Config.namespace)) defaultLabels["namespace"] = Config.namespace;
        // register.setDefaultLabels(defaultLabels);
        // client.collectDefaultMetrics({ register })
        // this.app.all("/metrics", (req, res, next) => {
        //     this.setCORSHeaders(req, res);
        //     if (!validlicense) {
        //         var result = { message: licenserror, statusCode: 500 };
        //         console.error(licenserror); // Log error message in our server"s console
        //         var json = JSON.stringify(result);
        //         res.status(500).send(json); // All HTTP requests must have a response, so let's send back an error with its status code and message
        //         return;
        //     }
        //     res.setHeader('Content-Type', register.contentType)
        //     res.end(register.metrics())
        // });
        // return register;
    };
    Prometheus.setCORSHeaders = function (req, res) {
        if (req.headers["origin"] == null)
            res.setHeader("Access-Control-Allow-Origin", "*");
        if (req.headers["origin"] != null)
            res.setHeader("Access-Control-Allow-Origin", req.headers["origin"]);
        res.setHeader("Access-Control-Allow-Methods", "POST");
        res.setHeader("Access-Control-Allow-Headers", "accept, content-type");
    };
    Prometheus.license_public_key = Config_1.Config.getEnv("license_public_key", "LS0tLS1CRUdJTiBQVUJMSUMgS0VZLS0tLS0KTUlJQklqQU5CZ2txaGtpRzl3MEJBUUVGQUFPQ0FROEFNSUlCQ2dLQ0FRRUFyVVJncVpLcHpmbTJISEUxTUJ3YwpmZW5oMk1PZVZQbEJMS2dUM3l2TXIvQlpTSHVPb3pkaFprZ3JoNVVOSVlXZ1JYNVR1SDVoeVZ1ODJzOFhoR2RLCnZ5ajFGWGt2SHVubWp4ZFpGWEhDcjJCVE12Y09VTnZsTk5MWFpTOXh0YXE1cHdBemh0SlpjbmZLdTVHcjZNR1MKbVFxcHpVeDRWRVIyN3hSaVo0QlhXUkQ4MGNVaHAvTHF3UGswdHYwU0FTNTNZVXVRNkJIcWp1MDdHd3lFYzZaUQpJNjlLRVpLaHMvbk5GRHlVOHhwT0JoL0EwR1dMT0wrb01JeU9NSkZ6a2RPZURDQzlnVzNabmJBOHo0dHZUc3VHCmI3c2ZidTJlSE5MK09ucWlzamdFYStrZWlIQXZXaTlCa2dGbWdZWnNaWk5ENnovWkpHUi84MElzekV4THRqL3YKNFFJREFRQUIKLS0tLS1FTkQgUFVCTElDIEtFWS0tLS0tCg==");
    return Prometheus;
}());
exports.Prometheus = Prometheus;
//# sourceMappingURL=Prometheus.js.map