import logger from "../Logger";
import Typeof from "../../../Types/Typeof";
export default class MonitorCriteriaMessageFormatter {
    static formatNumber(value, options) {
        if (value === null || value === undefined || isNaN(value)) {
            return null;
        }
        const fractionDigits = (options === null || options === void 0 ? void 0 : options.maximumFractionDigits) !== undefined
            ? options.maximumFractionDigits
            : Math.abs(value) < 10
                ? 2
                : Math.abs(value) < 100
                    ? 1
                    : 0;
        return value.toFixed(fractionDigits);
    }
    static formatPercentage(value) {
        const formatted = MonitorCriteriaMessageFormatter.formatNumber(value, {
            maximumFractionDigits: value !== null && value !== undefined && Math.abs(value) < 100
                ? 1
                : 0,
        });
        if (!formatted) {
            return null;
        }
        return `${formatted}%`;
    }
    static formatBytes(bytes) {
        if (bytes === null || bytes === undefined || isNaN(bytes)) {
            return null;
        }
        const units = ["B", "KB", "MB", "GB", "TB", "PB"];
        let value = bytes;
        let index = 0;
        while (value >= 1024 && index < units.length - 1) {
            value = value / 1024;
            index++;
        }
        const formatted = MonitorCriteriaMessageFormatter.formatNumber(value, {
            maximumFractionDigits: value >= 100 ? 0 : value >= 10 ? 1 : 2,
        });
        if (!formatted) {
            return null;
        }
        return `${formatted} ${units[index]}`;
    }
    static formatList(items, maxItems = 5) {
        if (!items.length) {
            return "";
        }
        const trimmedItems = items.slice(0, maxItems);
        const suffix = items.length > maxItems ? `, +${items.length - maxItems} more` : "";
        return `${trimmedItems.join(", ")} ${suffix}`.trim();
    }
    static formatSnippet(text, maxLength = 120) {
        const sanitized = text.replace(/\s+/g, " ").trim();
        if (sanitized.length <= maxLength) {
            return sanitized;
        }
        return `${sanitized.slice(0, maxLength)}…`;
    }
    static describeProcesses(processes) {
        if (!processes.length) {
            return null;
        }
        const processSummaries = processes.map((process) => {
            return `${process.name} (pid ${process.pid})`;
        });
        return MonitorCriteriaMessageFormatter.formatList(processSummaries);
    }
    static computeDiskUsagePercent(diskMetric) {
        if (!diskMetric) {
            return null;
        }
        if (diskMetric.percentUsed !== undefined &&
            diskMetric.percentUsed !== null &&
            !isNaN(diskMetric.percentUsed)) {
            return diskMetric.percentUsed;
        }
        if (diskMetric.percentFree !== undefined &&
            diskMetric.percentFree !== null &&
            !isNaN(diskMetric.percentFree)) {
            return 100 - diskMetric.percentFree;
        }
        if (diskMetric.total && diskMetric.used && diskMetric.total > 0) {
            return (diskMetric.used / diskMetric.total) * 100;
        }
        return null;
    }
    static summarizeNumericSeries(values) {
        if (!values.length) {
            return null;
        }
        const latest = values[values.length - 1];
        if (latest === undefined) {
            return null;
        }
        const latestFormatted = MonitorCriteriaMessageFormatter.formatNumber(latest, {
            maximumFractionDigits: 2,
        });
        let summary = `latest ${latestFormatted !== null && latestFormatted !== void 0 ? latestFormatted : latest}`;
        if (values.length > 1) {
            const min = Math.min(...values);
            const max = Math.max(...values);
            const minFormatted = MonitorCriteriaMessageFormatter.formatNumber(min, {
                maximumFractionDigits: 2,
            });
            const maxFormatted = MonitorCriteriaMessageFormatter.formatNumber(max, {
                maximumFractionDigits: 2,
            });
            summary += ` (min ${minFormatted !== null && minFormatted !== void 0 ? minFormatted : min}, max ${maxFormatted !== null && maxFormatted !== void 0 ? maxFormatted : max})`;
        }
        summary += ` across ${values.length} data point${values.length === 1 ? "" : "s"}`;
        return summary;
    }
    static formatResultValue(value) {
        if (value === null || value === undefined) {
            return "undefined";
        }
        if (typeof value === Typeof.Object) {
            try {
                return JSON.stringify(value);
            }
            catch (err) {
                logger.error(err);
                return "[object]";
            }
        }
        return value.toString();
    }
}
//# sourceMappingURL=MonitorCriteriaMessageFormatter.js.map