"use strict";
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
    if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
    if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
    return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
    if (kind === "m") throw new TypeError("Private method is not writable");
    if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
    if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
    return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var _FileBase_fileContainerStorage;
Object.defineProperty(exports, "__esModule", { value: true });
const StorageUtilities_1 = require("./StorageUtilities");
const js_md5_1 = require("js-md5");
const Log_1 = require("../core/Log");
class FileBase {
    get fileContainerStorage() {
        return __classPrivateFieldGet(this, _FileBase_fileContainerStorage, "f");
    }
    set fileContainerStorage(newStorage) {
        __classPrivateFieldSet(this, _FileBase_fileContainerStorage, newStorage, "f");
    }
    get isString() {
        return this._content !== null && typeof this._content === "string";
    }
    get isBinary() {
        if (this._content === undefined) {
            return false;
        }
        if (this._content instanceof Uint8Array) {
            return true;
        }
        return false;
    }
    get content() {
        return this._content;
    }
    get latestModified() {
        if (this.modified == null) {
            return this.modifiedAtLoad;
        }
        if (this.modifiedAtLoad == null) {
            return null;
        }
        if (this.modified.getTime() > this.modifiedAtLoad.getTime()) {
            return this.modified;
        }
        return this.modifiedAtLoad;
    }
    get type() {
        return StorageUtilities_1.default.getTypeFromName(this.name);
    }
    get needsSave() {
        if (this.isDisposed) {
            Log_1.default.throwIsDisposed();
        }
        return this.modified != null && (this.lastLoadedOrSaved == null || this.modified >= this.lastLoadedOrSaved);
    }
    get fullPath() {
        return this.storageRelativePath;
    }
    get extendedPath() {
        let start = "";
        if (this.parentFolder.storage.storagePath) {
            start = this.parentFolder.storage.storagePath;
        }
        return start + this.fullPath;
    }
    get storageRelativePath() {
        return this.parentFolder.storageRelativePath + this.name;
    }
    getRootRelativePath() {
        if (this.isDisposed) {
            Log_1.default.throwIsDisposed();
        }
        return this.getFolderRelativePath(this.parentFolder.storage.rootFolder);
    }
    getFolderRelativePath(toFolder) {
        if (this.isDisposed) {
            Log_1.default.throwIsDisposed();
        }
        let result = this.parentFolder.getFolderRelativePath(toFolder);
        if (result === undefined) {
            return undefined;
        }
        return result + this.name;
    }
    get coreContentLength() {
        if (!this.content) {
            return 0;
        }
        if (this.isBinary || typeof this.content !== "string") {
            return this.content.length;
        }
        return this.content.replace(/\s/g, "").length;
    }
    constructor() {
        this.isDisposed = false;
        _FileBase_fileContainerStorage.set(this, null);
        this.modified = null;
        this.modifiedAtLoad = null;
        this.lastLoadedOrSaved = null;
        this._content = null;
    }
    contentWasModified() {
        if (this.isDisposed) {
            Log_1.default.throwIsDisposed();
        }
        this.modified = new Date();
        if (this.parentFolder.storage) {
            this.parentFolder.storage.notifyFileContentsUpdated(this);
        }
    }
    async getHash() {
        await this.loadContent(false);
        if (this._content === undefined || this._content === null) {
            return undefined;
        }
        return js_md5_1.default.base64(this._content);
    }
    dispose() {
        this.manager = undefined;
        this._content = null;
        this.isDisposed = true;
    }
    async exists() {
        if (this.isDisposed) {
            Log_1.default.throwIsDisposed();
        }
        await this.loadContent(false);
        return this._content !== null;
    }
    getRelativePathFor(file) {
        if (file.parentFolder.storage !== this.parentFolder.storage) {
            return undefined;
        }
        const foldersByPath = {};
        let targetParentFolder = file.parentFolder;
        while (targetParentFolder) {
            foldersByPath[targetParentFolder.storageRelativePath] = targetParentFolder;
            targetParentFolder = targetParentFolder.parentFolder;
        }
        let myParentFolder = this.parentFolder;
        let relativePath = "." + myParentFolder.storage.folderDelimiter;
        while (myParentFolder && foldersByPath[myParentFolder.storageRelativePath] === undefined) {
            relativePath += ".." + myParentFolder.storage.folderDelimiter;
            myParentFolder = myParentFolder.parentFolder;
        }
        if (!myParentFolder) {
            return undefined;
        }
        const folderRelativePath = file.getFolderRelativePath(myParentFolder);
        if (!folderRelativePath) {
            return undefined;
        }
        return relativePath + StorageUtilities_1.default.ensureNotStartsWithDelimiter(folderRelativePath);
    }
}
exports.default = FileBase;
_FileBase_fileContainerStorage = new WeakMap();

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