import { Object3D, Vector3 } from "three";

import { serializable } from "../engine/engine_serialization_decorator.js";
import { Behaviour } from "./Component.js";
import type { OrbitControls } from "./OrbitControls.js";

/**
 * A LookAtConstraint is used by OrbitControls to make the camera look at a target.  
 * This component is used by {@link OrbitControls} internally.
 * 
 * @summary Look At Constraint for OrbitControls
 * @category Camera Controls
 * @group Components
 */
export class LookAtConstraint extends Behaviour {

    /**
     * When true the constraint is active.
     */
    @serializable()
    constraintActive: boolean = true;
    /**
     * When true the look at is locked to the position of the assigned sources.
     */
    @serializable()
    locked: boolean = false;
    /**
     * The sources to look at.
     */
    @serializable(Object3D)
    sources: Object3D[] = [];

    /**
     * Set the position of the constraint.
     */
    setConstraintPosition(worldPosition: Vector3) {
        const source = this.sources[0];
        if (source) source.worldPosition = worldPosition;
    }
}