import { AppObjectUC } from "@vived/core";
import { Vector3 } from "@vived/core";
import { pixelToSceneRay, sceneRayPick } from "../../Babylon";
import { dispatchStateChange } from "../../Dispatcher";
import { Transform3D } from "../Entities";
export class ScaleTransformUC extends AppObjectUC {
}
ScaleTransformUC.type = "ScaleTransformUC";
export function makeScaleTransformUC(appObject) {
    return new ScaleTransformUCImp(appObject);
}
class ScaleTransformUCImp extends ScaleTransformUC {
    doInitialize(ray) {
        this.isScaling = false;
        if (!this.transform)
            return;
        if (this.pickPlane) {
            this.pickPoint = this.pickPlane.intersectLine(ray) ?? Vector3.Zero();
        }
        else {
            this.pickPoint =
                sceneRayPick(ray, this.appObjects)?.worldPickPoint ?? Vector3.Zero();
        }
        this.initialScale = this.transform.worldScale.x;
        this.isScaling = true;
    }
    constructor(appObject) {
        super(appObject, ScaleTransformUC.type);
        this.raySensitivity = 1;
        this.pixelSensitivity = 0.1;
        this.enabled = true;
        this.lastPosition = 0;
        this.pickPoint = Vector3.Zero();
        this.initialScale = 1;
        this.isScaling = false;
        this.initializeRay = (ray) => {
            this.lastPosition = ray.origin.z;
            this.doInitialize(ray);
        };
        this.updateRay = (ray) => {
            if (!this.isScaling)
                return;
            if (!this.transform)
                return;
            if (!this.enabled)
                return;
            const movement = this.lastPosition - ray.origin.z;
            const currentScale = this.transform.worldScale.x;
            const scaleDelta = movement * this.raySensitivity * currentScale;
            this.bumpScaleAboutPoint(scaleDelta, this.pickPoint);
            this.lastPosition = ray.origin.z;
        };
        this.initializePixel = (initialPixelPos) => {
            this.lastPosition = initialPixelPos.y;
            const ray = pixelToSceneRay(initialPixelPos, this.appObjects);
            this.doInitialize(ray);
        };
        this.updatePixel = (pixelPosition) => {
            if (!this.enabled)
                return;
            if (!this.transform)
                return;
            if (!this.isScaling)
                return;
            const pixelChange = pixelPosition.y - this.lastPosition;
            this.lastPosition = pixelPosition.y;
            const currentScale = this.transform.worldScale.x;
            const scaleDelta = pixelChange * this.pixelSensitivity * currentScale;
            this.bumpScaleAboutPoint(scaleDelta, this.pickPoint);
        };
        this.finish = () => {
            if (!this.transform)
                return;
            if (!this.isScaling)
                return;
            if (this.initialScale !== this.transform.worldScale.x) {
                dispatchStateChange(this.appObjects);
            }
            this.isScaling = false;
        };
        this.bumpScaleAboutPoint = (amount, worldPoint) => {
            if (!this.transform)
                return;
            const localPickOffset = this.transform.worldPointToLocal(worldPoint);
            const currentScale = this.transform.worldScale.x;
            let newScale = currentScale + amount;
            if (this.minScale !== undefined && newScale < this.minScale) {
                newScale = this.minScale;
            }
            if (this.maxScale !== undefined && newScale > this.maxScale) {
                newScale = this.maxScale;
            }
            this.transform.worldScale = new Vector3(newScale, newScale, newScale);
            const worldPickOffset = this.transform.localPointToWorld(localPickOffset);
            const bump = Vector3.Subtract(worldPoint, worldPickOffset);
            this.transform.worldPosition = Vector3.Add(bump, this.transform.worldPosition);
        };
        this.transform = appObject.getComponent(Transform3D.type);
        if (!this.transform) {
            this.error("App Object does not have a Transform3D Entity");
        }
    }
}
//# sourceMappingURL=ScaleTransform.js.map