"use strict";
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
const ProjectInfoItem_1 = require("./ProjectInfoItem");
const IInfoItemData_1 = require("./IInfoItemData");
const StorageUtilities_1 = require("../storage/StorageUtilities");
class PackSizeInfoGenerator {
    constructor() {
        this.id = "PACKSIZE";
        this.title = "Pack Size Information";
        this.performAddOnValidations = false;
    }
    getTopicData(topicId) {
        switch (topicId) {
            case 1:
                return {
                    title: "Overall Size",
                };
            case 2:
                return {
                    title: "Overall File Count",
                };
            case 3:
                return {
                    title: "Overall Folder Count",
                };
            case 4:
                return {
                    title: "Content Size",
                };
            case 5:
                return {
                    title: "Content File Count",
                };
            case 6:
                return {
                    title: "Content Folder Count",
                };
        }
        return {
            title: topicId.toString(),
        };
    }
    summarize(info, infoSet) {
        info.overallSize = infoSet.getFirstNumberValue(this.id, 1);
        info.fileCounts = infoSet.getFirstNumberValue(this.id, 2);
        info.folderCounts = infoSet.getFirstNumberValue(this.id, 3);
        info.contentSize = infoSet.getFirstNumberValue(this.id, 4);
        info.contentFileCounts = infoSet.getFirstNumberValue(this.id, 5);
        info.contentFolderCounts = infoSet.getFirstNumberValue(this.id, 6);
    }
    async generate(project, contentIndex) {
        const genItems = [];
        const results = {
            size: 0,
            fileCounts: 0,
            folderCounts: 0,
            contentSize: 0,
            contentFileCounts: 0,
            contentFolderCounts: 0,
        };
        await this.processFolder(project, await project.ensureProjectFolder(), genItems, contentIndex, results, 0, false);
        genItems.push(new ProjectInfoItem_1.default(IInfoItemData_1.InfoItemType.featureAggregate, this.id, 1, "Overall Size", undefined, results.size));
        genItems.push(new ProjectInfoItem_1.default(IInfoItemData_1.InfoItemType.featureAggregate, this.id, 2, "File Counts", undefined, results.fileCounts));
        genItems.push(new ProjectInfoItem_1.default(IInfoItemData_1.InfoItemType.featureAggregate, this.id, 3, "Folder Counts", undefined, results.folderCounts));
        genItems.push(new ProjectInfoItem_1.default(IInfoItemData_1.InfoItemType.featureAggregate, this.id, 4, "Content Size", undefined, results.contentSize));
        genItems.push(new ProjectInfoItem_1.default(IInfoItemData_1.InfoItemType.featureAggregate, this.id, 5, "Content File Counts", undefined, results.contentFileCounts));
        genItems.push(new ProjectInfoItem_1.default(IInfoItemData_1.InfoItemType.featureAggregate, this.id, 6, "Content Folder Counts", undefined, results.contentFolderCounts));
        if (this.performAddOnValidations && results.contentSize > 25000000) {
            genItems.push(new ProjectInfoItem_1.default(IInfoItemData_1.InfoItemType.error, this.id, 401, "Exceeds recommended addon size", undefined, results.contentSize));
        }
        else if (!this.performAddOnValidations && results.contentSize > 250000000) {
            genItems.push(new ProjectInfoItem_1.default(IInfoItemData_1.InfoItemType.error, this.id, 402, "Exceeds recommended package size", undefined, results.contentSize));
        }
        return genItems;
    }
    async processFolder(project, folder, genItems, genContentIndex, results, depth, isInContent) {
        await folder.load();
        if (!isInContent && folder.files["manifest.json"]) {
            isInContent = true;
        }
        for (const fileName in folder.files) {
            const file = folder.files[fileName];
            if (file) {
                await file.loadContent();
                results.fileCounts++;
                if (StorageUtilities_1.default.isContainerFile(file.fullPath)) {
                    const storageFolder = await StorageUtilities_1.default.getFileStorageFolder(file);
                    if (storageFolder) {
                        await this.processFolder(project, storageFolder, genItems, genContentIndex, results, depth + 1, isInContent);
                    }
                }
                else {
                    if (isInContent) {
                        results.contentFileCounts++;
                        results.contentSize += file.coreContentLength;
                    }
                    results.size += file.coreContentLength;
                }
            }
        }
        if ((isInContent && depth < 15) || (!isInContent && depth < 8)) {
            for (const folderName in folder.folders) {
                const childFolder = folder.folders[folderName];
                if (childFolder && !childFolder.errorStatus && childFolder.name) {
                    results.folderCounts++;
                    if (isInContent) {
                        results.contentFolderCounts++;
                    }
                    await this.processFolder(project, childFolder, genItems, genContentIndex, results, depth + 1, isInContent);
                }
            }
        }
    }
}
exports.default = PackSizeInfoGenerator;

//# sourceMappingURL=../maps/info/PackSizeInfoGenerator.js.map
