import { Quaternion, Vector3 } from "../../math/index.js";
import {} from "../../scene/index.js";
import {} from "./ControlTypes.js";
export function createSceneCameraControlAdapter(camera) {
    const euler = quaternionToEuler(camera.transform.rotation);
    const position = {
        get x() {
            return camera.transform.position[0];
        },
        set x(value) {
            camera.transform.setPosition(value, camera.transform.position[1], camera.transform.position[2]);
            syncCamera(camera);
        },
        get y() {
            return camera.transform.position[1];
        },
        set y(value) {
            camera.transform.setPosition(camera.transform.position[0], value, camera.transform.position[2]);
            syncCamera(camera);
        },
        get z() {
            return camera.transform.position[2];
        },
        set z(value) {
            camera.transform.setPosition(camera.transform.position[0], camera.transform.position[1], value);
            syncCamera(camera);
        }
    };
    const rotation = {
        get x() {
            return euler.x;
        },
        set x(value) {
            euler.x = value;
            applyEuler(camera, euler);
        },
        get y() {
            return euler.y;
        },
        set y(value) {
            euler.y = value;
            applyEuler(camera, euler);
        },
        get z() {
            return euler.z;
        },
        set z(value) {
            euler.z = value;
            applyEuler(camera, euler);
        }
    };
    return {
        camera,
        position,
        rotation,
        lookAt(target) {
            const origin = new Vector3(camera.transform.position[0], camera.transform.position[1], camera.transform.position[2]);
            const direction = new Vector3(target.x, target.y, target.z).subtract(origin).normalize();
            if (direction.lengthSquared() === 0)
                return;
            const q = Quaternion.fromUnitVectors(Vector3.forward, direction);
            camera.transform.setRotation(q.x, q.y, q.z, q.w);
            const nextEuler = quaternionToEuler([q.x, q.y, q.z, q.w]);
            euler.x = nextEuler.x;
            euler.y = nextEuler.y;
            euler.z = nextEuler.z;
            syncCamera(camera);
        }
    };
}
function applyEuler(camera, euler) {
    const yaw = Quaternion.fromAxisAngle(Vector3.up, euler.y);
    const pitch = Quaternion.fromAxisAngle(Vector3.right, euler.x);
    const roll = Quaternion.fromAxisAngle(new Vector3(0, 0, 1), euler.z);
    const q = yaw.multiply(pitch).multiply(roll).normalize();
    camera.transform.setRotation(q.x, q.y, q.z, q.w);
    syncCamera(camera);
}
function syncCamera(camera) {
    camera.updateCameraMatrices();
}
function quaternionToEuler(rotation) {
    const [x, y, z, w] = rotation;
    const sinPitch = 2 * (w * x - z * y);
    const pitch = Math.abs(sinPitch) >= 1 ? Math.sign(sinPitch) * Math.PI / 2 : Math.asin(sinPitch);
    const yaw = Math.atan2(2 * (w * y + x * z), 1 - 2 * (x * x + y * y));
    const roll = Math.atan2(2 * (w * z + x * y), 1 - 2 * (x * x + z * z));
    return { x: pitch, y: yaw, z: roll };
}
//# sourceMappingURL=SceneCameraAdapter.js.map