"use strict";
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
const File_1 = require("./File");
const Storage_1 = require("./Storage");
const StorageUtilities_1 = require("./StorageUtilities");
const FolderBase_1 = require("./FolderBase");
const Log_1 = require("../core/Log");
class Folder extends FolderBase_1.default {
    _name;
    _path;
    folders;
    files;
    _storage;
    _parentFolder;
    get storage() {
        return this._storage;
    }
    get parentFolder() {
        return this._parentFolder;
    }
    get name() {
        return this._name;
    }
    get fullPath() {
        return this._path + Storage_1.default.folderDelimiter + this.name;
    }
    constructor(storage, parentFolder, parentPath, folderName) {
        super();
        this._storage = storage;
        this._parentFolder = parentFolder;
        this._path = parentPath;
        this._name = folderName;
        this.folders = {};
        this.files = {};
    }
    async exists() {
        return true;
    }
    async ensureExists() {
        return true;
    }
    ensureFile(name) {
        const nameCanon = StorageUtilities_1.default.canonicalizeName(name);
        let candFile = this.files[nameCanon];
        if (candFile === undefined) {
            candFile = new File_1.default(this, name);
            this.files[nameCanon] = candFile;
        }
        return candFile;
    }
    _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) {
        const nameCanon = StorageUtilities_1.default.canonicalizeName(name);
        let candFolder = this.folders[nameCanon];
        if (!candFolder) {
            candFolder = new Folder(this._storage, this, this.fullPath, name);
            this.folders[nameCanon] = candFolder;
        }
        return candFolder;
    }
    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.");
    }
    _addExistingFolderToParent(folder) {
        const nameCanon = StorageUtilities_1.default.canonicalizeName(folder.name);
        this.folders[nameCanon] = folder;
    }
    async moveTo(newStorageRelativePath) {
        const newFolderPath = StorageUtilities_1.default.getFolderPath(newStorageRelativePath);
        const newFolderName = StorageUtilities_1.default.getLeafName(newStorageRelativePath);
        if (newFolderName.length < 2) {
            throw new Error("New path is not correct.");
        }
        if (this._parentFolder !== null) {
            const newParentFolder = await this._parentFolder.storage.ensureFolderFromStorageRelativePath(newFolderPath);
            if (newParentFolder.folders[newFolderName] !== undefined) {
                throw new Error("Folder exists at specified path.");
            }
            this._parentFolder = newParentFolder;
            newParentFolder._addExistingFolderToParent(this);
        }
        this._name = newFolderName;
        return true;
    }
    async createFile(name) {
        return this.ensureFile(name);
    }
    async load(force) {
        if (this.lastLoadedOrSaved != null && !force) {
            return this.lastLoadedOrSaved;
        }
        this.updateLastLoadedOrSaved();
        return this.lastLoadedOrSaved;
    }
}
exports.default = Folder;

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