"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);
var web_exports = {};
__export(web_exports, {
  testWeb: () => testWeb
});
module.exports = __toCommonJS(web_exports);
var import_js_tools = require("@nacho-iot/js-tools");
var import_esbuild = require("esbuild");
var import_express = __toESM(require("express"), 1);
var import_promises = require("node:fs/promises");
var import_node_path = require("node:path");
var import_playwright = require("playwright");
var import_web_reporter = require("./web-reporter.js");
/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
async function testWeb(runner, manual) {
  const files = await runner.loadFiles("esm");
  const bundlePath = await bundle(files, runner);
  const server = await new Promise((resolve, reject) => {
    try {
      const server2 = (0, import_express.default)().use(import_express.default.static(import_js_tools.Package.workspace.resolve("node_modules"))).use(import_express.default.static(import_js_tools.Package.workspace.path)).get("/", (_, res) => {
        res.send(buildIndex(bundlePath));
      }).listen(0, "localhost", () => resolve(server2));
    } catch (e) {
      reject(e);
    }
  });
  const addr = server.address();
  let ip = addr.address;
  if (ip.indexOf(":") !== -1) {
    ip = `[${ip}]`;
  }
  const url = `http://${ip}:${addr.port}/`;
  await new Promise((resolve, reject) => {
    server.on("error", reject);
    server.on("close", resolve);
    if (manual) {
      console.log(`Run tests manually at ${url}`);
    } else {
      testInBrowser(url, runner.reporter, runner.options).then(() => {
        server.close(() => {
          resolve();
        });
      }).catch((error) => {
        reject(error);
      });
    }
  });
}
testWeb.entrypointPreprocessor = void 0;
async function testInBrowser(url, reporter, options) {
  async function setup() {
    const browser = await import_playwright.chromium.launch();
    const page = await browser.newPage();
    return { browser, page };
  }
  async function run({ browser, page }) {
    await page.goto(url);
    await page.evaluate((options2) => globalThis.MatterTest.auto(options2), options);
    await browser.close();
  }
  await new Promise((resolve, reject) => {
    setup().then((cx) => {
      cx.page.on("console", consoleHandler(reporter));
      cx.page.on("pageerror", (error) => reject(error));
      return cx;
    }).then(run).then(resolve).catch(reject);
  });
}
function consoleHandler(reporter) {
  return (message) => {
    const type = message.type();
    switch (type) {
      case "log":
      case "debug":
      case "info":
      case "error":
      case "warning":
      case "trace":
        break;
      default:
        return;
    }
    const text = message.text();
    if (type !== "log" || !text.startsWith(import_web_reporter.WebReporter.FLAG)) {
      console[type === "warning" ? "warn" : type](text);
      return;
    }
    const args = JSON.parse(text.slice(import_web_reporter.WebReporter.FLAG.length));
    const method = reporter[args[0]];
    if (typeof method !== "function") {
      throw new Error(`Invalid encoded reporter update method ${args[0]}`);
    }
    method.call(reporter, ...args.slice(1));
  };
}
function buildIndex(bundlePath) {
  return `<!DOCTYPE html>
<html>
<head>
    <title>Matter.js Web Testing</title>
    <link rel="stylesheet" href="mocha/mocha.css">
</head>
<body>
    <div id="mocha"><h1><a href="javascript:MatterTest.start()">run tests</a></h1></div>
    <script src="mocha/mocha.js"><\/script>
    <script src="${bundlePath}" type="module"><\/script>
</body>
</html>`;
}
async function bundle(files, { pkg, entrypointPreprocessor }) {
  const entrypointPath = pkg.resolve("build/esm/test-entrypoint.js");
  const bundlePath = pkg.resolve("build/cjs/test-bundle.js");
  const entrypoint = files.map((path) => {
    path = (0, import_node_path.relative)(pkg.resolve("build/esm"), path).replace(/\\/g, "/");
    if (!path.startsWith(".")) {
      path = `./${path}`;
    }
    return `import ${JSON.stringify(path)}`;
  });
  entrypointPreprocessor?.(entrypoint);
  await (0, import_promises.writeFile)(pkg.resolve("build/esm/test-entrypoint.js"), entrypoint.join("\n"));
  await (0, import_esbuild.build)({
    entryPoints: [entrypointPath],
    bundle: true,
    format: "cjs",
    outfile: bundlePath,
    external: ["wtfnode"],
    keepNames: true,
    conditions: ["static-load"],
    // This doesn't work...
    // logOverride: {
    //     "direct-eval": "silent",
    // },
    // ...so we do this:
    logLevel: "error"
  });
  return pkg.workspace.relative(bundlePath);
}
//# sourceMappingURL=web.js.map
