import { Vector3 } from "@vived/core";
import { MoveTransformUCImp, } from "../../Transform3D";
import { StickableEntity } from "../Entities/Stickable";
import { TryPickStickTargetUC } from "./TryPickStickTarget";
export function makeMoveTransformWithStickUC(appObject) {
    return new MoveTransformWithStickUC(appObject);
}
class MoveTransformWithStickUC extends MoveTransformUCImp {
    constructor() {
        super(...arguments);
        this.isFirstPeel = true;
        this.initialScale = Vector3.Zero();
    }
    get stickable() {
        return this.getCachedLocalComponent(StickableEntity.type);
    }
    get pickTargetsUC() {
        return this.getCachedLocalComponent(TryPickStickTargetUC.type);
    }
    initialize(helper) {
        if (!this.transform)
            return;
        super.initialize(helper);
        this.initialScale = this.transform.localScale;
        this.isFirstPeel = true;
    }
    update(helper, ray) {
        super.update(helper, ray);
        if (!this.pickTargetsUC || !this.stickable) {
            return;
        }
        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;
        }
    }
    finish() {
        super.finish();
        if (!this.stickable) {
            return;
        }
        const doStick = this.stickable.highlightTarget;
        this.stickable.showStickTarget = false;
        this.stickable.highlightTarget = false;
        if (doStick) {
            this.finishWithStick();
        }
        else {
            this.submitDispatchAndUndo();
        }
    }
    finishWithStick() {
        const dispatch = this.dispatchStateChange;
        const transform = this.transform;
        if (!transform || !dispatch || !this.stickable)
            return;
        const startPos = this.initialPos;
        const finalPos = this.stickable.stickPosition;
        const startRot = this.initialRotation;
        const finalRot = this.stickable.stickRotation;
        const startScale = this.initialScale;
        const finalScale = new 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();
                },
            });
        }
    }
    cancel() {
        super.cancel();
        if (this.stickable) {
            this.stickable.showStickTarget = false;
            this.stickable.highlightTarget = false;
        }
    }
}
//# sourceMappingURL=MoveTransformWithStickUC.js.map