"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var _1 = require(".");
var lib_1 = require("./lib");
var Service = /** @class */ (function () {
    function Service(options) {
        if (options === void 0) { options = {}; }
        this.defaults = options.defaults || {};
        this.baseUrl = options.baseUrl;
        this.middlewares =
            options.middlewares === undefined ? [] : options.middlewares;
        this.get = this.get.bind(this);
        this.getJSON = this.getJSON.bind(this);
        this.post = this.post.bind(this);
        this.del = this.del.bind(this);
        this.put = this.put.bind(this);
        this.request = this.request.bind(this);
    }
    Service.prototype.get = function (url, options) {
        return this.request(url, tslib_1.__assign(tslib_1.__assign({}, options), { method: "GET" }));
    };
    Service.prototype.getJSON = function (url, options) {
        return this.request(url, tslib_1.__assign(tslib_1.__assign({}, options), { method: "GET", json: true }));
    };
    Service.prototype.post = function (url, body, options) {
        return this.request(url, tslib_1.__assign(tslib_1.__assign({}, options), { method: "POST", body: body }));
    };
    Service.prototype.del = function (url, body, options) {
        return this.request(url, tslib_1.__assign(tslib_1.__assign({}, options), { method: "DELETE", body: body }));
    };
    Service.prototype.put = function (url, body, options) {
        return this.request(url, tslib_1.__assign(tslib_1.__assign({}, options), { method: "PUT", body: body }));
    };
    Service.prototype.patch = function (url, body, options) {
        return this.request(url, tslib_1.__assign(tslib_1.__assign({}, options), { method: "PATCH", body: body }));
    };
    Service.prototype.request = function (url, options) {
        var _this = this;
        var config = lib_1.joinConfigs([
            tslib_1.__assign({ baseUrl: this.baseUrl }, this.defaults),
            options,
        ]);
        var getMiddleware = function (num) {
            var func = _this.middlewares[num];
            return func === undefined ? _1.request : func;
        };
        var index = 0;
        var next = function (nextUrl, nextOptions) {
            index++;
            var middleware = getMiddleware(index);
            return middleware(nextUrl, nextOptions, next);
        };
        var result = getMiddleware(0)(url, config, next);
        return result;
    };
    return Service;
}());
exports.Service = Service;
