"use strict";
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
const Log_1 = require("../core/Log");
const FileBase_1 = require("./FileBase");
const Storage_1 = require("./Storage");
const StorageUtilities_1 = require("./StorageUtilities");
class File extends FileBase_1.default {
    _name;
    _parentFolder;
    get name() {
        return this._name;
    }
    get isContentLoaded() {
        return true;
    }
    get parentFolder() {
        return this._parentFolder;
    }
    get fullPath() {
        return this._parentFolder.fullPath + Storage_1.default.folderDelimiter + this.name;
    }
    constructor(parentFolder, folderName) {
        super();
        this._parentFolder = parentFolder;
        this._name = folderName;
    }
    async exists() {
        return true;
    }
    async loadContent(force) {
        this.lastLoadedOrSaved = new Date();
        return this.lastLoadedOrSaved;
    }
    async deleteThisFile(skipRemoveFromParent) {
        if (this.parentFolder.storage.readOnly) {
            throw new Error("Can't save read-only file.");
        }
        Log_1.default.verbose("Deleting file '" + this.storageRelativePath + "'");
        if (skipRemoveFromParent !== true) {
            this._parentFolder._removeFile(this);
        }
        return true;
    }
    async moveTo(newStorageRelativePath) {
        const newFolderPath = StorageUtilities_1.default.getFolderPath(newStorageRelativePath);
        const newFileName = StorageUtilities_1.default.getLeafName(newStorageRelativePath);
        if (newFileName.length < 2) {
            throw new Error("New path is not correct.");
        }
        const newParentFolder = await this._parentFolder.storage.ensureFolderFromStorageRelativePath(newFolderPath);
        if (newParentFolder.files[newFileName] !== undefined) {
            throw new Error("File exists at specified path.");
        }
        this._name = newFileName;
        this._parentFolder = newParentFolder;
        newParentFolder._addExistingFile(this);
        return true;
    }
    setContent(newContent) {
        if (this._content !== newContent) {
            if (!this.lastLoadedOrSaved) {
                this.lastLoadedOrSaved = new Date();
                this.lastLoadedOrSaved = new Date(this.lastLoadedOrSaved.getTime() - 1);
                Log_1.default.debugAlert("Setting a file without loading it first.");
            }
            this._content = newContent;
            this.contentWasModified();
        }
    }
    async saveContent() {
        if (this.parentFolder.storage.readOnly) {
            throw new Error("Can't save read-only file.");
        }
        this.lastLoadedOrSaved = new Date();
        return this.lastLoadedOrSaved;
    }
}
exports.default = File;

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