import { Constants } from "../Constants";
import { ThreeUtils } from ".";
export class GetUtils {
    static getFileExtension(file) {
        return file.substring(file.lastIndexOf(".") + 1);
    }
    static getGeometryCenter(geometry) {
        let geom;
        if (geometry instanceof THREE.BufferGeometry) {
            geom = new THREE.Geometry().fromBufferGeometry(geometry);
        }
        else {
            geom = geometry;
        }
        geom.computeBoundingSphere();
        return geom.boundingSphere.center;
    }
    static getCameraStateFromMesh(entityMesh) {
        let sceneCenter;
        let initialPosition;
        let sceneDistance;
        if (entityMesh) {
            const geom = entityMesh.geometry;
            if (!geom.boundingSphere) {
                geom.computeBoundingSphere();
            }
            sceneCenter = entityMesh.position;
            sceneDistance =
                (Constants.zoomFactor * geom.boundingSphere.radius) /
                    Math.tan((Constants.cameraValues.fov * Math.PI) / 180);
            initialPosition = new THREE.Vector3();
            initialPosition.copy(sceneCenter);
            initialPosition.z += sceneDistance;
            return {
                target: sceneCenter,
                position: initialPosition
            };
        }
        return null;
    }
    static getCameraStateFromNode(node, radius) {
        if (!node) {
            return null;
        }
        let targ = new THREE.Vector3();
        targ.copy(ThreeUtils.stringToVector3(node.target));
        let pos = new THREE.Vector3();
        pos.copy(ThreeUtils.stringToVector3(node.position));
        let dir = pos
            .clone()
            .sub(targ.clone())
            .normalize();
        let camPos = new THREE.Vector3();
        camPos.copy(pos);
        camPos.add(dir.clone().multiplyScalar(radius * Constants.zoomFactor));
        return {
            target: targ,
            position: camPos
        };
    }
}
