import { Object3D, Vector3 } from "three";

import { serializable } from "../engine/engine_serialization.js";
import { Behaviour } from "./Component.js";

/**
 * @deprecated LookAtConstraint will be removed in future versions. Please either use a direct object reference instead (e.g. assigning an Object3D as a look target in scripts that reply on the LookAtConstraint) or implement the LookAtConstraint signature in your web project:   
 * ```ts
 * export class LookAtConstraint extends Behaviour {
 *   constraintActive: boolean = true;
 *   locked: boolean = false;
 *   sources: Object3D[] = [];
 *   setConstraintPosition(worldPosition: Vector3) {
 *     const source = this.sources[0];
 *     if (source) source.worldPosition = worldPosition;
 *   }
 * }
 * ```
 */
export class LookAtConstraint extends Behaviour {

    /**
     * When true, the constraint is active and affects the target.
     * Set to false to temporarily disable without removing sources.
     */
    @serializable()
    constraintActive: boolean = true;

    /**
     * When true, the look-at position is locked and won't update
     * even if source objects move.
     */
    @serializable()
    locked: boolean = false;

    /**
     * Objects to look at. The first object in the array is used
     * as the primary look-at target.
     */
    @serializable(Object3D)
    sources: Object3D[] = [];

    /**
     * Sets the world position that the constraint should look at.
     * Updates the first source object's position.
     * @param worldPosition The world-space position to look at
     */
    setConstraintPosition(worldPosition: Vector3) {
        const source = this.sources[0];
        if (source) source.worldPosition = worldPosition;
    }
}
