"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.CdmStandardsAdapter = void 0;
const Network_1 = require("../Utilities/Network");
const NetworkAdapter_1 = require("./NetworkAdapter");
/**
 * An adapter pre-configured to read the standard schema files published by CDM.
 */
class CdmStandardsAdapter extends NetworkAdapter_1.NetworkAdapter {
    /**
     * Constructs a CdmStandardsAdapter.
     * @param root The root path specifies either to read the standard files in logical or resolved form.
     */
    constructor(root = "/logical") {
        super();
        this.STANDARDS_ENDPOINT = "https://cdm-schema.microsoft.com";
        /**
         * @internal
         */
        this.type = "cdm-standards";
        this.root = root;
        this.httpClient = new Network_1.CdmHttpClient(this.STANDARDS_ENDPOINT);
    }
    /**
     * The combinating of the standards endpoint and the root path.
     */
    get absolutePath() {
        return this.STANDARDS_ENDPOINT + this.root;
    }
    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(`${this.root}${corpusPath}`, null, 'GET');
            const cdmHttpResponse = yield _super.executeRequest.call(this, cdmHttpRequest);
            return cdmHttpResponse.content;
        });
    }
    canWrite() {
        return false;
    }
    writeAsync(corpusPath, data) {
        return __awaiter(this, void 0, void 0, function* () {
            throw new Error('Write operation not supported.');
        });
    }
    createAdapterPath(corpusPath) {
        return `${this.absolutePath}${corpusPath}`;
    }
    createCorpusPath(adapterPath) {
        if (!adapterPath.startsWith(this.absolutePath)) {
            return undefined;
        }
        return adapterPath.slice(this.absolutePath.length);
        ;
    }
    computeLastModifiedTimeAsync(corpusPath) {
        return __awaiter(this, void 0, void 0, function* () {
            return new Date();
        });
    }
    fetchAllFilesAsync(currFullPath) {
        return __awaiter(this, void 0, void 0, function* () {
            return undefined;
        });
    }
    clearCache() { }
    fetchConfig() {
        const resultConfig = {
            type: this.type
        };
        // Construct network configs.
        const configObject = this.fetchNetworkConfig();
        if (this.locationHint) {
            configObject.locationHint = this.locationHint;
        }
        if (this.root) {
            configObject.root = this.root;
        }
        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);
        }
        if (configJson.root) {
            this.root = JSON.stringify(configJson.root);
        }
    }
}
exports.CdmStandardsAdapter = CdmStandardsAdapter;
//# sourceMappingURL=CdmStandardsAdapter.js.map