"use strict";
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __exportStar = (this && this.__exportStar) || function(m, exports) {
    for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Fetcher = exports.CompletionsFetcher = exports.ICompletionsFetcherService = void 0;
exports.isInterruptedNetworkError = isInterruptedNetworkError;
// Re-export everything from networking types module for backward compatibility
__exportStar(require("./networkingTypes"), exports);
// Import what we need locally for this module's implementation
const configurationService_1 = require("../../../../../platform/configuration/common/configurationService");
const fetcherService_1 = require("../../../../../platform/networking/common/fetcherService");
const nullExperimentationService_1 = require("../../../../../platform/telemetry/common/nullExperimentationService");
const services_1 = require("../../../../../util/common/services");
exports.ICompletionsFetcherService = (0, services_1.createServiceIdentifier)('ICompletionsFetcherService');
let CompletionsFetcher = class CompletionsFetcher {
    constructor(configurationService, fetcherService, experimentationService) {
        this.configurationService = configurationService;
        this.fetcherService = fetcherService;
        this.experimentationService = experimentationService;
    }
    getImplementation() {
        return this;
    }
    fetch(url, options) {
        const useFetcher = this.configurationService.getExperimentBasedConfig(configurationService_1.ConfigKey.CompletionsFetcher, this.experimentationService) || undefined;
        const baseOptions = useFetcher ? { ...options, useFetcher } : options;
        return this.fetcherService.fetch(url, {
            ...baseOptions,
            callSite: baseOptions.callSite ?? 'completions-core',
        });
    }
    disconnectAll() {
        return this.fetcherService.disconnectAll();
    }
};
exports.CompletionsFetcher = CompletionsFetcher;
exports.CompletionsFetcher = CompletionsFetcher = __decorate([
    __param(0, configurationService_1.IConfigurationService),
    __param(1, fetcherService_1.IFetcherService),
    __param(2, nullExperimentationService_1.IExperimentationService)
], CompletionsFetcher);
/**
 * Encapsulates all the functionality related to making GET/POST/DELETE requests using
 * different libraries (and in the future, different environments like web vs
 * node).
 */
class Fetcher {
    /**
     * Returns the real implementation, not a delegator.  Used by diagnostics to ensure the fetcher name and all
     * reachability checks are aligned.
     */
    getImplementation() {
        return this;
    }
}
exports.Fetcher = Fetcher;
function isInterruptedNetworkError(error) {
    if (!(error instanceof Error)) {
        return false;
    }
    if (error.message === 'ERR_HTTP2_GOAWAY_SESSION') {
        return true;
    }
    if (!('code' in error)) {
        return false;
    }
    return error.code === 'ECONNRESET' || error.code === 'ETIMEDOUT' || error.code === 'ERR_HTTP2_INVALID_SESSION';
}
//# sourceMappingURL=networking.js.map