var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var nodes_exports = {};
__export(nodes_exports, {
  buildNodeObject: () => buildNodeObject
});
module.exports = __toCommonJS(nodes_exports);
var import_builders = require("./builders.js");
function applyNodeTransforms(context, object, node) {
  object.position.set(node.position[0], node.position[1], node.position[2]);
  if (node.rotation) {
    object.rotation.set(context.THREE.MathUtils.degToRad(node.rotation[0]), context.THREE.MathUtils.degToRad(node.rotation[1]), context.THREE.MathUtils.degToRad(node.rotation[2]));
  }
  if (node.scale) {
    object.scale.set(node.scale[0], node.scale[1], node.scale[2]);
  }
}
function buildNodeObject(context, node, visitedPrimitives = /* @__PURE__ */ new Set()) {
  const connectMode = node.connect ?? "none";
  const object = new context.THREE.Object3D();
  object.name = node.id;
  object.userData = {
    ...object.userData ?? {},
    kind: "entity-node",
    nodeId: node.id,
    primitiveId: node.mesh ?? (object.userData?.primitiveId ?? void 0),
    connectMode
  };
  if (node.size) {
    object.userData.size = [...node.size];
  }
  if (node.center) {
    object.userData.center = [...node.center];
  }
  applyNodeTransforms(context, object, node);
  const nodes = /* @__PURE__ */ new Map();
  nodes.set(node.id, object);
  if (node.mesh) {
    const primitive = context.primitivesById.get(node.mesh);
    if (primitive) {
      const mesh = (0, import_builders.createPrimitiveObject)(context, primitive, "centered", visitedPrimitives);
      if (mesh) {
        if (node.center) {
          mesh.position.set(node.center[0], node.center[1], node.center[2]);
        }
        object.add(mesh);
        if (mesh.userData?.size && !object.userData.size) {
          object.userData.size = mesh.userData.size;
        }
        if (mesh.userData?.sockets) {
          object.userData.sockets = mesh.userData.sockets;
        }
      }
    }
  } else if (node.size) {
    const mesh = (0, import_builders.createBoundingBoxMesh)(context, node.size);
    mesh.name = `${node.id}__placeholder`;
    if (node.center) {
      mesh.position.set(node.center[0], node.center[1], node.center[2]);
    }
    object.add(mesh);
  }
  node.children.forEach((child) => {
    const childResult = buildNodeObject(context, child, new Set(visitedPrimitives));
    object.add(childResult.object);
    childResult.nodes.forEach((value, key) => nodes.set(key, value));
  });
  return { object, nodes };
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  buildNodeObject
});
