/**
 * Shared logging utilities for Vue client.
 * Console-only, no server sync.
 */

const LOG_LEVELS = {
  DEBUG: 0,
  INFO: 1,
  WARN: 2,
  ERROR: 3,
};

function getMinLevel() {
  const envLevel = import.meta.env.VITE_LOG_LEVEL?.toUpperCase();
  return LOG_LEVELS[envLevel] ?? LOG_LEVELS.INFO;
}

export function isDebugEnabled() {
  return getMinLevel() <= LOG_LEVELS.DEBUG;
}

export function shouldLog(level) {
  return LOG_LEVELS[level] >= getMinLevel();
}

export function formatDuration(ms) {
  return ms < 10 ? `${ms.toFixed(1)}ms` : `${Math.round(ms)}ms`;
}

export function truncate(str, maxLen = 500) {
  if (!str) return "";
  const s = String(str);
  return s.length > maxLen ? s.slice(0, maxLen) + "…" : s;
}

export function safeStringify(value, maxLen) {
  try {
    const json = JSON.stringify(value);
    return maxLen ? truncate(json, maxLen) : json;
  } catch {
    return "[Circular]";
  }
}
