"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
var base = require("../lib/index.js");
// https://en.wikipedia.org/wiki/List_of_HTTP_status_codes
let statuses = {
    200: "OK",
    206: "Partial Content",
    400: "Bad Request",
    404: "Not Found",
    429: "Too Many Requests",
    500: "Internal Server Error",
    503: "Service Unavailable"
};
class StaticMethods {
    /**
    * Return a response to the caller
    * @param res
    * @param status_code { 200: "OK", 206: "Partial Content", 400: "Bad Request", 404: "Not Found", 429: "Too Many Requests", 500: "Internal Server Error", 503: "Service Unavailable" }
    * @param message
    * @param data
    */
    static sendResponse(res, status_code, message = undefined, data = {}, loggingLevel = base.LOG_LEVEL.INFO) {
        let status = statuses[status_code] || "Unknown";
        let response = JSON.stringify({ status: status, message: message, data: data });
        res.status(status_code);
        res.setHeader('Content-Type', 'application/json');
        res.send(response);
        base.logger().log(response, loggingLevel);
    }
    /**
    * wrap a method in promisefy
    *
    * THIS has the expectation that he last params of the method is of type function(succes:boolean, data:any)
    */
    static promisefy(method, options) {
        return __awaiter(this, void 0, void 0, function* () {
            if (options.bind == undefined) {
                options.bind = method;
            }
            if (options.controller == undefined) {
                options.controller = function (succes, data) {
                    if (succes) {
                        if (options.resolve != undefined)
                            options.resolve(data);
                    }
                    else {
                        if (options.reject != undefined)
                            options.reject(data);
                    }
                };
            }
            try {
                let result = yield new Promise(function (resolve, reject) {
                    options.resolve = resolve;
                    options.reject = reject;
                    method.bind(options.bind)(...options.args, options.controller);
                });
                return { success: true, data: result };
            }
            catch (e) {
                return { success: false, data: e };
            }
        });
    }
}
exports.default = StaticMethods;
//# sourceMappingURL=statics.js.map