import * as THREE from 'three';

interface SetCameraViewProps {
    controls: any;
    camera: THREE.Camera;
    position: THREE.Vector3 | { x: number; y: number; z: number };
    rotation: THREE.Euler | { x: number; y: number; z: number };
    username?: string;
    target?: THREE.Vector3 | { x: number; y: number; z: number };
}

export default async function setCameraView({
    controls,
    camera,
    position,
    rotation,
    username,
    target
}: SetCameraViewProps) {
    if (!controls || !camera) return;

    if (target == null) return;

    // Normalize position
    const newPosition = position instanceof THREE.Vector3
        ? position
        : new THREE.Vector3(position.x, position.y, position.z);

    const newTarget = target instanceof THREE.Vector3 ? target : new THREE.Vector3(target.x, target.y, target.z);

    if (controls.setTarget) {
        controls?.setLookAt(...newPosition.toArray(), ...newTarget.toArray(), true);
    }
}
