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 THREE = __toESM(require("three"));
var import_GLTFExporter = require("three/examples/jsm/exporters/GLTFExporter.js");
var import_src = require("../../src/index.js");
var import_boxel_core = require("@playdrop/boxel-core");
function arrayBufferToBase64(buffer) {
  const bytes = new Uint8Array(buffer);
  const chunkSize = 32768;
  let binary = "";
  for (let index = 0; index < bytes.length; index += chunkSize) {
    const chunk = bytes.subarray(index, index + chunkSize);
    binary += String.fromCharCode(...chunk);
  }
  return btoa(binary);
}
let cachedRenderer = null;
function getRenderer() {
  if (cachedRenderer) {
    return cachedRenderer;
  }
  const canvas = document.getElementById("viewport");
  if (!canvas) {
    throw new Error("render_canvas_unavailable");
  }
  cachedRenderer = new THREE.WebGLRenderer({
    canvas,
    antialias: true,
    preserveDrawingBuffer: true,
    alpha: true
  });
  return cachedRenderer;
}
async function exportBoxel(modelJson, options) {
  const model = (0, import_boxel_core.parseBoxel)(modelJson);
  const blob = await (0, import_src.exportModelToGLB)({
    THREE,
    GLTFExporterCtor: import_GLTFExporter.GLTFExporter,
    model,
    mode: options?.mode,
    animationIds: options?.animationIds
  });
  const arrayBuffer = await blob.arrayBuffer();
  return arrayBufferToBase64(arrayBuffer);
}
async function renderBoxel(modelJson, options) {
  const model = (0, import_boxel_core.parseBoxel)(modelJson);
  const renderer = getRenderer();
  renderer.setPixelRatio(1);
  return (0, import_src.renderBoxelToDataURL)({
    THREE,
    renderer,
    entity: model,
    mode: options?.mode ?? "hierarchical",
    animationIds: options?.animationIds,
    width: options?.width,
    height: options?.height,
    direction: options?.direction,
    background: options?.background ?? null
  });
}
window.boxelExport = {
  exportBoxel,
  renderBoxel
};
