"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.HttpClientImpl = void 0;
const axios_1 = __importDefault(require("axios"));
const isNumber_1 = __importDefault(require("lodash/isNumber"));
const isString_1 = __importDefault(require("lodash/isString"));
const config_1 = require("../config");
const logger_1 = require("../logger");
const request_params_serializer_1 = require("../util/request-params-serializer");
const http_client_interface_1 = require("./http-client.interface");
class HttpClientImpl {
    constructor() {
        this.log = logger_1.logger.getLogger('http:client');
        this.log.debug('HTTP settings:', this.httpConfig);
    }
    get defaultConfig() {
        return {
            paramsSerializer: request_params_serializer_1.commaRequestParamsSerializer,
            timeout: this.getTimeoutSetting(),
        };
    }
    get httpConfig() {
        return config_1.config.get('http');
    }
    static getInstance() {
        if (!this.instance) {
            this.instance = new this();
        }
        return this.instance;
    }
    request(config) {
        return axios_1.default.request(this.mergeDefaults(config));
    }
    post(url, data, config = {}) {
        return axios_1.default.post(url, data, this.mergeDefaults(config));
    }
    mergeDefaults(config) {
        return Object.assign(Object.assign({}, this.defaultConfig), config);
    }
    isValidTimeoutNumber(value) {
        return (0, isNumber_1.default)(value) && Math.sign(value) >= 0 && Number.isFinite(value) && value <= Number.MAX_SAFE_INTEGER;
    }
    getTimeoutSetting() {
        let timeout = this.httpConfig['request-timeout'];
        timeout = (0, isString_1.default)(timeout) ? Number(timeout) : timeout;
        if (!this.isValidTimeoutNumber(timeout)) {
            timeout = config_1.config.default(http_client_interface_1.TIMEOUT_CONFIG_PATH);
            this.log.warn(`Invalid value of the "${http_client_interface_1.TIMEOUT_CONFIG_PATH}". Default value "${timeout}" is used`);
        }
        return timeout * 1000;
    }
}
exports.HttpClientImpl = HttpClientImpl;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaHR0cC1jbGllbnQtaW1wbC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4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