"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PanSceneRootUC = void 0;
exports.makePanSceneRootUC = makePanSceneRootUC;
const core_1 = require("@vived/core");
const core_2 = require("@vived/core");
const Babylon_1 = require("../../Babylon");
const Dispatcher_1 = require("../../Dispatcher");
const SceneRootEntity_1 = require("../Entities/SceneRootEntity");
class PanSceneRootUC extends core_1.AppObjectUC {
}
exports.PanSceneRootUC = PanSceneRootUC;
PanSceneRootUC.type = "PanSceneRootUC";
PanSceneRootUC.get = (appObjects) => (0, core_1.getSingletonComponent)(PanSceneRootUC.type, appObjects);
function makePanSceneRootUC(appObject) {
    return new PanSceneRootImp(appObject);
}
class PanSceneRootImp extends PanSceneRootUC {
    constructor(appObject) {
        super(appObject, PanSceneRootUC.type);
        this.pickOffset = core_2.Vector3.Zero();
        this.initialPosition = core_2.Vector3.Zero();
        this.hasBeenInitialized = false;
        this.panPlane = core_2.ParametricPlane.XY();
        this.doPan = (pixelPosition) => {
            if (!this.transformNode)
                return;
            if (!this.hasBeenInitialized)
                return;
            const pickRay = (0, Babylon_1.pixelToSceneRay)(pixelPosition, this.appObjects);
            const pick = this.panPlane.intersectLine(pickRay) ?? core_2.Vector3.Zero();
            this.transformNode.worldPosition = core_2.Vector3.Add(pick, this.pickOffset);
        };
        this.initializePan = (initialPixelPos) => {
            if (!this.transformNode)
                return;
            if (!this.sceneRoot)
                return;
            const scenePick = (0, Babylon_1.scenePixelPick)(initialPixelPos, this.appObjects);
            if (!scenePick)
                return;
            const pickPlaneDirection = core_2.Vector3.Backward();
            const planePickPos = scenePick?.worldPickPoint ?? core_2.Vector3.Zero();
            this.panPlane = core_2.ParametricPlane.FromPointNormal(planePickPos, pickPlaneDirection);
            const pick = this.panPlane.intersectLine(scenePick.sceneRay) ?? core_2.Vector3.Zero();
            const rootPos = this.transformNode.worldPosition;
            this.pickOffset = core_2.Vector3.Subtract(rootPos, pick);
            this.hasBeenInitialized = true;
            this.initialPosition = this.sceneRoot.position;
        };
        this.finishPan = () => {
            if (!this.hasBeenInitialized)
                return;
            if (!this.sceneRoot)
                return;
            this.hasBeenInitialized = false;
            if (core_2.Vector3.Equal(this.initialPosition, this.sceneRoot.position))
                return;
            (0, Dispatcher_1.dispatchStateChange)(this.appObjects);
        };
        this.cancelPan = () => {
            this.hasBeenInitialized = false;
        };
        this.sceneRoot = appObject.getComponent(SceneRootEntity_1.SceneRootEntity.type);
        if (!this.sceneRoot) {
            this.warn("OrbitSceneRootImp UC has been added to an App Object that does not have a SceneRoot Entity");
            return;
        }
        this.transformNode = this.sceneRoot?.transform;
    }
}
//# sourceMappingURL=PanSceneRoot.js.map