import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
/**
 * Health Bar Component
 *
 * Visualizes sync history as a timeline of dots.
 * Each dot represents 15 minutes, showing success/failure/no-activity.
 */
import { Array as Arr, Match, pipe } from "effect";
import { Box, Text } from "ink";
import { memo } from "react";
// ============================================================================
// Constants
// ============================================================================
/** Number of dots to display (12 dots = 3 hours at 15 min each) */
const DOT_COUNT = 12;
/** Minutes per dot for health bar resolution */
const MINUTES_PER_DOT = 15;
/** Unicode dot symbols for health status */
const SYMBOL_SUCCESS = "●";
const SYMBOL_INACTIVE = "○";
const SYMBOL_WARNING = "◐";
const SYMBOL_FAILED = "✕";
/** Get display configuration for a slot status */
const getSlotConfig = (status) => pipe(Match.value(status), Match.when("success", () => ({ symbol: SYMBOL_SUCCESS, color: "green" })), Match.when("inactive", () => ({ symbol: SYMBOL_INACTIVE, color: "gray" })), Match.when("warning", () => ({ symbol: SYMBOL_WARNING, color: "yellow" })), Match.when("failed", () => ({ symbol: SYMBOL_FAILED, color: "red" })), Match.exhaustive);
/** Render a single dot with appropriate color */
const renderDot = (slot, index) => {
    const config = getSlotConfig(slot.status);
    return (_jsx(Text, { color: config.color, children: config.symbol }, index));
};
/** Pad slots to ensure we always have DOT_COUNT slots */
const padSlots = (slots) => {
    const inactiveSlot = { status: "inactive", timestamp: 0 };
    const paddingNeeded = DOT_COUNT - slots.length;
    return paddingNeeded > 0
        ? Arr.appendAll(slots, Arr.replicate(inactiveSlot, paddingNeeded))
        : Arr.take(slots, DOT_COUNT);
};
// ============================================================================
// Component
// ============================================================================
/**
 * Health bar showing sync status over time.
 *
 * Displays 12 dots representing the last 3 hours of sync activity.
 * Each dot represents a 15-minute interval.
 */
export const HealthBar = memo(({ slots, timeLabel }) => {
    const paddedSlots = padSlots(slots);
    return (_jsxs(Box, { gap: 0, children: [timeLabel !== undefined && _jsx(Text, { dimColor: true, children: `${timeLabel} ` }), _jsx(Box, { children: Arr.map(paddedSlots, renderDot) })] }));
});
HealthBar.displayName = "HealthBar";
// ============================================================================
// Factory Functions
// ============================================================================
/** Create a health bar with all success dots */
export const createSuccessBar = (count = DOT_COUNT) => Arr.replicate({ status: "success", timestamp: Date.now() }, count);
/** Create a health bar with all inactive dots (for paused flows) */
export const createInactiveBar = (count = DOT_COUNT) => Arr.replicate({ status: "inactive", timestamp: 0 }, count);
/** Create a health bar from sync events */
export const createHealthBarFromEvents = (events, nowMs) => {
    const msPerSlot = MINUTES_PER_DOT * 60 * 1000;
    // Create slots from newest to oldest
    return Arr.map(Arr.range(0, DOT_COUNT - 1), (slotIndex) => {
        const slotEnd = nowMs - slotIndex * msPerSlot;
        const slotStart = slotEnd - msPerSlot;
        // Find events in this slot
        const slotEvents = Arr.filter(events, (e) => e.timestamp >= slotStart && e.timestamp < slotEnd);
        // Determine status based on events
        const status = Arr.match(slotEvents, {
            onEmpty: () => "inactive",
            onNonEmpty: (nonEmpty) => {
                const hasFailure = Arr.some(nonEmpty, (e) => !e.success);
                const hasSuccess = Arr.some(nonEmpty, (e) => e.success);
                if (hasFailure && hasSuccess)
                    return "warning";
                if (hasFailure)
                    return "failed";
                return "success";
            }
        });
        return { status, timestamp: slotEnd };
    });
};
// ============================================================================
// Exports
// ============================================================================
export { DOT_COUNT, MINUTES_PER_DOT };
//# sourceMappingURL=HealthBar.js.map