"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CalloutLinePointsPM = void 0;
exports.makeCalloutLinePointsPM = makeCalloutLinePointsPM;
const core_1 = require("@vived/core");
const SceneRootEntity_1 = require("../../SceneRoot/Entities/SceneRootEntity");
const CalloutEntity_1 = require("../Entities/CalloutEntity");
class CalloutLinePointsPM extends core_1.AppObjectPM {
    static getById(id, appObjects) {
        return appObjects
            .get(id)
            ?.getComponent(CalloutLinePointsPM.type);
    }
}
exports.CalloutLinePointsPM = CalloutLinePointsPM;
CalloutLinePointsPM.type = "CalloutLinePointsPM";
function makeCalloutLinePointsPM(appObject) {
    return new CalloutLinePMImp(appObject);
}
class CalloutLinePMImp extends CalloutLinePointsPM {
    get callout() {
        return this.getCachedLocalComponent(CalloutEntity_1.CalloutEntity.type);
    }
    get sceneRootEntity() {
        return this.getCachedSingleton(SceneRootEntity_1.SceneRootEntity.type);
    }
    vmsAreEqual(a, b) {
        if (a.length !== b.length)
            return false;
        let arraysAreTheSame = true;
        a.forEach((aVec, i) => {
            if (!arraysAreTheSame)
                return;
            const bVec = b[i];
            if (!core_1.Vector3.Equal(aVec, bVec)) {
                arraysAreTheSame = false;
            }
        });
        return arraysAreTheSame;
    }
    constructor(appObject) {
        super(appObject, CalloutLinePointsPM.type);
        this.bumpScaleFactor = 0.002;
        this.onChange = () => {
            if (!this.callout || !this.sceneRootEntity)
                return;
            const bumpSize = this.bumpScaleFactor / this.sceneRootEntity.scale;
            const startPos = this.callout.rootTransform.localPosition;
            const startDir = core_1.Vector3.Transform(core_1.Vector3.Forward(), this.callout.rootTransform.localMatrix);
            const startBumpOffset = core_1.Vector3.NewVectorOfLength(startDir, bumpSize);
            const startBump = core_1.Vector3.Add(startPos, startBumpOffset);
            const endPos = this.callout.labelTransform.localPosition;
            const offsetDirection = this.callout.offsetDirection;
            let localEndDir = core_1.Vector3.Left();
            if (offsetDirection === "DOWN") {
                localEndDir = core_1.Vector3.Up();
            }
            else if (offsetDirection === "UP") {
                localEndDir = core_1.Vector3.Down();
            }
            else if (offsetDirection === "LEFT") {
                localEndDir = core_1.Vector3.Left();
            }
            else if (offsetDirection === "RIGHT") {
                localEndDir = core_1.Vector3.Right();
            }
            const endDir = core_1.Vector3.Transform(localEndDir, this.callout.labelTransform.localMatrix);
            const endBumpOffset = core_1.Vector3.NewVectorOfLength(endDir, bumpSize);
            const endBump = core_1.Vector3.Add(endPos, endBumpOffset);
            this.doUpdateView([startPos, startBump, endBump, endPos]);
        };
        this.dispose = () => {
            super.dispose();
            this.sceneRootEntity?.removeChangeObserver(this.onChange);
            this.callout?.removeChangeObserver(this.onChange);
            this.callout?.rootTransform.removeChangeObserver(this.onChange);
            this.callout?.labelTransform.removeChangeObserver(this.onChange);
        };
        if (this.callout) {
            this.callout.addChangeObserver(this.onChange);
            this.callout.rootTransform.addChangeObserver(this.onChange);
            this.callout.labelTransform.addChangeObserver(this.onChange);
        }
        else {
            this.error("App Object does not have a callout Entity");
        }
        if (this.sceneRootEntity) {
            this.sceneRootEntity.addChangeObserver(this.onChange);
        }
        else {
            this.error("Unable to find scene root");
        }
        this.onChange();
    }
}
//# sourceMappingURL=CalloutLinePointsPM.js.map