"use strict";

// ─── Output Control ──────────────────────────────────────────────────────────

const output = { level: "normal", isSilent: false };

// Theme import — lazy loaded to avoid startup penalty when not needed
let _theme = null;
function getTheme() {
  if (!_theme) {
    try {
      _theme = require("../ui/theme");
    } catch {
      // Fallback when UI modules not available (e.g. in tests)
      _theme = {
        success: (s) => s,
        error: (s) => s,
        warning: (s) => s,
        info: (s) => s,
        dim: (s) => s,
        bold: (s) => s,
        brand: (s) => s,
        symbols: {
          active: "[*]",
          complete: "[x]",
          error: "[!]",
          skip: "[-]",
          progress: "[>]",
          pending: "[ ]",
        },
        isColorSupported: false,
      };
    }
  }
  return _theme;
}

function log(msg) {
  if (output.isSilent || output.level === "quiet") return;
  console.log(`  ${msg}`);
}

function logHeader(msg) {
  if (output.isSilent || output.level === "quiet") return;
  const t = getTheme();
  console.log(`\n${t.bold(msg)}`);
}

function logSuccess(msg) {
  if (output.isSilent || output.level === "quiet") return;
  const t = getTheme();
  console.log(`  ${t.success(t.symbols.complete)} ${msg}`);
}

function logSkip(msg) {
  if (output.isSilent || output.level === "quiet") return;
  const t = getTheme();
  console.log(`  ${t.dim(t.symbols.skip)} ${msg}`);
}

function logError(msg) {
  // logError always prints, even in quiet/silent mode
  const t = getTheme();
  console.error(`  ${t.error(t.symbols.error)} ${msg}`);
}

function logWarning(msg) {
  if (output.isSilent || output.level === "quiet") return;
  const t = getTheme();
  console.log(`  ${t.warning("[~]")} ${msg}`);
}

function logVerbose(msg) {
  if (output.level !== "verbose") return;
  if (output.isSilent) return;
  const t = getTheme();
  console.log(`  ${t.dim(msg)}`);
}

module.exports = {
  output,
  getTheme,
  log,
  logHeader,
  logSuccess,
  logSkip,
  logError,
  logWarning,
  logVerbose,
};
