"use strict";
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
const HttpFile_1 = require("./HttpFile");
const HttpStorage_1 = require("./HttpStorage");
const StorageUtilities_1 = require("./StorageUtilities");
const FolderBase_1 = require("./FolderBase");
const Log_1 = require("../core/Log");
const axios_1 = require("axios");
class HttpFolder extends FolderBase_1.default {
    _name;
    folders;
    files;
    _storage;
    _parentFolder;
    _pendingLoadRequests = [];
    _isLoading = false;
    get storage() {
        return this._storage;
    }
    get parentFolder() {
        return this._parentFolder;
    }
    get name() {
        return this._name;
    }
    get fullPath() {
        if (this._parentFolder === null) {
            return this._storage.baseUrl;
        }
        return this._parentFolder.fullPath + this.name + HttpStorage_1.default.slashFolderDelimiter;
    }
    constructor(storage, parentFolder, folderName) {
        super();
        this._storage = storage;
        this._parentFolder = parentFolder;
        this._name = folderName;
        this.folders = {};
        this.files = {};
    }
    async exists() {
        return true;
    }
    async ensureExists() {
        return true;
    }
    ensureFile(name) {
        Log_1.default.assert(name.indexOf("/") < 0, "Unexpected to find / in file name: " + name);
        const nameCanon = StorageUtilities_1.default.canonicalizeName(name);
        let candFile = this.files[nameCanon];
        if (candFile === undefined) {
            candFile = new HttpFile_1.default(this, name);
            this.files[nameCanon] = candFile;
        }
        return candFile;
    }
    async moveTo(newStorageRelativePath) {
        throw new Error("Not implemented.");
    }
    _removeFile(file) {
        const nameCanon = StorageUtilities_1.default.canonicalizeName(file.name);
        const candFile = this.files[nameCanon];
        Log_1.default.assert(candFile === file, "Files don't match.");
        this.files[nameCanon] = undefined;
    }
    _addExistingFile(file) {
        const nameCanon = StorageUtilities_1.default.canonicalizeName(file.name);
        this.files[nameCanon] = file;
    }
    ensureFolder(name) {
        Log_1.default.assert(name.indexOf("/") < 0, "Unexpected to find / in folder name: " + name);
        const nameCanon = StorageUtilities_1.default.canonicalizeName(name);
        let candFolder = this.folders[nameCanon];
        if (!candFolder) {
            candFolder = new HttpFolder(this._storage, this, name);
            this.folders[nameCanon] = candFolder;
        }
        return candFolder;
    }
    async deleteFile(name) {
        throw new Error("Deletion of file not supported");
    }
    async createFile(name) {
        throw new Error("Deletion of file not supported");
    }
    async deleteThisFolder() {
        throw new Error("Deletion of this folder " + this.fullPath + " is not supported.");
    }
    async deleteAllFolderContents() {
        throw new Error("Deletion of all folder contents at " + this.fullPath + " is not supported.");
    }
    async load(force) {
        if (this.lastLoadedOrSaved != null && !force) {
            return 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 {
            let response = undefined;
            try {
                response = await axios_1.default.get(this.fullPath + "index.json");
            }
            catch (e) {
                Log_1.default.debug(e.message + " at " + this.fullPath + "index.json");
            }
            if (response) {
                const index = response.data;
                if (index.files !== null && index.files !== undefined) {
                    for (let i = 0; i < index.files.length; i++) {
                        const file = index.files[i];
                        if (StorageUtilities_1.default.isUsableFile(file)) {
                            this.ensureFile(file);
                        }
                    }
                }
                if (index.folders !== null && index.folders !== undefined) {
                    for (let i = 0; i < index.folders.length; i++) {
                        const folder = index.folders[i];
                        this.ensureFolder(folder);
                    }
                }
            }
            this.updateLastLoadedOrSaved();
            this._isLoading = false;
            const pendingLoad = this._pendingLoadRequests;
            this._pendingLoadRequests = [];
            for (const prom of pendingLoad) {
                prom(undefined);
            }
        }
        return this.lastLoadedOrSaved;
    }
}
exports.default = HttpFolder;

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