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 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);
}
