"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 __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.GithubAdapter = void 0;
const Network_1 = require("../Utilities/Network");
const NetworkAdapter_1 = require("./NetworkAdapter");
/**
 * @deprecated Please use the CdmStandardsAdapter instead.
 */
class GithubAdapter extends NetworkAdapter_1.NetworkAdapter {
    constructor() {
        super();
        /**
         * @internal
         */
        this.type = 'github';
        this.httpClient = new Network_1.CdmHttpClient(`https://${GithubAdapter.ghHost}`);
    }
    static ghRawRoot() {
        return `https://${this.ghHost}${this.ghPath}`;
    }
    canRead() {
        return true;
    }
    readAsync(corpusPath) {
        const _super = Object.create(null, {
            executeRequest: { get: () => super.executeRequest }
        });
        return __awaiter(this, void 0, void 0, function* () {
            const cdmHttpRequest = this.setUpCdmRequest(`${GithubAdapter.ghPath}${corpusPath}`, new Map(), 'GET');
            const cdmHttpResponse = yield _super.executeRequest.call(this, cdmHttpRequest);
            return cdmHttpResponse.content;
        });
    }
    createAdapterPath(corpusPath) {
        return `${GithubAdapter.ghRawRoot()}${corpusPath}`;
    }
    createCorpusPath(adapterPath) {
        const ghRoot = GithubAdapter.ghRawRoot();
        // might not be an adapterPath that we understand. check that first
        if (ghRoot && adapterPath.startsWith(ghRoot)) {
            return adapterPath.slice(ghRoot.length);
        }
        return undefined;
    }
    fetchConfig() {
        const resultConfig = {
            type: this.type
        };
        // Construct network configs.
        const configObject = this.fetchNetworkConfig();
        if (this.locationHint) {
            configObject.locationHint = this.locationHint;
        }
        resultConfig.config = configObject;
        return JSON.stringify(resultConfig);
    }
    updateConfig(config) {
        if (!config) {
            // It is fine just to skip it for GitHub adapter.
            return;
        }
        this.updateNetworkConfig(config);
        const configJson = JSON.parse(config);
        if (configJson.locationHint) {
            this.locationHint = JSON.stringify(configJson.locationHint);
        }
    }
}
exports.GithubAdapter = GithubAdapter;
GithubAdapter.ghHost = 'raw.githubusercontent.com';
GithubAdapter.ghPath = '/Microsoft/CDM/master/schemaDocuments';
//# sourceMappingURL=GithubAdapter.js.map