import * as THREE from '../node_modules/three/build/three.module.js';
import { checkBufferGeometryIntersection, uvIntersection } from './IntersectionUtilities.js';

const abcFields = [ 'a', 'b', 'c' ];
const xyzFields = [ 'x', 'y', 'z' ];

const getLongestEdgeIndex = ( bb ) => {

	let splitDimIdx = - 1;
	let splitDist = - Infinity;
	for ( let i = 0; i < xyzFields.length; i++ ) {

		const d = xyzFields[ i ];
		const dist = bb.max[ d ] - bb.min[ d ];
		if ( dist > splitDist ) {

			splitDist = dist;
			splitDimIdx = i;

		}

	}

	return splitDimIdx;

};

// For BVH code specifically. Does not check morph targets
// Copied from mesh raycasting
// Ripped an modified from the THREE.js source in Mesh.CS
const intersectionPoint = new THREE.Vector3();
const intersectTri = ( mesh, geo, raycaster, ray, tri, intersections ) => {

	const triOffset = tri * 3;
	const a = geo.index ? geo.index.getX( triOffset ) : triOffset;
	const b = geo.index ? geo.index.getX( triOffset + 1 ) : triOffset + 1;
	const c = geo.index ? geo.index.getX( triOffset + 2 ) : triOffset + 2;

	const intersection = checkBufferGeometryIntersection( mesh, raycaster, ray, geo.attributes.position, geo.attributes.uv, a, b, c );

	if ( intersection ) {

		intersection.faceIndex = tri;
		if ( intersections ) intersections.push( intersection );
		return intersection;

	}

	return null;

};

const intersectTris = ( mesh, geo, raycaster, ray, offset, count, intersections ) => {

	for ( let i = offset; i < offset + count; i ++ ) {

		intersectTri( mesh, geo, raycaster, ray, i, intersections );

	}

};

const intersectClosestTri = ( mesh, geo, raycaster, ray, offset, count ) => {

	let dist = Infinity;
	let res = null;
	for ( let i = offset; i < offset + count; i ++ ) {

		const intersection = intersectTri( mesh, geo, raycaster, ray, i );
		if ( intersection && intersection.distance < dist ) {

			res = intersection;
			dist = intersection.distance;

		}

	}

	return res;

};

export {
	getLongestEdgeIndex, intersectTri, intersectTris, intersectClosestTri
};

@@ -1,74 +1,4 @@
-import * as THREE from '../node_modules/three/build/three.module.js';%0A
 impo
@@ -37,24 +37,8 @@
 tion
-, uvIntersection
  %7D f
@@ -76,45 +76,8 @@
 ';%0A%0A
-const abcFields = %5B 'a', 'b', 'c' %5D;%0A
 cons
@@ -581,55 +581,8 @@
 .CS%0A
-const intersectionPoint = new THREE.Vector3();%0A
 cons

Kill unused code