"use strict";
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
const Log_1 = require("../core/Log");
const Utilities_1 = require("../core/Utilities");
const MinecraftUtilities_1 = require("../minecraft/MinecraftUtilities");
const IInfoItemData_1 = require("./IInfoItemData");
class ProjectInfoItem {
    #data;
    #projectItem;
    get dataObject() {
        return this.#data;
    }
    get itemType() {
        return this.#data.iTp;
    }
    get message() {
        return this.#data.m;
    }
    set message(newMessage) {
        this.#data.m = newMessage;
    }
    get generatorId() {
        return this.#data.gId;
    }
    get generatorIndex() {
        return this.#data.gIx;
    }
    get projectItem() {
        return this.#projectItem;
    }
    get data() {
        return this.#data.d;
    }
    set data(data) {
        this.#data.d = data;
    }
    get content() {
        return this.#data.c;
    }
    get featureSets() {
        return this.#data.fs;
    }
    get contentSummary() {
        let errorContent = this.#data.c;
        if (errorContent) {
            errorContent = errorContent.replace(/\n/gi, " ");
            errorContent = errorContent.replace(/\r/gi, " ");
            if (errorContent.length > 80) {
                errorContent = errorContent.substring(0, 77) + "...";
            }
        }
        return errorContent;
    }
    disconnect() {
        this.#projectItem = undefined;
    }
    get typeSummary() {
        switch (this.#data.iTp) {
            case IInfoItemData_1.InfoItemType.info:
                return "Info";
            case IInfoItemData_1.InfoItemType.warning:
                return "Warning";
            case IInfoItemData_1.InfoItemType.recommendation:
                return "Recommendation";
            case IInfoItemData_1.InfoItemType.featureAggregate:
                return "Feature aggregation";
            case IInfoItemData_1.InfoItemType.testCompleteFail:
                return "Test fail";
            case IInfoItemData_1.InfoItemType.testCompleteSuccess:
                return "Test success";
            case IInfoItemData_1.InfoItemType.error:
                return "Error";
            default:
                return "Unknown";
        }
    }
    get projectItemPath() {
        if (this.projectItem) {
            return this.projectItem.projectPath;
        }
        else if (this.#data.p) {
            return this.#data.p;
        }
        return undefined;
    }
    get shortProjectItemPath() {
        if (this.projectItem && this.projectItem.projectPath) {
            return MinecraftUtilities_1.default.getAfterPackPath(this.projectItem.projectPath);
        }
        else if (this.#data.p) {
            return MinecraftUtilities_1.default.getAfterPackPath(this.#data.p);
        }
        return undefined;
    }
    get typeSummaryShort() {
        let short = this.typeSummary.toUpperCase();
        short = short.replace(/\s/g, "");
        return short;
    }
    toString() {
        let summaryString = this.typeSummaryShort + ": ";
        summaryString += "[" + this.generatorId + Utilities_1.default.frontPadToLength(this.generatorIndex, 3, "0") + "] ";
        if (this.projectItem) {
            summaryString += "(" + this.projectItem.projectPath + ") ";
        }
        else if (this.#data.p) {
            summaryString += "(" + this.#data.p + ") ";
        }
        if (this.message) {
            summaryString += this.message;
        }
        if (this.data) {
            summaryString += ": " + this.data;
        }
        const errorContent = this.contentSummary;
        if (errorContent) {
            summaryString += " [in " + errorContent + "]";
        }
        return summaryString;
    }
    minFeature(setName, measureName, newValue) {
        if (this.#data.fs === undefined) {
            this.#data.fs = {};
        }
        if (!Utilities_1.default.isUsableAsObjectKey(setName)) {
            Log_1.default.unsupportedToken(setName);
            throw new Error();
        }
        let setVal = this.#data.fs[setName];
        if (setVal === undefined) {
            setVal = {};
            this.#data.fs[setName] = setVal;
        }
        let curVal = setVal[measureName];
        if (curVal === undefined) {
            curVal = newValue;
        }
        else {
            curVal = Math.min(curVal, newValue);
        }
        setVal[measureName] = curVal;
    }
    maxFeature(setName, measureName, newValue) {
        if (this.#data.fs === undefined) {
            this.#data.fs = {};
        }
        if (!Utilities_1.default.isUsableAsObjectKey(setName)) {
            Log_1.default.unsupportedToken(setName);
            throw new Error();
        }
        let setVal = this.#data.fs[setName];
        if (setVal === undefined) {
            setVal = {};
            this.#data.fs[setName] = setVal;
        }
        let curVal = setVal[measureName];
        if (curVal === undefined) {
            curVal = newValue;
        }
        else {
            curVal = Math.max(curVal, newValue);
        }
        setVal[measureName] = curVal;
    }
    getFeatureContaining(token) {
        token = token.toLowerCase();
        for (const setName in this.#data.fs) {
            const featureSet = this.#data.fs[setName];
            if (featureSet) {
                for (const measureName in featureSet) {
                    if (measureName.toLowerCase().indexOf(token) >= 0) {
                        return featureSet[measureName];
                    }
                }
            }
        }
        return undefined;
    }
    getFeatureMeasureNumber(setName, measure) {
        measure = measure.toLowerCase();
        if (!this.#data.fs) {
            return 0;
        }
        const featureSet = this.#data.fs[setName];
        if (featureSet) {
            for (const measureName in featureSet) {
                if (measureName.toLowerCase() === measure) {
                    return featureSet[measureName];
                }
            }
        }
        return 0;
    }
    getNonZeroFeatureMeasures() {
        const results = [];
        if (!this.#data.fs) {
            return results;
        }
        for (const setName in this.#data.fs) {
            const featureSet = this.#data.fs[setName];
            if (featureSet) {
                for (const measureName in featureSet) {
                    const val = featureSet[measureName];
                    if (val !== undefined &&
                        typeof val === "number" &&
                        val > 0 &&
                        (measureName === "Count" || measureName === "Instance Count")) {
                        results.push(setName);
                    }
                }
            }
        }
        return results;
    }
    spectrumFeature(setName, newValue) {
        if (this.#data.fs === undefined) {
            this.#data.fs = {};
        }
        if (!Utilities_1.default.isUsableAsObjectKey(setName)) {
            Log_1.default.unsupportedToken(setName);
            throw new Error();
        }
        this.incrementFeature(setName, "Instance Count", 1);
        this.incrementFeature(setName, "Total", newValue);
        this.maxFeature(setName, "Max", newValue);
        this.minFeature(setName, "Min", newValue);
        let setVal = this.#data.fs[setName];
        if (setVal === undefined) {
            setVal = {};
            this.#data.fs[setName] = setVal;
        }
        const curTotal = setVal["Total"];
        const curCount = setVal["Instance Count"];
        if (curCount && curTotal !== undefined) {
            setVal["Average"] = curTotal / curCount;
        }
    }
    spectrumIntFeature(setName, newValue) {
        if (this.#data.fs === undefined) {
            this.#data.fs = {};
        }
        let setVal = this.#data.fs[setName];
        if (setVal === undefined) {
            setVal = {};
            this.#data.fs[setName] = setVal;
        }
        this.incrementFeature(setName, "Instance Count", 1);
        this.incrementFeature(setName, "Total", newValue);
        this.maxFeature(setName, "Max", newValue);
        this.minFeature(setName, "Min", newValue);
        const curTotal = setVal["Total"];
        const curCount = setVal["Instance Count"];
        if (curCount && curTotal !== undefined) {
            setVal["Average"] = Math.round(curTotal / curCount);
        }
    }
    incrementFeature(setName, measureName = "Count", incrementalValue = 1) {
        if (this.#data.fs === undefined) {
            this.#data.fs = {};
        }
        if (!Utilities_1.default.isUsableAsObjectKey(setName)) {
            return;
        }
        let setVal = this.#data.fs[setName];
        if (setVal === undefined) {
            setVal = this.#data.fs[setName] = {};
        }
        setVal[measureName] = (setVal[measureName] ?? 0) + incrementalValue;
    }
    constructor(itemType, generatorId, generatorIndex, message, projectItem, data, itemId, content, projectItemPath) {
        this.#data = {
            iTp: itemType,
            gId: generatorId,
            gIx: generatorIndex,
            m: message,
            p: projectItem ? projectItem.projectPath : projectItemPath,
            d: data,
            iId: itemId,
            c: content,
            fs: undefined,
        };
        this.#projectItem = projectItem;
    }
}
exports.default = ProjectInfoItem;

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