"use strict";
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
exports.PackType = void 0;
const BehaviorManifestDefinition_1 = require("./BehaviorManifestDefinition");
const ResourceManifestDefinition_1 = require("./ResourceManifestDefinition");
const SkinManifestDefinition_1 = require("./SkinManifestDefinition");
const PersonaManifestDefinition_1 = require("./PersonaManifestDefinition");
const Log_1 = require("../core/Log");
var PackType;
(function (PackType) {
    PackType[PackType["resource"] = 0] = "resource";
    PackType[PackType["behavior"] = 1] = "behavior";
    PackType[PackType["skin"] = 2] = "skin";
    PackType[PackType["persona"] = 3] = "persona";
    PackType[PackType["design"] = 4] = "design";
})(PackType = exports.PackType || (exports.PackType = {}));
class Pack {
    packType;
    manifestFile;
    folder;
    project;
    projectItem;
    manifest;
    _items;
    //stubbing in for use later
    get isEDUOffer() {
        return false;
    }
    constructor(folderIn, packTypeIn, project, projectItem) {
        this.project = project;
        this.projectItem = projectItem;
        this.folder = folderIn;
        this.packType = packTypeIn;
    }
    ensureManifestFile() {
        if (this.manifestFile === undefined) {
            this.manifestFile = this.folder.ensureFile("manifest.json");
        }
        return this.manifestFile;
    }
    async ensureManifest() {
        if (this.manifest) {
            return this.manifest;
        }
        this.manifestFile = this.ensureManifestFile();
        if (this.packType === PackType.behavior) {
            this.manifest = await BehaviorManifestDefinition_1.default.ensureOnFile(this.manifestFile);
        }
        else if (this.packType === PackType.skin) {
            this.manifest = await SkinManifestDefinition_1.default.ensureOnFile(this.manifestFile);
        }
        else if (this.packType === PackType.persona) {
            this.manifest = await PersonaManifestDefinition_1.default.ensureOnFile(this.manifestFile);
        }
        else if (this.packType === PackType.design) {
            this.manifest = await PersonaManifestDefinition_1.default.ensureOnFile(this.manifestFile);
        }
        else {
            this.manifest = await ResourceManifestDefinition_1.default.ensureOnFile(this.manifestFile);
        }
        return this.manifest;
    }
    getManifest() {
        const manifest = this.getPackItems().find((item) => item.name === "manifest.json");
        Log_1.default.assert(!!manifest, "Pack should always have a manifest item");
        return manifest;
    }
    getPackItems() {
        if (!!this._items) {
            return this._items;
        }
        const folderPath = this.projectItem.projectPath;
        if (!folderPath) {
            throw new Error("Pack.getPackItems called without a project path");
        }
        this._items = this.project.items.filter((item) => item.projectPath?.startsWith(folderPath));
        return this._items;
    }
    static ensureOnFolder(folder, packType, project, projectItem) {
        if (folder.manager === undefined) {
            const pack = new Pack(folder, packType, project, projectItem);
            pack.project = project;
            pack.packType = packType;
            return pack;
        }
        else {
            return folder.manager;
        }
    }
    get name() {
        return this.projectItem?.name || "Unnamed pack";
    }
    async getFiles(predicate) {
        const result = [];
        for await (const file of this.folder.allFiles) {
            if (!file.isContentLoaded) {
                await file.loadContent();
            }
            if (file.content && (!predicate || predicate(file))) {
                result.push(file);
            }
        }
        return result;
    }
}
exports.default = Pack;

//# sourceMappingURL=../maps/minecraft/Pack.js.map
