import { createColors } from "picocolors";

const pc = createColors(Boolean(process.stdout?.isTTY || process.env.FORCE_COLOR));

export function colorStatus(status) {
  if (status === "PASS") return pc.green(status);
  if (status === "FAIL") return pc.red(status);
  if (status === "SKIP") return pc.yellow(status);
  if (status === "RUN") return pc.cyan(status);
  return status;
}

export function dim(text) {
  return pc.dim(text);
}

export function bold(text) {
  return pc.bold(text);
}

export function muted(text) {
  return pc.dim(text);
}

export function colorHeading(text) {
  return pc.bold(text);
}

export function colorService(text) {
  return pc.cyan(pc.bold(text));
}

export function colorTypeBadge(type) {
  if (type === "INT") return pc.blue(type);
  if (type === "E2E") return pc.magenta(type);
  if (type === "SCENARIO") return pc.yellow(type);
  if (type === "DAL") return pc.green(type);
  if (type === "LOAD") return pc.cyan(type);
  if (type === "PW") return pc.blue(pc.bold(type));
  return type;
}

export function colorDiagnosticSeverity(severity) {
  if (severity === "error") return pc.red(pc.bold("ERR"));
  if (severity === "warning") return pc.yellow(pc.bold("WARN"));
  return pc.dim("INFO");
}

export function colorResultLine(line) {
  if (/^Result: PASSED\b/.test(line)) return line.replace("PASSED", pc.green("PASSED"));
  if (/^Result: FAILED\b/.test(line)) return line.replace("FAILED", pc.red("FAILED"));
  return line;
}

export function colorSectionLine(line) {
  if (line === "Failures:" || line === "Runtime Errors:" || line === "Known-failure issues:" || line === "Triage:") {
    return pc.bold(line);
  }
  if (/^Summary: /.test(line)) return line.replace("Summary:", `${pc.bold("Summary:")}`);
  return colorResultLine(line);
}
