"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const ste_events_1 = require("ste-events");
const ZipStorage_1 = require("../storage/ZipStorage");
const IProjectItemData_1 = require("./IProjectItemData");
const IProjectItemVariant_1 = require("./IProjectItemVariant");
const Log_1 = require("../core/Log");
const MCWorld_1 = require("../minecraft/MCWorld");
const IStorage_1 = require("../storage/IStorage");
const Pack_1 = require("../minecraft/Pack");
class ProjectItemVariant {
    _item;
    _data;
    _file = null;
    _folder = null;
    _projectVariant;
    _isFileContentProcessed = false;
    _isLoaded = false;
    _onFileRetrieved = new ste_events_1.EventDispatcher();
    _onFolderRetrieved = new ste_events_1.EventDispatcher();
    _onLoaded = new ste_events_1.EventDispatcher();
    _isDefault;
    constructor(parentItem, data, projectVariant) {
        this._item = parentItem;
        this._data = data;
        this._projectVariant = projectVariant;
        this._isDefault = this._data.label === "";
    }
    get projectVariant() {
        return this._projectVariant;
    }
    get errorMessage() {
        return this._data.errorMessage;
    }
    set errorMessage(newErrorMessage) {
        this._data.errorMessage = newErrorMessage;
    }
    get isLoaded() {
        return this._isLoaded;
    }
    get onLoaded() {
        return this._onLoaded.asEvent();
    }
    get onFileRetrieved() {
        return this._onFileRetrieved.asEvent();
    }
    get onFolderRetrieved() {
        return this._onFolderRetrieved.asEvent();
    }
    get label() {
        return this._data.label;
    }
    get variantType() {
        return this._data.variantType;
    }
    get file() {
        if (this._isDefault) {
            return this._item.primaryFile;
        }
        return this._file;
    }
    setFile(file) {
        this._file = file;
    }
    get itemType() {
        return this._item.itemType;
    }
    get projectPath() {
        return this._data.projectPath;
    }
    set variantType(newVariantType) {
        this._data.variantType = newVariantType;
    }
    set projectPath(newProjectPath) {
        this._data.projectPath = newProjectPath;
    }
    get folder() {
        if (this._isDefault) {
            return this._item.defaultFolder;
        }
        return this._folder;
    }
    setFolder(file) {
        this._folder = file;
    }
    _handleMCWorldLoaded(world, worldA) {
        this._fireLoadedEvent();
    }
    _fireLoadedEvent() {
        if (this._onLoaded && this.isLoaded) {
            this._onLoaded.dispatch(this, this);
        }
    }
    get errorStatus() {
        return this._data.errorStatus;
    }
    set errorStatus(errorStatus) {
        this._data.errorStatus = errorStatus;
    }
    async ensureFileStorage() {
        if (this._item.storageType === IProjectItemData_1.ProjectItemStorageType.singleFile &&
            this._file === null &&
            this.projectPath !== null &&
            this.projectPath !== undefined &&
            this.projectPath.startsWith("/") &&
            this._item.project.projectFolder !== null &&
            this._item.project.projectFolder !== undefined) {
            const prefixPaths = this.projectPath.split("#");
            if (prefixPaths.length > 1) {
                let folderToLoadFrom = this._item.project.projectFolder;
                for (let i = 0; i < prefixPaths.length - 1; i++) {
                    if (folderToLoadFrom) {
                        const zipFile = await folderToLoadFrom.ensureFileFromRelativePath(prefixPaths[i]);
                        await zipFile.loadContent();
                        if (zipFile.content && zipFile.content instanceof Uint8Array) {
                            if (!zipFile.fileContainerStorage) {
                                const zipStorage = new ZipStorage_1.default();
                                zipStorage.storagePath = zipFile.storageRelativePath + "#";
                                await zipStorage.loadFromUint8Array(zipFile.content, zipFile.name);
                                zipFile.fileContainerStorage = zipStorage;
                            }
                            folderToLoadFrom = zipFile.fileContainerStorage.rootFolder;
                        }
                        else {
                            folderToLoadFrom = undefined;
                        }
                    }
                }
                if (folderToLoadFrom) {
                    this._file = await folderToLoadFrom.ensureFileFromRelativePath(prefixPaths[prefixPaths.length - 1]);
                    this._isFileContentProcessed = false;
                }
                else {
                    Log_1.default.debugAlert("Unable to parse a containerized file path of '" + this.projectPath + "'");
                    return null;
                }
            }
            else {
                this._file = await this._item.project.projectFolder.ensureFileFromRelativePath(this.projectPath);
                this._isFileContentProcessed = false;
            }
        }
        else if (this._item.storageType === IProjectItemData_1.ProjectItemStorageType.singleFile &&
            this._file === null &&
            (this.projectPath === null || this.projectPath === undefined) &&
            (!this.variantType || this.variantType === IProjectItemVariant_1.ProjectItemVariantType.subPack) &&
            this._item.project.projectFolder !== null &&
            this._item.project.projectFolder !== undefined) {
            await this._item.project.ensurePacksAsync();
            const pack = await this._item.getPack();
            if (pack && pack.type === Pack_1.PackType.resource && this._item.projectPath) {
                const manifest = (await pack.ensureManifest());
                const folder = pack.folder;
                if (manifest && folder) {
                    const subpack = manifest.getSubpackByFolderName(this.label);
                    if (subpack) {
                        const path = this._item.projectPath;
                        const resourcePacksRel = path.indexOf("/resource_packs/");
                        if (resourcePacksRel > -1) {
                            const file = await folder.getFileFromRelativePath(path.substring(resourcePacksRel + 15));
                            if (file) {
                                this._file = file;
                            }
                        }
                    }
                }
            }
        }
        if (!this._isFileContentProcessed && this._file) {
            /*if (this._data.creationType === ProjectItemCreationType.generated) {
              await ProjectAutogeneration.updateItemAutogeneration(this, true);
            } else {*/
            await this._file.loadContent();
            // }
            //await ProjectAutogeneration.updateItemAutogeneratedSideFiles(this);
            this._isFileContentProcessed = true;
            this._onFileRetrieved.dispatch(this, this._file);
            if (this.itemType === IProjectItemData_1.ProjectItemType.MCWorld || this.itemType === IProjectItemData_1.ProjectItemType.MCTemplate) {
                const mcworld = await MCWorld_1.default.ensureOnFile(this._file, this._item.project, this._handleMCWorldLoaded);
                if (mcworld) {
                    this.errorMessage = mcworld.storageErrorMessage;
                    if (mcworld.storageErrorStatus === IStorage_1.StorageErrorStatus.unprocessable) {
                        this.errorStatus = IProjectItemData_1.ProjectItemErrorStatus.unprocessable;
                    }
                    else {
                        this.errorStatus = IProjectItemData_1.ProjectItemErrorStatus.none;
                    }
                }
            }
            else {
                this._fireLoadedEvent();
            }
        }
        return this._file;
    }
    async ensureFolderStorage() {
        if (this._item.storageType === IProjectItemData_1.ProjectItemStorageType.folder) {
            if (this._folder === null &&
                this.projectPath !== null &&
                this.projectPath !== undefined &&
                this.projectPath.startsWith("/") &&
                this._item.project.projectFolder !== null &&
                this._item.project.projectFolder !== undefined) {
                const prefixPaths = this.projectPath.split("#");
                if (prefixPaths.length > 1) {
                    let folderToLoadFrom = this._item.project.projectFolder;
                    for (let i = 0; i < prefixPaths.length - 1; i++) {
                        if (folderToLoadFrom) {
                            const zipFile = await folderToLoadFrom.ensureFileFromRelativePath(prefixPaths[i]);
                            await zipFile.loadContent();
                            if (zipFile.content && zipFile.content instanceof Uint8Array) {
                                if (!zipFile.fileContainerStorage) {
                                    const zipStorage = new ZipStorage_1.default();
                                    zipStorage.storagePath = zipFile.storageRelativePath + "#";
                                    await zipStorage.loadFromUint8Array(zipFile.content, zipFile.name);
                                    zipFile.fileContainerStorage = zipStorage;
                                }
                                folderToLoadFrom = zipFile.fileContainerStorage.rootFolder;
                            }
                            else {
                                folderToLoadFrom = undefined;
                            }
                        }
                    }
                    if (folderToLoadFrom) {
                        this._folder = await folderToLoadFrom.ensureFolderFromRelativePath(prefixPaths[prefixPaths.length - 1]);
                    }
                    else {
                        // Log.debugAlert("Unable to parse a containerized file path of '" + this.storagePath + "'");
                        return null;
                    }
                }
                else {
                    this._folder = await this._item.project.projectFolder.ensureFolderFromRelativePath(this.projectPath);
                }
                await this._folder.load();
                this._onFolderRetrieved.dispatch(this, this._folder);
                if (this.itemType === IProjectItemData_1.ProjectItemType.worldFolder) {
                    const mcworld = await MCWorld_1.default.ensureMCWorldOnFolder(this._folder, this._item.project, this._handleMCWorldLoaded);
                    if (mcworld) {
                        this.errorMessage = mcworld.storageErrorMessage;
                        if (mcworld.storageErrorStatus === IStorage_1.StorageErrorStatus.unprocessable) {
                            this.errorStatus = IProjectItemData_1.ProjectItemErrorStatus.unprocessable;
                        }
                        else {
                            this.errorStatus = IProjectItemData_1.ProjectItemErrorStatus.none;
                        }
                    }
                }
                else {
                    this._fireLoadedEvent();
                }
            }
            return this._folder;
        }
        return undefined;
    }
}
exports.default = ProjectItemVariant;

//# sourceMappingURL=../maps/app/ProjectItemVariant.js.map
