import { AppObjectPM, getSingletonComponent, } from "@vived/core";
import { Color, Vector3 } from "@vived/core";
import { SceneRayPickUC } from "../../Babylon";
import { StylusEntity } from "../Entity/StylusEntity";
export class StylusColorLengthPM extends AppObjectPM {
    static get(appObjects) {
        return getSingletonComponent(StylusColorLengthPM.type, appObjects);
    }
}
StylusColorLengthPM.type = "StylusColorLengthPM";
export function makeStylusColorLengthPM(appObject) {
    return new StylusColorLengthPMImp(appObject);
}
class StylusColorLengthPMImp extends StylusColorLengthPM {
    vmsAreEqual(a, b) {
        if (Color.Equal(a.color, b.color) && a.length === b.length) {
            return true;
        }
        return false;
    }
    setDefaultVM() {
        const vm = {
            color: this.rayColorNoPick,
            length: 0,
        };
        this.doUpdateView(vm);
    }
    constructor(appObject) {
        super(appObject, StylusColorLengthPM.type);
        this.defaultLength = 1;
        this.rayColorNoPick = Color.RGB(128, 128, 128);
        this.rayColorPick = Color.RGB(0, 255, 0);
        this.onEntityChange = () => {
            if (!this.stylus || !this.stylus.isInUse) {
                this.setDefaultVM();
                return;
            }
            const pickRay = this.stylus.pickRay;
            const scenePickUC = SceneRayPickUC.get(this.appObject.appObjectRepo);
            if (!scenePickUC) {
                this.setDefaultVM();
                return;
            }
            const scenePick = scenePickUC.sceneRayPick(pickRay);
            let color = this.rayColorNoPick;
            let length = this.defaultLength;
            if (scenePick && scenePick?.hit && scenePick.worldPickPoint) {
                color = this.rayColorPick;
                length = Math.abs(Vector3.Subtract(this.stylus.transform.worldPosition, scenePick.worldPickPoint).magnitude);
            }
            const vm = {
                color,
                length,
            };
            this.doUpdateView(vm);
        };
        this.dispose = () => {
            super.dispose();
            this.stylus?.removeChangeObserver(this.onEntityChange);
            this.stylus?.removeOnDisposeObserver(this.dispose);
        };
        this.stylus = appObject.getComponent(StylusEntity.type);
        if (this.stylus) {
            this.stylus.addChangeObserver(this.onEntityChange);
            this.stylus.addOnDisposeObserver(() => {
                this.dispose();
            });
            this.onEntityChange();
        }
        else {
            this.warn("App Object does not have a Stylus Entity");
        }
        this.appObjects.registerSingleton(this);
    }
}
//# sourceMappingURL=StylusColorLengthPM.js.map