import { AppObjectUC } from "@vived/core";
import { Vector3 } from "@vived/core";
import { pixelToSceneRay, scenePixelPick } from "../../Babylon";
import { dispatchStateChange } from "../../Dispatcher";
import { Transform3D } from "../Entities";
export class BumpTransformScaleAboutPixelPointUC extends AppObjectUC {
}
BumpTransformScaleAboutPixelPointUC.type = "BumpTransformScaleAboutPixelPointUC";
export function makeBumpTransformScaleAboutPixelPointUC(appObject) {
    return new BumpTransformScaleAboutPixelPointUCImp(appObject);
}
class BumpTransformScaleAboutPixelPointUCImp extends BumpTransformScaleAboutPixelPointUC {
    constructor(appObject) {
        super(appObject, BumpTransformScaleAboutPixelPointUC.type);
        this.enabled = true;
        this.sensitivity = 0.1;
        this.allow = () => {
            return true;
        };
        this.bump = (direction, pixelPosition) => {
            if (!this.enabled)
                return;
            if (!this.transform)
                return;
            if (!this.allow())
                return;
            let bumpDirection = -1;
            if (direction === "IN") {
                bumpDirection = 1;
            }
            const currentScale = this.transform.worldScale.x;
            const scaleDelta = bumpDirection * this.sensitivity * currentScale;
            let pickPoint = Vector3.Zero();
            if (this.pickPlane) {
                const pickRay = pixelToSceneRay(pixelPosition, this.appObjects);
                pickPoint = this.pickPlane.intersectLine(pickRay) ?? Vector3.Zero();
            }
            else {
                pickPoint =
                    scenePixelPick(pixelPosition, this.appObjects)?.worldPickPoint ??
                        Vector3.Zero();
            }
            this.bumpScaleAboutPoint(scaleDelta, pickPoint);
            dispatchStateChange(this.appObjects);
        };
        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("BumpSceneRootScaleAboutPixelPointImp UC has been added to an App Object that does not have a Transform");
            return;
        }
    }
}
//# sourceMappingURL=BumpTransformScaleAboutPixelPoint.js.map