import { Camera } from "./Camera.js";

/**
 *
 * @param {Camera} camera
 * @param {number} width
 * @param {number} height
 */
export function set_camera_aspect_ratio(camera, width, height) {
    const c = camera.object;

    if (c === null) {
        return;
    }

    const aspect = width / height;

    switch (camera.projectionType.getValue()) {
        case Camera.ProjectionType.Perspective:
            c.aspect = aspect;
            break;
        case Camera.ProjectionType.Orthographic:
            //use smaller values to get a bit of a "zoom" on the world
            const w = c.right - c.left;
            const h = c.bottom - c.top;

            const existingAspect = w / h;

            const aspectDelta = aspect - existingAspect;

            if (aspectDelta > 0) {
                const d = h * aspectDelta;
                c.left -= d / 2;
                c.right += d / 2;
            } else if (aspectDelta < 0) {
                const d = -h * aspectDelta;

                c.top += d / 2;
                c.bottom -= d / 2;
            }

            break;
    }

    c.updateProjectionMatrix();
}
