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 overlays_exports = {};
__export(overlays_exports, {
  attachBoxelOverlays: () => attachBoxelOverlays
});
module.exports = __toCommonJS(overlays_exports);
function createOriginHelper(THREE, options) {
  const axes = new THREE.AxesHelper(0.5);
  if (options.opacity !== void 0) {
    const axesMaterial = axes.material;
    axesMaterial.opacity = options.opacity;
    axesMaterial.transparent = options.opacity < 1;
  }
  return axes;
}
function createBoundsHelper(THREE, size, options) {
  const geometry = new THREE.BoxGeometry(size[0], size[1], size[2]);
  const material = new THREE.LineDashedMaterial({
    color: options.color ?? 16777215,
    dashSize: options.dashed ? 0.1 : 1,
    gapSize: options.dashed ? 0.05 : 0,
    transparent: options.opacity !== void 0,
    opacity: options.opacity ?? 1
  });
  const edges = new THREE.EdgesGeometry(geometry);
  const line = new THREE.LineSegments(edges, material);
  line.computeLineDistances();
  return line;
}
function createSocketHelper(THREE, options) {
  const geometry = new THREE.SphereGeometry(0.05, 8, 8);
  const material = new THREE.MeshBasicMaterial({
    color: options.color ?? 16776960,
    transparent: options.opacity !== void 0,
    opacity: options.opacity ?? 1,
    wireframe: true
  });
  return new THREE.Mesh(geometry, material);
}
function disposeObjectResources(object) {
  object.traverse((entry) => {
    const mesh = entry;
    if ("geometry" in mesh && mesh.geometry) {
      const geometry = mesh.geometry;
      geometry?.dispose?.();
    }
    if ("material" in mesh && mesh.material) {
      const material = mesh.material;
      const disposeMaterial = (mat) => {
        const typedMaterial = mat;
        typedMaterial.map?.dispose?.();
        typedMaterial.emissiveMap?.dispose?.();
        typedMaterial.dispose?.();
      };
      if (Array.isArray(material))
        material.forEach(disposeMaterial);
      else
        disposeMaterial(material);
    }
  });
}
function attachBoxelOverlays(context, options = {}) {
  const { THREE, target } = context;
  const overlays = {
    dispose: () => {
      if (overlays.origin) {
        target.remove(overlays.origin);
        disposeObjectResources(overlays.origin);
      }
      overlays.bounds?.forEach((bound) => {
        target.remove(bound);
        disposeObjectResources(bound);
      });
      overlays.sockets?.forEach((socket) => {
        target.remove(socket);
        disposeObjectResources(socket);
      });
      overlays.origin = void 0;
      overlays.bounds = void 0;
      overlays.sockets = void 0;
    }
  };
  if (options.origin) {
    const origin = createOriginHelper(THREE, options);
    overlays.origin = origin;
    target.add(origin);
  }
  if (options.bounds && target.userData?.size) {
    const bound = createBoundsHelper(THREE, target.userData.size, options);
    overlays.bounds = [bound];
    target.add(bound);
  }
  if (options.sockets && target.userData?.sockets) {
    const sockets = target.userData.sockets.map((position) => {
      const helper = createSocketHelper(THREE, options);
      helper.position.set(position[0], position[1], position[2]);
      target.add(helper);
      return helper;
    });
    overlays.sockets = sockets;
  }
  return overlays;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  attachBoxelOverlays
});
