import * as THREE from 'three'

function OpenFileModel_ObjectFocus(object, domElement, editor) {
  this.center = new THREE.Vector3()

  const delta = new THREE.Vector3()
  const box = new THREE.Box3()

  const center = this.center
  const sphere = new THREE.Sphere()

  this.focus = function(target) {
    let distance

    box.setFromObject(target)

    if (box.isEmpty() === false) {
      box.getCenter(center)
      distance = box.getBoundingSphere(sphere).radius
    } else {
      center.setFromMatrixPosition(target.matrixWorld)
      distance = 0.1
    }

    delta.set(0, 0, 1)
    delta.applyQuaternion(object.quaternion)
    delta.multiplyScalar(distance * 4)

    object.position.copy(center).add(delta)

    editor.cameraControls.target = center
  }
}

export { OpenFileModel_ObjectFocus }
