"use strict";
/**
 * Fireblocks API
 * Fireblocks provides a suite of applications to manage digital asset operations and a complete development platform to build your business on the blockchain.  - Visit our website for more information: [Fireblocks Website](https://fireblocks.com) - Visit our developer docs: [Fireblocks DevPortal](https://developers.fireblocks.com)
 *
 * The version of the OpenAPI document: 1.6.2
 * Contact: developers@fireblocks.com
 *
 * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
 * https://openapi-generator.tech
 * Do not edit the class manually.
 */
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    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) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
    return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (g && (g = 0, op[0] && (_ = 0)), _) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AxiosManager = void 0;
var package_json_1 = require("../package.json");
var os_1 = __importDefault(require("os"));
var platform_1 = __importDefault(require("platform"));
var axios_1 = __importDefault(require("axios"));
var bearerTokenProvider_1 = require("./bearerTokenProvider");
var fireblocksError_1 = require("../error/fireblocksError");
var API_KEY_HEADER_NAME = "X-API-Key";
var AUTHORIZATION_HEADER_NAME = "Authorization";
var USER_AGENT_HEADER_NAME = "User-Agent";
var CONTENT_TYPE_HEADER_NAME = "Content-Type";
var AxiosManager = /** @class */ (function () {
    /**
    * Configures the http authentication with the required details.
    *
    * @param apiKey the API key to use
    * @param secretKey the secret key to use
    * @param additionalOptions additional options to configure the SDK and the underlying HTTP client
    */
    function AxiosManager(apiKey, secretKey, additionalOptions) {
        var _this = this;
        this.apiKey = apiKey;
        this.secretKey = secretKey;
        this.additionalOptions = additionalOptions;
        this.tokenProvider = new bearerTokenProvider_1.BearerTokenProvider(apiKey, secretKey);
        this.axiosInstance = axios_1.default.create();
        // Request interceptor for API calls
        this.axiosInstance.interceptors.request.use(function (config) { return __awaiter(_this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                config.headers = __assign(__assign({}, config.headers), this.getHeaders(config));
                return [2 /*return*/, config];
            });
        }); }, function (error) {
            Promise.reject((0, fireblocksError_1.handleError)(error));
        });
        // Response interceptor for API calls
        this.axiosInstance.interceptors.response.use(function (response) { return response; }, function (error) {
            throw (0, fireblocksError_1.handleError)(error);
        });
    }
    AxiosManager.prototype.getHeaders = function (config) {
        return __assign(__assign(__assign(__assign({}, this.getApiKeyHeader()), this.getAuthorizationHeader(config)), this.getUserAgentHeader()), this.getContentTypeHeader());
    };
    AxiosManager.prototype.getApiKeyHeader = function () {
        var _a;
        return _a = {}, _a[API_KEY_HEADER_NAME] = this.apiKey || '', _a;
    };
    AxiosManager.prototype.getAuthorizationHeader = function (config) {
        var _a;
        var token = this.tokenProvider.getToken(config.method, config.url, config.data);
        return _a = {}, _a[AUTHORIZATION_HEADER_NAME] = "Bearer ".concat(token), _a;
    };
    AxiosManager.prototype.getUserAgentHeader = function () {
        var _a;
        return _a = {}, _a[USER_AGENT_HEADER_NAME] = this.getUserAgent(), _a;
    };
    AxiosManager.prototype.getContentTypeHeader = function () {
        var _a;
        return _a = {}, _a[CONTENT_TYPE_HEADER_NAME] = "application/json", _a;
    };
    AxiosManager.prototype.getUserAgent = function () {
        var _a, _b, _c;
        var userAgent = "fireblocks/sdk/typescript/".concat(package_json_1.version);
        if (!((_a = this.additionalOptions) === null || _a === void 0 ? void 0 : _a.isAnonymousPlatform)) {
            userAgent += " (".concat(os_1.default.type(), " ").concat(os_1.default.release(), "; ").concat(platform_1.default.name, " ").concat(platform_1.default.version, "; ").concat(os_1.default.arch(), ")");
        }
        if ((_b = this.additionalOptions) === null || _b === void 0 ? void 0 : _b.userAgent) {
            userAgent = "".concat((_c = this.additionalOptions) === null || _c === void 0 ? void 0 : _c.userAgent, " ").concat(userAgent);
        }
        return userAgent;
    };
    Object.defineProperty(AxiosManager.prototype, "axios", {
        get: function () {
            return this.axiosInstance;
        },
        enumerable: false,
        configurable: true
    });
    return AxiosManager;
}());
exports.AxiosManager = AxiosManager;
//# sourceMappingURL=axiosManager.js.map