"use strict";
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
const FileBase_1 = require("./FileBase");
const StorageUtilities_1 = require("./StorageUtilities");
const Log_1 = require("../core/Log");
class ZipFile extends FileBase_1.default {
    _name;
    _parentFolder;
    _jszipo;
    get name() {
        return this._name;
    }
    get fullPath() {
        return StorageUtilities_1.default.ensureEndsWithDelimiter(this._parentFolder.fullPath) + this.name;
    }
    get parentFolder() {
        return this._parentFolder;
    }
    get isContentLoaded() {
        return this.lastLoadedOrSaved != null || this.modified != null;
    }
    updateZipNativeFile(thisFileObject) {
        this._jszipo = thisFileObject;
    }
    constructor(parentFolder, fileName, thisFileObject) {
        super();
        this._jszipo = thisFileObject;
        this._parentFolder = parentFolder;
        this._name = fileName;
        this.modified = null;
        this.lastLoadedOrSaved = null;
    }
    async deleteThisFile(skipRemoveFromParent) {
        throw new Error("Not implemented.");
    }
    async moveTo(newStorageRelativePath) {
        throw new Error("Not implemented.");
    }
    async loadContent(force, forceEncoding) {
        if (force || !this.lastLoadedOrSaved) {
            if (this._jszipo !== null) {
                const type = forceEncoding ?? StorageUtilities_1.default.getEncodingByFileName(this.name);
                if (type === StorageUtilities_1.EncodingType.ByteBuffer) {
                    try {
                        this._content = await this._jszipo.async("uint8array");
                    }
                    catch (e) {
                        this.errorStateMessage = e.toString();
                    } /*, (metadata) => {
                      Log.verbose("Extracting " + this.storageRelativePath + " (" + metadata.percent.toFixed(2) + ")%");
                    });*/
                    Log_1.default.assert(this._content !== null, "Unexpectedly could not load content.");
                }
                else {
                    try {
                        this._content = await this._jszipo.async("string");
                    }
                    catch (e) {
                        this.errorStateMessage = e.toString();
                    }
                }
            }
            this.lastLoadedOrSaved = new Date();
        }
        return this.lastLoadedOrSaved;
    }
    setContent(newContent) {
        const areEqual = StorageUtilities_1.default.contentsAreEqual(this._content, newContent);
        if (!areEqual) {
            if (!this.lastLoadedOrSaved) {
                this.lastLoadedOrSaved = new Date();
                this.lastLoadedOrSaved = new Date(this.lastLoadedOrSaved.getTime() - 1);
                // Log.debugAlert("Setting a file without loading it first.");
            }
            this._content = newContent;
            this.contentWasModified();
            this._parentFolder.storage.modified = this.modified;
        }
    }
    async saveContent(force) {
        if (this.parentFolder.storage.readOnly) {
            throw new Error("Can't save read-only file.");
        }
        if (this.needsSave || force === true) {
            this.lastLoadedOrSaved = new Date();
            if (this.content !== null) {
                // Log.debug("Saving '" + this.content.length + "' content to '" + this.name + "' within zip");
                this._parentFolder.storage.modified = this.modified;
                this._parentFolder.zip.file(this.name, this.content);
            }
        }
        if (this.lastLoadedOrSaved === null) {
            this.lastLoadedOrSaved = new Date();
        }
        return this.lastLoadedOrSaved;
    }
}
exports.default = ZipFile;

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