"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var sp_core_library_1 = require("@microsoft/sp-core-library");
var FetchProvider_1 = require("./FetchProvider");
var HttpClientConfiguration_1 = require("./HttpClientConfiguration");
var HttpClientResponse_1 = tslib_1.__importDefault(require("./HttpClientResponse"));
var HttpClientHelper_1 = tslib_1.__importDefault(require("./HttpClientHelper"));
var CacheManager_1 = require("../caching/CacheManager");
var CacheDataProviderService_1 = require("../caching/CacheDataProviderService");
/**
 * HttpClient implements a basic set of features for performing REST operations against
 * a generic service.
 *
 * @remarks
 * For communicating with SharePoint, use the {@link SPHttpClient} class instead.
 *
 * @public
 */
var HttpClient = /** @class */ (function () {
    function HttpClient(serviceScope) {
        var _this = this;
        this._serviceScope = serviceScope;
        serviceScope.whenFinished(function () {
            _this._fetchProvider = serviceScope.consume(FetchProvider_1.fetchProviderServiceKey);
        });
    }
    HttpClient.prototype.fetch = function (url, configuration, options, cacheConfiguration) {
        var _this = this;
        // Default fetch behavior - No caching
        // If no caching options are provided, or If Cache Provider is not present continue to API call
        if (!cacheConfiguration || !this._cacheDataProvider) {
            return this._fetch(url, configuration, options);
        }
        // Get the cache Key
        // tslint:disable-next-line:no-any
        var cacheKey = this._cacheDataProvider.getCacheKey(url, configuration, options);
        // Fetch data and Cache according to the cacheConfiguration
        return CacheManager_1.CacheManager.fetchAndCache(cacheKey, this._cacheDataProvider, cacheConfiguration, function () { return _this._fetch(url, configuration, options); });
    };
    HttpClient.prototype.get = function (url, configuration, options, cacheConfiguration) {
        if (cacheConfiguration) {
            return this.fetch(url, configuration, HttpClientHelper_1.default.overrideHttpMethod(options, 'GET'), cacheConfiguration);
        }
        else {
            return this.fetch(url, configuration, HttpClientHelper_1.default.overrideHttpMethod(options, 'GET'));
        }
    };
    /**
     * Calls fetch(), but sets the method to "POST".
     *
     * @param url - the URL to fetch
     * @param configuration - determines the default behavior of HttpClient; normally this should
     *   be the latest version number from HttpClientConfigurations
     * @param options - additional options that affect the request
     * @returns A promise with behavior similar to WHATWG fetch().  This promise will resolve normally
     * (with {@link HttpClientResponse.ok} being false) for error status codes such as HTTP 404
     * or 500.  The promise will only reject for network failures or other errors that prevent communication
     * with the server.
     */
    HttpClient.prototype.post = function (url, configuration, options) {
        return this._fetch(url, configuration, HttpClientHelper_1.default.overrideHttpMethod(options, 'POST'));
    };
    HttpClient.prototype._fetch = function (url, configuration, options) {
        return HttpClientHelper_1.default.fetchCore(configuration, new Request(url, options), this._serviceScope, this._fetchProvider, HttpClient._className).then(function (response) {
            return new HttpClientResponse_1.default(response);
        });
    };
    Object.defineProperty(HttpClient.prototype, "_cacheDataProvider", {
        /**
         * Gets the cache data provider
         */
        // tslint:disable-next-line:no-any
        get: function () {
            if (!this._cacheProvider) {
                this._cacheProvider = this._serviceScope.consume(CacheDataProviderService_1.getCacheDataProviderServiceKey())._provider;
            }
            return this._cacheProvider;
        },
        enumerable: false,
        configurable: true
    });
    /**
     * The standard predefined HttpClientConfiguration objects for use with
     * the HttpClient class.
     */
    HttpClient.configurations = HttpClientConfiguration_1.predefinedConfigurations;
    /**
     * The service key for HttpClient.
     *
     * @public
     */
    HttpClient.serviceKey = sp_core_library_1.ServiceKey.create('sp-http:HttpClient', HttpClient);
    HttpClient._className = 'HttpClient';
    return HttpClient;
}());
exports.default = HttpClient;
//# sourceMappingURL=HttpClient.js.map