import { __assign, __awaiter, __generator, __read } from "tslib";
// @ts-ignore
import fetch from "isomorphic-fetch";
import { delay } from "../util";
import { buildRequestConfig, handleFetchResult } from "./lib";
export { isFileType, isAbsoluteUrl, joinPaths, buildParameterString, createUrl, } from "./lib";
export { Service } from "./service";
var typedFetch = fetch;
export var get = function (url, options) { return request(url, __assign(__assign({}, options), { method: "GET" })); };
export var getJSON = function (url, options) { return request(url, __assign(__assign({}, options), { method: "GET", json: true })); };
export var post = function (url, body, options) { return request(url, __assign(__assign({}, options), { method: "POST", body: body })); };
export var del = function (url, body, options) { return request(url, __assign(__assign({}, options), { method: "DELETE", body: body })); };
export var put = function (url, body, options) { return request(url, __assign(__assign({}, options), { method: "PUT", body: body })); };
export var patch = function (url, body, options) {
    return request(url, __assign(__assign({}, options), { method: "PATCH", body: body }));
};
export var request = function (url, options) {
    var _a = __read(buildRequestConfig(url, options), 2), requestInfo = _a[0], requestInit = _a[1];
    var fetchResult = typedFetch(requestInfo, requestInit)
        .then(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 __awaiter(void 0, void 0, void 0, function () {
        var index, res, err_1;
        return __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*/, fetch(requestInfo, requestInit).then(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*/, 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 = __read(buildRequestConfig(url, options), 2), requestInfo = _a[0], requestInit = _a[1];
    var fetchResult = typedFetch(requestInfo, requestInit).then(handleFetchResult);
    return fetchResult.catch(function (err) {
        if (options.retry !== undefined) {
            return startRetry(requestInfo, requestInit, options.retry);
        }
        return Promise.reject(err);
    });
};
export { _fetch as fetch };
