import a from './index5.mjs';
import '../shared/ccjk.BAGoDD49.mjs';

function formatTokenCount(tokens) {
  if (tokens >= 1e6) {
    return `${(tokens / 1e6).toFixed(1)}M`;
  }
  if (tokens >= 1e3) {
    return `${(tokens / 1e3).toFixed(1)}K`;
  }
  return tokens.toString();
}
function formatCost(cost) {
  if (cost >= 1) {
    return `$${cost.toFixed(2)}`;
  }
  if (cost >= 0.01) {
    return `$${cost.toFixed(3)}`;
  }
  return `$${cost.toFixed(4)}`;
}
function formatRatio(ratio) {
  return `${Math.round(ratio * 100)}%`;
}
function formatTime(ms) {
  if (ms >= 1e3) {
    return `${(ms / 1e3).toFixed(2)}s`;
  }
  return `${Math.round(ms)}ms`;
}
function displayCompressionResult(originalTokens, compressedTokens, compressionRatio, timeTakenMs) {
  const reductionPercent = Math.round(compressionRatio * 100);
  const originalFormatted = formatTokenCount(originalTokens);
  const compressedFormatted = formatTokenCount(compressedTokens);
  console.log(
    `${a.green("\u2705")} Compressed ${a.cyan(originalFormatted)} tokens \u2192 ${a.cyan(compressedFormatted)} ${a.yellow(`(${reductionPercent}% reduction)`)} ${a.gray(`[${formatTime(timeTakenMs)}]`)}`
  );
}
function displayCompressionStats(stats) {
  console.log();
  console.log(a.cyan.bold("\u{1F4CA} Compression Statistics"));
  console.log(a.gray("\u2500".repeat(60)));
  console.log();
  console.log(a.white.bold("Overall:"));
  console.log(`  ${a.gray("Total Compressions:")}  ${a.white(stats.totalCompressions.toLocaleString())}`);
  console.log(`  ${a.gray("Tokens Saved:")}       ${a.green(formatTokenCount(stats.totalTokensSaved))}`);
  console.log(`  ${a.gray("Avg Compression:")}    ${a.yellow(formatRatio(stats.averageCompressionRatio))}`);
  console.log(`  ${a.gray("Avg Time:")}           ${a.cyan(formatTime(stats.averageTimeTakenMs))}`);
  console.log(`  ${a.gray("Cost Savings:")}       ${a.green.bold(formatCost(stats.estimatedCostSavings))}`);
  if (stats.sessionStats && stats.sessionStats.compressions > 0) {
    console.log();
    console.log(a.white.bold("Session (24h):"));
    console.log(`  ${a.gray("Compressions:")}      ${a.white(stats.sessionStats.compressions.toLocaleString())}`);
    console.log(`  ${a.gray("Tokens Saved:")}      ${a.green(formatTokenCount(stats.sessionStats.tokensSaved))}`);
    console.log(`  ${a.gray("Cost Savings:")}      ${a.green(formatCost(stats.sessionStats.costSavings))}`);
  }
  if (stats.weeklyStats && stats.weeklyStats.compressions > 0) {
    console.log();
    console.log(a.white.bold("Weekly (7d):"));
    console.log(`  ${a.gray("Compressions:")}      ${a.white(stats.weeklyStats.compressions.toLocaleString())}`);
    console.log(`  ${a.gray("Tokens Saved:")}      ${a.green(formatTokenCount(stats.weeklyStats.tokensSaved))}`);
    console.log(`  ${a.gray("Cost Savings:")}      ${a.green(formatCost(stats.weeklyStats.costSavings))}`);
  }
  if (stats.monthlyStats && stats.monthlyStats.compressions > 0) {
    console.log();
    console.log(a.white.bold("Monthly (30d):"));
    console.log(`  ${a.gray("Compressions:")}      ${a.white(stats.monthlyStats.compressions.toLocaleString())}`);
    console.log(`  ${a.gray("Tokens Saved:")}      ${a.green(formatTokenCount(stats.monthlyStats.tokensSaved))}`);
    console.log(`  ${a.gray("Cost Savings:")}      ${a.green(formatCost(stats.monthlyStats.costSavings))}`);
  }
  console.log();
  console.log(a.gray("\u2500".repeat(60)));
  console.log(a.gray("\u{1F4A1} Cost based on $0.015 per 1K tokens (Claude Opus pricing)"));
  console.log();
}
function displayCompactCompressionStats(stats) {
  const lines = [];
  lines.push(a.cyan.bold("Compression Metrics"));
  lines.push(`  ${a.gray("Total Saved:")}        ${a.green(formatTokenCount(stats.totalTokensSaved))} tokens`);
  lines.push(`  ${a.gray("Avg Reduction:")}      ${a.yellow(formatRatio(stats.averageCompressionRatio))}`);
  lines.push(`  ${a.gray("Cost Savings:")}       ${a.green.bold(formatCost(stats.estimatedCostSavings))}`);
  if (stats.sessionStats && stats.sessionStats.compressions > 0) {
    lines.push(`  ${a.gray("Session (24h):")}     ${a.white(stats.sessionStats.compressions)} compressions, ${a.green(formatCost(stats.sessionStats.costSavings))} saved`);
  }
  return lines;
}
function createCompressionBar(ratio, width = 20) {
  const filled = Math.round(ratio * width);
  const empty = width - filled;
  let bar = "";
  if (ratio >= 0.7) {
    bar = a.green("\u2588".repeat(filled));
  } else if (ratio >= 0.4) {
    bar = a.yellow("\u2588".repeat(filled));
  } else {
    bar = a.red("\u2588".repeat(filled));
  }
  bar += a.gray("\u2591".repeat(empty));
  return bar;
}
function displayCompressionTable(metrics, limit = 10) {
  if (metrics.length === 0) {
    console.log(a.gray("No compression metrics available"));
    return;
  }
  console.log();
  console.log(a.cyan.bold("Recent Compressions"));
  console.log(a.gray("\u2500".repeat(80)));
  console.log(
    a.white.bold("Original".padEnd(12)) + a.white.bold("Compressed".padEnd(12)) + a.white.bold("Ratio".padEnd(10)) + a.white.bold("Time".padEnd(10)) + a.white.bold("Strategy".padEnd(15)) + a.white.bold("Algorithm")
  );
  console.log(a.gray("\u2500".repeat(80)));
  const displayMetrics = metrics.slice(0, limit);
  for (const metric of displayMetrics) {
    const original = formatTokenCount(metric.originalTokens).padEnd(12);
    const compressed = formatTokenCount(metric.compressedTokens).padEnd(12);
    const ratio = formatRatio(metric.compressionRatio).padEnd(10);
    const time = formatTime(metric.timeTakenMs).padEnd(10);
    const strategy = metric.strategy.padEnd(15);
    const algorithm = metric.algorithm;
    console.log(
      a.cyan(original) + a.green(compressed) + a.yellow(ratio) + a.gray(time) + a.white(strategy) + a.gray(algorithm)
    );
  }
  console.log(a.gray("\u2500".repeat(80)));
  console.log();
}
function formatBytes(bytes) {
  if (bytes >= 1073741824) {
    return `${(bytes / 1073741824).toFixed(2)} GB`;
  }
  if (bytes >= 1048576) {
    return `${(bytes / 1048576).toFixed(2)} MB`;
  }
  if (bytes >= 1024) {
    return `${(bytes / 1024).toFixed(2)} KB`;
  }
  return `${bytes} bytes`;
}
const MetricsDisplay = {
  displayCompressionResult,
  displayCompressionStats,
  displayCompactCompressionStats,
  displayCompressionTable,
  createCompressionBar,
  formatTokenCount,
  formatCost,
  formatRatio,
  formatTime,
  formatBytes
};

export { MetricsDisplay, createCompressionBar, displayCompactCompressionStats, displayCompressionResult, displayCompressionStats, displayCompressionTable };
