"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/playwright/reporter/index.ts
var reporter_exports = {};
__export(reporter_exports, {
  default: () => reporter_default
});
module.exports = __toCommonJS(reporter_exports);

// src/common/utils.ts
var import_ai_model = require("@midscene_spp/core/ai-model");
var import_utils = require("@midscene_spp/core/utils");
var import_env = require("@midscene_spp/shared/env");
var import_extractor = require("@midscene_spp/shared/extractor");
var import_img = require("@midscene_spp/shared/img");
var import_utils2 = require("@midscene_spp/shared/utils");
var import_dayjs = __toESM(require("dayjs"));
function reportFileName(tag = "web") {
  const reportTagName = (0, import_env.getAIConfig)(import_env.MIDSCENE_REPORT_TAG_NAME);
  const dateTimeInFileName = (0, import_dayjs.default)().format("YYYY-MM-DD_HH-mm-ss");
  const uniqueId = (0, import_utils2.uuid)().substring(0, 8);
  return `${reportTagName || tag}-${dateTimeInFileName}-${uniqueId}`;
}
function printReportMsg(filepath) {
  (0, import_utils2.logMsg)(`Midscene - report file updated: ${filepath}`);
}
function replaceIllegalPathCharsAndSpace(str) {
  return str.replace(/[/\\:*?"<>| ]/g, "-");
}

// src/playwright/reporter/index.ts
var import_utils4 = require("@midscene_spp/core/utils");
function logger(...message) {
  if (process.env.DEBUG === "true") {
    console.log("Midscene e2e report:", ...message);
  }
}
var testDataList = [];
var mergedFilename;
var testTitleToFilename = /* @__PURE__ */ new Map();
function getStableFilename(testTitle) {
  if (!testTitleToFilename.has(testTitle)) {
    const baseTag = `playwright-${replaceIllegalPathCharsAndSpace(testTitle)}`;
    const generatedFilename = reportFileName(baseTag);
    testTitleToFilename.set(testTitle, generatedFilename);
  }
  return testTitleToFilename.get(testTitle);
}
function updateReport(mode, testId) {
  if (mode === "separate") {
    const testData = testDataList.find(
      (data) => data.attributes?.playwright_test_id === testId
    );
    if (testData) {
      const stableFilename = getStableFilename(
        testData.attributes?.playwright_test_title
      );
      const reportPath = (0, import_utils4.writeDumpReport)(stableFilename, [testData]);
      reportPath && printReportMsg(reportPath);
    }
  } else if (mode === "merged") {
    if (!mergedFilename) {
      mergedFilename = reportFileName("playwright-merged");
    }
    const reportPath = (0, import_utils4.writeDumpReport)(mergedFilename, testDataList);
    reportPath && printReportMsg(reportPath);
  } else {
    throw new Error(
      `Unknown reporter type in playwright config: ${mode}, only support 'merged' or 'separate'`
    );
  }
}
function getMode(reporterType) {
  if (!reporterType) {
    return "merged";
  }
  if (reporterType !== "merged" && reporterType !== "separate") {
    throw new Error(
      `Unknown reporter type in playwright config: ${reporterType}, only support 'merged' or 'separate'`
    );
  }
  return reporterType;
}
var MidsceneReporter = class {
  async onBegin(config, suite) {
    const reporterType = config.reporter?.[1]?.[1]?.type;
    this.mode = getMode(reporterType);
  }
  onTestBegin(test, _result) {
  }
  onTestEnd(test, result) {
    const dumpAnnotation = test.annotations.find((annotation) => {
      return annotation.type === "MIDSCENE_DUMP_ANNOTATION";
    });
    if (!dumpAnnotation?.description)
      return;
    const retry = result.retry ? `(retry #${result.retry})` : "";
    const testId = `${test.id}${retry}`;
    const testData = {
      dumpString: dumpAnnotation.description,
      attributes: {
        playwright_test_id: testId,
        playwright_test_title: `${test.title}${retry}`,
        playwright_test_status: result.status,
        playwright_test_duration: result.duration
      }
    };
    testDataList.push(testData);
    updateReport(this.mode, testId);
    test.annotations = test.annotations.filter(
      (annotation) => annotation.type !== "MIDSCENE_DUMP_ANNOTATION"
    );
  }
  onEnd(result) {
    updateReport(this.mode);
    logger(`Finished the run: ${result.status}`);
  }
};
var reporter_default = MidsceneReporter;

//# sourceMappingURL=playwright-report.js.map