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 __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 import_node_test = __toESM(require("node:test"));
var import_strict = __toESM(require("node:assert/strict"));
var import_node_path = __toESM(require("node:path"));
var import_node_url = require("node:url");
const import_meta = {};
const HERE = import_node_path.default.dirname((0, import_node_url.fileURLToPath)(import_meta.url));
const WORKER_ENTRY = import_node_path.default.resolve(HERE, "fixtures/render-worker.js");
async function bundleEntityWorker() {
  const { build } = await import("esbuild");
  const result = await build({
    entryPoints: [WORKER_ENTRY],
    bundle: true,
    platform: "browser",
    format: "esm",
    target: "es2022",
    write: false,
    sourcemap: false,
    logLevel: "silent"
  });
  const output = result.outputFiles?.[0]?.text;
  if (!output) {
    throw new Error("failed_to_bundle_entity_worker");
  }
  return output;
}
function createPlaywrightLaunchError(tool, error) {
  const message = error && typeof error === "object" && "message" in error ? String(error.message) : String(error);
  if (/browserType\.launch/i.test(message) || /MachPortRendezvousServer/i.test(message)) {
    return new Error([
      `[${tool}] Failed to launch Playwright Chromium.`,
      "",
      "Required setup:",
      "  1. npx playwright install chromium",
      '  2. On macOS: xattr -dr com.apple.quarantine "$HOME/Library/Caches/ms-playwright"',
      "",
      `Original error: ${message}`
    ].join("\n"));
  }
  return error instanceof Error ? error : new Error(message);
}
(0, import_node_test.default)("renderBoxelToDataURL produces a PNG data URL", async (t) => {
  const { chromium } = await import("playwright-core");
  const bundle = await bundleEntityWorker();
  let browser = null;
  try {
    browser = await chromium.launch({ headless: true, args: ["--no-sandbox"], chromiumSandbox: false });
  } catch (error) {
    const launchError = createPlaywrightLaunchError("boxel-three-test", error);
    t.skip(launchError.message);
    return;
  }
  const page = await browser.newPage({ viewport: { width: 512, height: 512 } });
  const consoleErrors = [];
  page.on("console", (message) => {
    if (message.type() === "error") {
      consoleErrors.push(message.text());
    }
  });
  page.on("pageerror", (error) => {
    consoleErrors.push(error?.message ?? String(error));
  });
  try {
    await page.setContent('<canvas id="viewport"></canvas>', { waitUntil: "domcontentloaded" });
    await page.addScriptTag({ type: "module", content: bundle });
    await page.waitForFunction(() => typeof window.boxelExport !== "undefined");
    const minimalEntity = {
      id: "render-test",
      geometry: {
        id: "geom",
        root: {
          id: "Root",
          position: [0, 0, 0],
          children: [
            {
              id: "Node",
              position: [0, 0.5, 0],
              mesh: "box",
              children: []
            }
          ]
        }
      },
      primitives: [
        {
          id: "box",
          kind: "plain_box",
          size: [1, 1, 1],
          material: "mat"
        }
      ],
      materials: [{ id: "mat", baseColor: [200, 120, 60] }],
      textures: [],
      patterns: [],
      animations: []
    };
    const dataUrl = await page.evaluate((model) => window.boxelExport.renderBoxel(model, {
      mode: "hierarchical",
      width: 512,
      height: 512,
      direction: "preview",
      animationIds: null
    }), minimalEntity);
    import_strict.default.strictEqual(typeof dataUrl, "string");
    import_strict.default.ok(dataUrl.startsWith("data:image/png;base64,"), "Render output is not a PNG data URL");
    import_strict.default.ok(dataUrl.length > "data:image/png;base64,".length, "Render output is empty");
    if (consoleErrors.length > 0) {
      throw new Error(consoleErrors.join("\n"));
    }
  } finally {
    await page.close();
    if (browser) {
      await browser.close();
    }
  }
});
