"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.StylusColorLengthPM = void 0;
exports.makeStylusColorLengthPM = makeStylusColorLengthPM;
const core_1 = require("@vived/core");
const core_2 = require("@vived/core");
const Babylon_1 = require("../../Babylon");
const StylusEntity_1 = require("../Entity/StylusEntity");
class StylusColorLengthPM extends core_1.AppObjectPM {
    static get(appObjects) {
        return (0, core_1.getSingletonComponent)(StylusColorLengthPM.type, appObjects);
    }
}
exports.StylusColorLengthPM = StylusColorLengthPM;
StylusColorLengthPM.type = "StylusColorLengthPM";
function makeStylusColorLengthPM(appObject) {
    return new StylusColorLengthPMImp(appObject);
}
class StylusColorLengthPMImp extends StylusColorLengthPM {
    vmsAreEqual(a, b) {
        if (core_2.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 = core_2.Color.RGB(128, 128, 128);
        this.rayColorPick = core_2.Color.RGB(0, 255, 0);
        this.onEntityChange = () => {
            if (!this.stylus || !this.stylus.isInUse) {
                this.setDefaultVM();
                return;
            }
            const pickRay = this.stylus.pickRay;
            const scenePickUC = Babylon_1.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(core_2.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_1.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