"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var moment = require("moment");
var request = require("superagent");
var ApiService = /** @class */ (function () {
    function ApiService() {
    }
    ApiService.prototype.executeRequest = function (params, headers) {
        return new Promise(function (resolve, reject) {
            var req = request(params.method, params.url)
                .set('Content-Type', 'application/json');
            if (params.apiKeyId) {
                req = req.set('X-API-KEY-ID', params.apiKeyId);
            }
            if (headers) {
                Object.keys(headers).forEach(function (key) {
                    req = req.set(key, headers[key]);
                });
            }
            var queryParameters = params.queryParameters;
            if (queryParameters) {
                Object.keys(queryParameters).forEach(function (key) {
                    var value = queryParameters[key];
                    if (Object.prototype.toString.call(value) === '[object Date]') {
                        queryParameters[key] = moment(value).format();
                    }
                });
                req = req.query(queryParameters);
            }
            if (params.body) {
                req.send(params.body);
            }
            req.end(function (error, response) {
                if (error || !response.ok) {
                    if (response && response.body) {
                        var customError = new Error(response.body.message);
                        customError.status = response.body.status;
                        customError.type = response.body.type;
                        reject(customError);
                        return;
                    }
                    reject(error);
                }
                else {
                    resolve(response.body);
                }
            });
        });
    };
    return ApiService;
}());
exports.ApiService = ApiService;
//# sourceMappingURL=api-service.js.map