/**
 * Status Formatters
 *
 * Human-readable formatting for daemon status output.
 */
import { Array as Arr, Option } from "effect";
import { ANSI } from "../constants/ansi.js";
// ============================================================================
// Status Icons
// ============================================================================
const STATUS_ICONS = {
    healthy: `${ANSI.GREEN}●${ANSI.RESET}`, // Green circle
    degraded: `${ANSI.YELLOW}◑${ANSI.RESET}`, // Yellow half circle
    quarantined: `${ANSI.RED}✗${ANSI.RESET}`, // Red X
    unhealthy: `${ANSI.RED}○${ANSI.RESET}`, // Red empty circle
    paused: `${ANSI.GRAY}⏸${ANSI.RESET}` // Gray pause symbol
};
const STATUS_COLORS = {
    healthy: ANSI.GREEN,
    degraded: ANSI.YELLOW,
    quarantined: ANSI.RED,
    unhealthy: ANSI.RED,
    paused: ANSI.GRAY
};
// ============================================================================
// Time Formatting
// ============================================================================
/**
 * Format duration in seconds to human-readable string.
 */
export const formatDuration = (seconds) => {
    if (seconds < 60) {
        return `${Math.floor(seconds)}s`;
    }
    if (seconds < 3600) {
        const mins = Math.floor(seconds / 60);
        const secs = Math.floor(seconds % 60);
        return secs > 0 ? `${mins}m ${secs}s` : `${mins}m`;
    }
    if (seconds < 86400) {
        const hours = Math.floor(seconds / 3600);
        const mins = Math.floor((seconds % 3600) / 60);
        return mins > 0 ? `${hours}h ${mins}m` : `${hours}h`;
    }
    const days = Math.floor(seconds / 86400);
    const hours = Math.floor((seconds % 86400) / 3600);
    return hours > 0 ? `${days}d ${hours}h` : `${days}d`;
};
/**
 * Format ISO timestamp to relative time.
 */
export const formatRelativeTime = (timestamp) => {
    if (timestamp == null)
        return "never";
    const date = new Date(timestamp);
    const now = new Date();
    const diffMs = now.getTime() - date.getTime();
    const diffSecs = Math.floor(diffMs / 1000);
    if (diffSecs < 0)
        return "just now";
    if (diffSecs < 60)
        return `${diffSecs}s ago`;
    if (diffSecs < 3600)
        return `${Math.floor(diffSecs / 60)}m ago`;
    if (diffSecs < 86400)
        return `${Math.floor(diffSecs / 3600)}h ago`;
    return `${Math.floor(diffSecs / 86400)}d ago`;
};
// ============================================================================
// Status Formatting
// ============================================================================
/**
 * Build quarantine lines if status is quarantined.
 */
const buildQuarantineLines = (status) => status.quarantine.isQuarantined
    ? ["", `${ANSI.RED}Quarantined:${ANSI.RESET} ${status.quarantine.reason ?? "unknown reason"}`]
    : [];
/**
 * Build last error lines if an error is present.
 */
const buildLastErrorLines = (status) => status.lastError != null ? ["", `${ANSI.RED}Last Error:${ANSI.RESET} ${status.lastError.message}`] : [];
/**
 * Format HealthStatus as a human-readable string.
 * Uses immutable array concatenation instead of push mutations.
 */
export const formatStatus = (status) => {
    const icon = STATUS_ICONS[status.status];
    const color = STATUS_COLORS[status.status];
    const lastSync = Option.match(Arr.head(status.recentOperations), {
        onNone: () => "never",
        onSome: (lastOp) => formatRelativeTime(lastOp.timestamp)
    });
    const baseLines = [
        `${icon} Ghostflow Daemon`,
        "",
        `Status:     ${color}${status.status}${ANSI.RESET}`,
        `Uptime:     ${formatDuration(status.uptimeSeconds)}`,
        `Last Sync:  ${lastSync}`,
        "",
        "Metrics:",
        `  Syncs:    ${status.metrics.successfulSyncs}/${status.metrics.totalSyncs}`,
        `  Pulls:    ${status.metrics.successfulPulls}/${status.metrics.totalPulls}`,
        `  Pushes:   ${status.metrics.successfulPushes}/${status.metrics.totalPushes}`
    ];
    const allLines = Arr.flatten([baseLines, buildQuarantineLines(status), buildLastErrorLines(status)]);
    return Arr.join(allLines, "\n");
};
/**
 * Format status as compact one-liner.
 */
export const formatStatusCompact = (status) => {
    const icon = STATUS_ICONS[status.status];
    const uptime = formatDuration(status.uptimeSeconds);
    const syncs = `${status.metrics.successfulSyncs}/${status.metrics.totalSyncs}`;
    return `${icon} ${status.status} | uptime: ${uptime} | syncs: ${syncs}`;
};
//# sourceMappingURL=status.js.map