"use strict";

// ─── Color System ──────────────────────────────────────────────────────────

const USE_COLOR = !process.env.NO_COLOR && process.env.TERM !== "dumb";
const IS_TRUECOLOR =
  process.env.COLORTERM === "truecolor" || process.env.COLORTERM === "24bit";

/**
 * Create a color function that picks truecolor, 256-color, or 16-color ANSI.
 * Returns a function that wraps text in the chosen escape sequence.
 */
function color(tc, c256) {
  if (!USE_COLOR) return (s) => s;
  const code = IS_TRUECOLOR ? tc : c256;
  return (s) => `\x1b[${code}m${s}\x1b[39m`;
}

// Warm Neutral palette
const BRAND = color("38;2;255;140;0", "38;5;208");
const AMBER = color("38;2;204;153;0", "38;5;178");
const SLATE = color("38;2;108;153;204", "38;5;110");
const OK = color("38;2;76;191;115", "38;5;78");
const WARN = color("38;2;230;190;50", "38;5;220");
const ERR = color("38;2;230;80;80", "38;5;167");
const MUTED = color("38;2;140;140;140", "38;5;245");
const SEP_C = color("38;2;70;70;70", "38;5;238");

// Typography helpers — use SGR-specific resets for nesting
const BOLD = USE_COLOR ? "\x1b[1m" : "";
const DIM = USE_COLOR ? "\x1b[2m" : "";
const ITALIC = USE_COLOR ? "\x1b[3m" : "";
const RESET = USE_COLOR ? "\x1b[0m" : "";

// Shorthand: italic dim for labels
function lbl(s) {
  if (!USE_COLOR) return s;
  return `${ITALIC}${DIM}${s}${RESET}`;
}

// Nerd Font detection
const USE_NERDFONT = (() => {
  if (process.env.NERD_FONTS || process.env.NERDFONT) return true;
  const fontName = process.env.TERM_FONT || process.env.LC_TERMINAL_FONT || "";
  return /nerd/i.test(fontName);
})();

const ICONS = USE_NERDFONT
  ? {
      brand: " ",
      output: " ",
      status: " ",
      model: "󰚩 ",
      cache: "󰑮 ",
      tokens: "󰦨 ",
      project: " ",
      folder: " ",
      branch: " ",
      mode: "󰔡 ",
      usage: " ",
      clock: " ",
      files: " ",
      prot: " ",
      dirty: " ",
    }
  : {
      brand: "",
      output: "",
      status: "",
      model: "",
      cache: "",
      tokens: "",
      project: "",
      folder: "",
      branch: "",
      mode: "",
      usage: "",
      clock: "",
      files: "",
      prot: "",
      dirty: "",
    };

module.exports = {
  USE_COLOR,
  IS_TRUECOLOR,
  color,
  BRAND,
  AMBER,
  SLATE,
  OK,
  WARN,
  ERR,
  MUTED,
  SEP_C,
  BOLD,
  DIM,
  ITALIC,
  RESET,
  lbl,
  USE_NERDFONT,
  ICONS,
};
