"use strict";
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
const GitHubFile_1 = require("./GitHubFile");
const GitHubStorage_1 = require("./GitHubStorage");
const StorageUtilities_1 = require("../storage/StorageUtilities");
const FolderBase_1 = require("../storage/FolderBase");
const Log_1 = require("../core/Log");
class GitHubFolder extends FolderBase_1.default {
    get storage() {
        return this._storage;
    }
    get parentFolder() {
        return this._parentFolder;
    }
    get name() {
        return this._name;
    }
    get fullPath() {
        let path = this._parentPath;
        if (!path.endsWith(GitHubStorage_1.default.slashFolderDelimiter)) {
            path += GitHubStorage_1.default.slashFolderDelimiter;
        }
        path += this.name;
        return path;
    }
    constructor(storage, parentFolder, parentPath, folderName) {
        super();
        this._storage = storage;
        this._parentFolder = parentFolder;
        this._parentPath = parentPath;
        this._name = folderName;
        this.folders = {};
        this.files = {};
    }
    async exists() {
        return true;
    }
    async ensureExists() {
        return true;
    }
    async moveTo(newStorageRelativePath) {
        throw new Error("Not implemented.");
    }
    async deleteThisFolder() {
        throw new Error("Deletion of this folder " + this.fullPath + " is not supported.");
    }
    ensureFile(name) {
        const nameCanon = StorageUtilities_1.default.canonicalizeName(name);
        let candFile = this.files[nameCanon];
        if (candFile == null) {
            candFile = new GitHubFile_1.default(this, name);
            this.files[nameCanon] = candFile;
        }
        return candFile;
    }
    ensureFolder(name) {
        const nameCanon = StorageUtilities_1.default.canonicalizeName(name);
        let candFolder = this.folders[nameCanon];
        if (!candFolder) {
            candFolder = new GitHubFolder(this._storage, this, this.fullPath, name);
            this.folders[nameCanon] = candFolder;
        }
        return candFolder;
    }
    async deleteFile(name) {
        throw new Error("Deletion of files not supported");
    }
    async createFile(name) {
        throw new Error("Creation of files not supported");
    }
    async load(force) {
        if (this.lastLoadedOrSaved != null && !force) {
            return this.lastLoadedOrSaved;
        }
        const repos = this.storage.manager.octokit.rest.repos;
        let pathMinusSlash = this.fullPath;
        if (pathMinusSlash === "/") {
            pathMinusSlash = "";
        }
        else if (!pathMinusSlash.startsWith("/")) {
            pathMinusSlash = "/" + pathMinusSlash;
        }
        if (pathMinusSlash.endsWith("/")) {
            pathMinusSlash = pathMinusSlash.substring(0, pathMinusSlash.length - 1);
        }
        // Log.debug("Loading GH folder from '" + pathMinusSlash + "'");
        const options = {
            owner: this.storage.ownerName,
            repo: this.storage.repoName,
            path: pathMinusSlash,
        };
        let contentListing = undefined;
        try {
            contentListing = await repos.getContent(options);
        }
        catch (e) {
            Log_1.default.debug("Error retrieving repo information: " + this.storage.ownerName + " " + this.storage.repoName + " " + e);
        }
        if (contentListing === undefined) {
            Log_1.default.debug("No data retrieved from repo: " + this.storage.ownerName + " " + this.storage.repoName);
            this.updateLastLoadedOrSaved();
            return this.lastLoadedOrSaved;
        }
        const response = contentListing;
        const fileObjects = response.data;
        // Log.debug("Retrieved '" + fileObjects.length + "' GitHub objects.");
        for (const i in fileObjects) {
            const fso = fileObjects[i];
            if (fso.type === "dir") {
                const childFolder = this.ensureFolder(fso.name);
                childFolder.sha = fso.sha;
            }
            else if (fso.type === "file") {
                if (StorageUtilities_1.default.isUsableFile(fso.name)) {
                    const childFile = this.ensureFile(fso.name);
                    childFile.sha = fso.sha;
                }
            }
        }
        this.updateLastLoadedOrSaved();
        return this.lastLoadedOrSaved;
    }
}
exports.default = GitHubFolder;

//# sourceMappingURL=../maps/github/GitHubFolder.js.map
