"use strict";
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
const FileBase_1 = require("./FileBase");
const StorageUtilities_1 = require("./StorageUtilities");
const axios_1 = require("axios");
const Log_1 = require("../core/Log");
class HttpFile extends FileBase_1.default {
    _name;
    _parentFolder;
    _pendingLoadRequests = [];
    _isLoading = false;
    get name() {
        return this._name;
    }
    get isContentLoaded() {
        return true;
    }
    get parentFolder() {
        return this._parentFolder;
    }
    get fullPath() {
        return this._parentFolder.fullPath + this.name;
    }
    constructor(parentFolder, folderName) {
        super();
        this._parentFolder = parentFolder;
        this._name = folderName;
    }
    async exists() {
        await this.loadContent(false);
        return this._content !== null;
    }
    async loadContent(force) {
        //        Log.assert(this.fullPath.startsWith("/"), "Expecting a full absolute path");
        if (force || !this.lastLoadedOrSaved) {
            if (this._isLoading) {
                const pendingLoad = this._pendingLoadRequests;
                const prom = (resolve, reject) => {
                    pendingLoad.push(resolve);
                };
                await new Promise(prom);
                if (this.lastLoadedOrSaved === null) {
                    throw new Error();
                }
                return this.lastLoadedOrSaved;
            }
            else {
                this._content = null;
                const path = this.fullPath;
                if (StorageUtilities_1.default.getEncodingByFileName(this.name) === StorageUtilities_1.EncodingType.ByteBuffer) {
                    try {
                        const response = await axios_1.default.get(path, {
                            responseType: "arraybuffer",
                            headers: {},
                        });
                        this._content = new Uint8Array(response.data);
                    }
                    catch (e) { }
                }
                else {
                    let result = null;
                    try {
                        const response = await axios_1.default.get(path, {
                            headers: {},
                        });
                        result = response.data;
                        if (typeof result === "object") {
                            try {
                                result = JSON.stringify(result, undefined, 2);
                            }
                            catch (e) {
                                Log_1.default.fail("Could not convert file to JSON");
                            }
                        }
                        if (response.status !== 200) {
                            Log_1.default.verbose("Could not retrieve file from '" + path + "' - response code is " + response.status);
                        }
                        if (result === null || result === "null") {
                            Log_1.default.verbose("Could not retrieve file from '" + path + "' - result is null.");
                        }
                    }
                    catch (e) {
                        Log_1.default.verbose("Could not retrieve file from '" + path + "' - " + e + " - " + e?.stack);
                    }
                    this._content = result;
                }
                this.lastLoadedOrSaved = new Date();
                this._isLoading = false;
                const pendingLoad = this._pendingLoadRequests;
                this._pendingLoadRequests = [];
                for (const prom of pendingLoad) {
                    prom(undefined);
                }
            }
        }
        return this.lastLoadedOrSaved;
    }
    async deleteThisFile(skipRemoveFromParent) {
        throw new Error("HttpFile is read-only.");
    }
    async moveTo(newStorageRelativePath) {
        throw new Error("HttpFile is read-only.");
    }
    setContent(newContent) {
        throw new Error("HttpFile is read-only.");
    }
    async saveContent() {
        throw new Error("HttpFile is read-only.");
    }
}
exports.default = HttpFile;

//# sourceMappingURL=../maps/storage/HttpFile.js.map
