"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.makePanTransformWithStickUC = makePanTransformWithStickUC;
const core_1 = require("@vived/core");
const Transform3D_1 = require("../../Transform3D");
const Stickable_1 = require("../Entities/Stickable");
const TryPickStickTarget_1 = require("./TryPickStickTarget");
function makePanTransformWithStickUC(appObject) {
    return new PanTransformWithStickUC(appObject);
}
class PanTransformWithStickUC extends Transform3D_1.PanTransformUCImp {
    constructor() {
        super(...arguments);
        this.isFirstPeel = true;
        this.initialScale = core_1.Vector3.Zero();
        this.initialRot = core_1.Quaternion.Identity();
        this.initializeRay = (ray) => {
            if (!this.transformNode || !this.stickable || !this.pickTargetsUC) {
                this.error("Missing components");
                return;
            }
            const pick = this.panPlane.intersectLine(ray) ?? core_1.Vector3.Zero();
            const rootPos = this.transformNode.worldPosition;
            this.pickOffset = core_1.Vector3.Subtract(rootPos, pick);
            this.initialScale = this.transformNode.localScale;
            this.initialRot = this.transformNode.localRotation;
            this.isFirstPeel = true;
            this.hasBeenInitialized = true;
            this.initialPosition = this.transformNode.localPosition;
        };
        this.updateRay = (ray) => {
            if (!this.transformNode || !this.stickable || !this.pickTargetsUC) {
                return;
            }
            if (!this.hasBeenInitialized)
                return;
            if (!this.enabled)
                return;
            const pick = this.panPlane.intersectLine(ray) ?? core_1.Vector3.Zero();
            const wPos = core_1.Vector3.Add(pick, this.pickOffset);
            const clampedPos = this.enforceBoundaries(wPos);
            this.transformNode.worldPosition = clampedPos;
            const isHitStickTarget = this.pickTargetsUC.tryPick(ray);
            if (isHitStickTarget) {
                if (this.isFirstPeel) {
                    this.stickable.showStickTarget = false;
                    this.stickable.highlightTarget = false;
                }
                else {
                    this.stickable.showStickTarget = true;
                    this.stickable.highlightTarget = true;
                }
            }
            else {
                this.stickable.showStickTarget = true;
                this.stickable.highlightTarget = false;
                this.isFirstPeel = false;
            }
        };
        this.finish = () => {
            if (!this.stickable) {
                return;
            }
            if (!this.hasBeenInitialized)
                return;
            if (!this.enabled)
                return;
            const doStick = this.stickable.highlightTarget;
            this.hasBeenInitialized = false;
            this.stickable.showStickTarget = false;
            this.stickable.highlightTarget = false;
            if (doStick) {
                this.finishWithStick();
            }
            else {
                this.submitDispatchAndUndo();
            }
        };
        this.cancelPan = () => {
            this.hasBeenInitialized = false;
            if (this.stickable) {
                this.stickable.showStickTarget = false;
                this.stickable.highlightTarget = false;
            }
        };
    }
    get stickable() {
        return this.getCachedLocalComponent(Stickable_1.StickableEntity.type);
    }
    get pickTargetsUC() {
        return this.getCachedLocalComponent(TryPickStickTarget_1.TryPickStickTargetUC.type);
    }
    finishWithStick() {
        const dispatch = this.dispatchStateChange;
        const transform = this.transformNode;
        if (!transform || !dispatch || !this.stickable)
            return;
        const startPos = this.initialPosition;
        const finalPos = this.stickable.stickPosition;
        const startRot = this.initialRot;
        const finalRot = this.stickable.stickRotation;
        const startScale = this.initialScale;
        const finalScale = new core_1.Vector3(this.stickable.stickScale, this.stickable.stickScale, this.stickable.stickScale);
        if (this.dispatchStateChangeOnComplete) {
            this.stickable.doStick(dispatch);
        }
        else {
            this.stickable.doStick();
        }
        if (this.submitUndoOnComplete) {
            this.undoRedo?.submitAction({
                undoAction: () => {
                    transform.localPosition = startPos;
                    transform.localRotation = startRot;
                    transform.localScale = startScale;
                    dispatch();
                },
                redoAction: () => {
                    transform.localPosition = finalPos;
                    transform.localRotation = finalRot;
                    transform.localScale = finalScale;
                    dispatch();
                },
            });
        }
    }
}
//# sourceMappingURL=PanTransformWithStickUC.js.map