import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { Box, Text } from "ink";
import { toneColor } from "./terminalTheme.js";
export function buildProgressBarParts(ratio, width, chars = {}) {
    const normalizedWidth = Math.max(0, Math.floor(width));
    const normalizedRatio = clampRatio(ratio);
    const filledChar = chars.filled ?? "#";
    const emptyChar = chars.empty ?? ".";
    const filledCells = Math.round(normalizedRatio * normalizedWidth);
    const emptyCells = Math.max(0, normalizedWidth - filledCells);
    return {
        ratio: normalizedRatio,
        filled: filledChar.repeat(filledCells),
        empty: emptyChar.repeat(emptyCells),
        percent: `${Math.round(normalizedRatio * 100)}%`,
    };
}
export function ProgressBar(props) {
    const parts = buildProgressBarParts(props.ratio, props.width);
    return (_jsxs(Box, { flexDirection: "row", children: [_jsx(Text, { color: "gray", children: "[" }), _jsx(Text, { color: toneColor(props.filledTone ?? "brand"), children: parts.filled }), _jsx(Text, { color: toneColor(props.emptyTone ?? "muted"), children: parts.empty }), _jsx(Text, { color: "gray", children: "]" }), props.showPercent && _jsx(Text, { color: "gray", children: ` ${parts.percent}` })] }));
}
export function clampRatio(value) {
    if (!Number.isFinite(value))
        return 0;
    if (value < 0)
        return 0;
    if (value > 1)
        return 1;
    return value;
}
