/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
import { Progress, STATUS_ICON_FAILURE, STATUS_ICON_SKIPPED, STATUS_ICON_SUCCESS } from "@nacho-iot/js-tools";
import colors from "ansi-colors";
import { chip } from "./chip/chip.js";
import { InvalidPicsExpressionError, PicsExpression } from "./chip/pics/expression.js";
import { createNodejsMocha } from "./nodejs.js";
import { TestDescriptor } from "./test-descriptor.js";
function printReport(descriptor, includeDisabled) {
  printDescriptor(descriptor, includeDisabled);
}
function printDescriptor(descriptor, includeDisabled, prefix = "", path) {
  const { members } = descriptor;
  if (!members?.length) {
    return;
  }
  let hasPrinted = false;
  for (const member of members) {
    let title = member.name;
    let submembers = member.members ?? [];
    let steps;
    if (submembers.length) {
      if (member.kind === "py" || member.kind === "yaml" || member.kind === "manual") {
        steps = submembers.length;
        submembers = [];
      } else {
        title = colors.bold(title);
      }
    }
    if (member.isDisabled) {
      if (!includeDisabled) {
        continue;
      }
      title = colors.dim(title);
    }
    if (member.kind === "suite") {
      if (prefix === "" && hasPrinted) {
        console.log();
      }
    } else {
      switch (member.passed) {
        case true:
          title = `${STATUS_ICON_SUCCESS} ${title}`;
          break;
        case false:
          title = `${STATUS_ICON_FAILURE} ${title}`;
          break;
        default:
          title = `${STATUS_ICON_SKIPPED} ${title}`;
      }
    }
    if (member.durationMs !== void 0) {
      title = `${title} ${Progress.formatDuration(member.durationMs)}`;
    }
    if (member.kind !== "suite") {
      title = `${title} ${colors.cyan(member.kind)}`;
    }
    if (steps) {
      title = `${title} ${colors.cyan(`(${steps} steps)`)}`;
    }
    const app = member.app ?? member.config?.["app"];
    if (app) {
      title = `${title} ${colors.magenta(`[${app}]`)}`;
    }
    if (member.pics) {
      try {
        const expr = new PicsExpression(member.pics);
        if (expr.evaluate(member.picsValues ?? chip.defaultPics)) {
          title = `${title} ${colors.dim.green(member.pics)}`;
        } else {
          title = `${title} ${colors.dim.red(member.pics)}`;
        }
      } catch (e) {
        if (e instanceof InvalidPicsExpressionError) {
          title = `${title} ${colors.dim.strikethrough.red(member.pics)}`;
        } else {
          throw e;
        }
      }
    }
    if (member.path && member.path !== path) {
      let { path: path2 } = member;
      if (member.subpath) {
        path2 = `${path2}#${member.subpath}`;
      }
      title = `${title} ${colors.dim.blueBright(path2)}`;
    }
    if (member.kind === "manual") {
      title = colors.strikethrough(title);
    }
    console.log(`${prefix}${title}`);
    hasPrinted = true;
    if (submembers.length) {
      printDescriptor(member, includeDisabled, `${prefix}    `, member.path ?? path);
    }
  }
}
async function defaultDescriptor(runner) {
  const mocha = await createNodejsMocha(runner, "esm");
  let descriptor = { ...mocha.suite.descriptor };
  if (chip.isInitialized) {
    const used = /* @__PURE__ */ new Set();
    TestDescriptor.visit(descriptor, (descriptor2) => {
      used.add(descriptor2);
    });
    let unused = TestDescriptor.filter(chip.tests.descriptor, {
      predicate(descriptor2) {
        return descriptor2.kind !== "py" && descriptor2.kind !== "yaml" || !used.has(descriptor2);
      }
    });
    if (unused) {
      unused = {
        ...unused,
        name: "CHIP (unused)"
      };
      TestDescriptor.visit(unused, (descriptor2) => {
        descriptor2.isDisabled = true;
      });
      descriptor = {
        ...descriptor,
        members: descriptor.members ? [...descriptor.members, unused] : [unused]
      };
    }
  }
  const previous = await TestDescriptor.open(runner.pkg.resolve(TestDescriptor.DEFAULT_FILENAME));
  descriptor = TestDescriptor.merge(previous, descriptor);
  return descriptor;
}
export {
  defaultDescriptor,
  printReport
};
//# sourceMappingURL=print-report.js.map
