/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
import { ansi, std } from "@nacho-iot/js-tools";
import { FailureReporter } from "./failure-reporter.js";
class NodejsReporter {
  constructor(progress) {
    this.progress = progress;
  }
  progress;
  #run = "";
  #suite = Array();
  #test;
  #step;
  #stats;
  #failures = Array();
  #lastTitle;
  beginRun(name, stats, supportsSuites = true) {
    this.#run = name;
    this.#suite = [];
    this.#failures = [];
    if (!supportsSuites) {
      this.progress.update(this.#summarize(stats));
    }
  }
  beginSuite(name, _stats, _file) {
    this.#suite = name;
  }
  beginTest(name, stats) {
    this.#test = name;
    this.#stats = stats;
    this.#step = void 0;
    const title = process.stdout.isTTY ? this.#formatName(this.#suite, name) : this.#suite[0];
    this.#updateTitle(title);
  }
  beginStep(name) {
    this.#step = name;
    if (process.stdout.isTTY && this.#test) {
      const title = this.#formatName(this.#suite, this.#test, name);
      this.#updateTitle(title);
    }
  }
  #updateTitle(title) {
    if (title === this.#lastTitle) {
      return;
    }
    this.#lastTitle = title;
    this.progress.update(this.#summarize(this.#stats), title);
  }
  passTest(_name) {
  }
  failTest(name, detail) {
    this.#failures.push({
      suite: this.#suite,
      test: name,
      step: this.#step,
      detail
    });
  }
  endRun(stats) {
    if (this.#failures.length) {
      this.progress.failure(this.#summarize(stats));
      this.#dumpFailures();
    } else if (stats && !stats.complete) {
      this.progress.failure("No tests found");
    } else {
      this.progress.success(this.#summarize(stats));
    }
  }
  #summarize(stats) {
    let statStr;
    if (stats) {
      const complete = ansi.dim(`${stats.complete}/${stats.total}`);
      const failures = stats.failures ? ansi.bright.red(` ${stats.failures.toString()} failed`) : "";
      statStr = ` ${complete}${failures}`;
    } else {
      statStr = "";
    }
    return `${ansi.bold(this.#run)}${statStr}`;
  }
  #dumpFailures() {
    std.out("\n");
    for (let i = 0; i < this.#failures.length; i++) {
      const failure = this.#failures[i];
      const index = `Failure ${ansi.bold((i + 1).toString())} of ${this.#failures.length}: `;
      const title = `${index}${this.#formatName(failure.suite, failure.test, failure.step)}`;
      FailureReporter.report(std.out, failure.detail, title);
    }
  }
  #formatName(suite, test, step) {
    const breadcrumb = [...suite, test];
    if (step) {
      breadcrumb.push(step);
    }
    breadcrumb[breadcrumb.length - 1] = ansi.bold(breadcrumb[breadcrumb.length - 1]).toString();
    return breadcrumb.join(" \u27A1 ");
  }
}
export {
  NodejsReporter
};
//# sourceMappingURL=nodejs-reporter.js.map
