"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TryMoveTransformUCImp = exports.TryMoveTransformUC = void 0;
exports.makeTryMoveTransformUC = makeTryMoveTransformUC;
const core_1 = require("@vived/core");
const Babylon_1 = require("../../Babylon");
const Stylus_1 = require("../../Stylus");
const Entities_1 = require("../Entities");
const MoveTransformUC_1 = require("./MoveTransformUC");
class TryMoveTransformUC extends core_1.AppObjectUC {
}
exports.TryMoveTransformUC = TryMoveTransformUC;
TryMoveTransformUC.type = "TryMoveTransformUC";
function makeTryMoveTransformUC(appObject) {
    return new TryMoveTransformUCImp(appObject);
}
class TryMoveTransformUCImp extends TryMoveTransformUC {
    get rayPickUC() {
        return this.getCachedSingleton(Babylon_1.SceneRayPickUC.type);
    }
    constructor(appObject) {
        super(appObject, TryMoveTransformUC.type);
        this.allow = () => {
            return true;
        };
        this.tryWithRay = (pickRay) => {
            this.moveUC = undefined;
            if (!this.allow())
                return false;
            const appObjectID = this.rayPickUC?.sceneRayPick(pickRay)?.appObjectID;
            if (!appObjectID)
                return false;
            this.moveUC = MoveTransformUC_1.MoveTransformUC.get(appObjectID, this.appObjects);
            if (this.moveUC === undefined) {
                return false;
            }
            this.moveUC.initialize(this.helperTransform);
            return true;
        };
        this.moveWithRay = (pickRay) => {
            if (!this.moveUC)
                return;
            this.moveUC.update(this.helperTransform, pickRay);
        };
        this.stop = () => {
            if (!this.moveUC)
                return;
            this.moveUC.finish();
            this.moveUC = undefined;
        };
        this.cancel = () => {
            if (!this.moveUC)
                return;
            this.moveUC.cancel();
            this.moveUC = undefined;
        };
        const helperAO = this.appObjects.getOrCreate(this.appObject.id + "_helper");
        this.helperTransform = Entities_1.Transform3D.addIfMissing(helperAO);
        const stylus = Stylus_1.StylusEntity.get(this.appObjects);
        if (!stylus) {
            this.error("Unable to find stylus");
        }
        else {
            this.helperTransform.parent = stylus.transform;
        }
    }
}
exports.TryMoveTransformUCImp = TryMoveTransformUCImp;
//# sourceMappingURL=TryMoveTransformUC.js.map