import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { Box, Text } from "ink";
import { useMemoryUsage } from "../hooks/useMemoryUsage.js";
import { StatusBadge } from "./design/StatusBadge.js";
import { truncateCells } from "./design/textLayout.js";
export function MemoryUsageIndicator(props) {
    const info = useMemoryUsage();
    const model = memoryUsageIndicatorModel(info);
    if (!model)
        return null;
    const width = props.width ?? 80;
    return (_jsxs(Box, { flexDirection: "row", paddingX: 1, children: [_jsx(StatusBadge, { label: model.label, tone: model.tone }), _jsx(Text, { color: "gray", children: " " }), _jsx(Text, { color: "gray", children: truncateCells(model.text, Math.max(20, width - 12)) })] }));
}
export function memoryUsageIndicatorModel(info) {
    if (!info || info.status === "normal")
        return null;
    const size = formatMemoryBytes(info.heapUsed);
    if (info.status === "critical") {
        return {
            label: "memory",
            tone: "error",
            text: `critical heap ${size}; run /compact, finish current task, then restart before large edits`,
        };
    }
    return {
        label: "memory",
        tone: "warning",
        text: `high heap ${size}; use /compact or /cache plan before a token-heavy request`,
    };
}
export function formatMemoryBytes(bytes) {
    const units = ["B", "KB", "MB", "GB", "TB"];
    let value = Math.max(0, bytes);
    let unitIndex = 0;
    while (value >= 1024 && unitIndex < units.length - 1) {
        value /= 1024;
        unitIndex += 1;
    }
    const precision = value >= 10 || unitIndex === 0 ? 0 : 1;
    return `${value.toFixed(precision)} ${units[unitIndex]}`;
}
