"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
// @ts-ignore
var isomorphic_fetch_1 = tslib_1.__importDefault(require("isomorphic-fetch"));
var util_1 = require("../util");
var lib_1 = require("./lib");
var lib_2 = require("./lib");
exports.isFileType = lib_2.isFileType;
exports.isAbsoluteUrl = lib_2.isAbsoluteUrl;
exports.joinPaths = lib_2.joinPaths;
exports.buildParameterString = lib_2.buildParameterString;
exports.createUrl = lib_2.createUrl;
var service_1 = require("./service");
exports.Service = service_1.Service;
var typedFetch = isomorphic_fetch_1.default;
exports.get = function (url, options) { return exports.request(url, tslib_1.__assign(tslib_1.__assign({}, options), { method: "GET" })); };
exports.getJSON = function (url, options) { return exports.request(url, tslib_1.__assign(tslib_1.__assign({}, options), { method: "GET", json: true })); };
exports.post = function (url, body, options) { return exports.request(url, tslib_1.__assign(tslib_1.__assign({}, options), { method: "POST", body: body })); };
exports.del = function (url, body, options) { return exports.request(url, tslib_1.__assign(tslib_1.__assign({}, options), { method: "DELETE", body: body })); };
exports.put = function (url, body, options) { return exports.request(url, tslib_1.__assign(tslib_1.__assign({}, options), { method: "PUT", body: body })); };
exports.patch = function (url, body, options) {
    return exports.request(url, tslib_1.__assign(tslib_1.__assign({}, options), { method: "PATCH", body: body }));
};
exports.request = function (url, options) {
    var _a = tslib_1.__read(lib_1.buildRequestConfig(url, options), 2), requestInfo = _a[0], requestInit = _a[1];
    var fetchResult = typedFetch(requestInfo, requestInit)
        .then(lib_1.handleFetchResult)
        .then(function (response) { return processSuccessResponse(response, options); });
    return fetchResult.catch(function (err) {
        if (options.retry !== undefined) {
            return startRetry(requestInfo, requestInit, options.retry).then(function (response) { return processSuccessResponse(response, options); });
        }
        return Promise.reject(err);
    });
};
var processSuccessResponse = function (response, options) {
    var json = options.json;
    if (json === "auto") {
        var headers = response.headers;
        var contentTypeHeader = headers.get("content-type");
        json =
            contentTypeHeader !== null &&
                contentTypeHeader.match(/application\/json/) !== null;
    }
    return json ? response.json() : response.text();
};
var startRetry = function (requestInfo, requestInit, _a) {
    var maxAttempts = _a.maxAttempts, attemptTimeout = _a.attemptTimeout;
    return tslib_1.__awaiter(void 0, void 0, void 0, function () {
        var index, res, err_1;
        return tslib_1.__generator(this, function (_b) {
            switch (_b.label) {
                case 0:
                    index = 0;
                    _b.label = 1;
                case 1:
                    if (!(index < maxAttempts)) return [3 /*break*/, 7];
                    _b.label = 2;
                case 2:
                    _b.trys.push([2, 4, , 6]);
                    return [4 /*yield*/, isomorphic_fetch_1.default(requestInfo, requestInit).then(lib_1.handleFetchResult)];
                case 3:
                    res = _b.sent();
                    return [2 /*return*/, res];
                case 4:
                    err_1 = _b.sent();
                    if (index === maxAttempts - 1) {
                        throw err_1;
                    }
                    return [4 /*yield*/, util_1.delay(attemptTimeout)];
                case 5:
                    _b.sent();
                    return [3 /*break*/, 6];
                case 6:
                    index++;
                    return [3 /*break*/, 1];
                case 7: return [2 /*return*/, undefined];
            }
        });
    });
};
var _fetch = function (url, options) {
    var _a = tslib_1.__read(lib_1.buildRequestConfig(url, options), 2), requestInfo = _a[0], requestInit = _a[1];
    var fetchResult = typedFetch(requestInfo, requestInit).then(lib_1.handleFetchResult);
    return fetchResult.catch(function (err) {
        if (options.retry !== undefined) {
            return startRetry(requestInfo, requestInit, options.retry);
        }
        return Promise.reject(err);
    });
};
exports.fetch = _fetch;
