import { AppObjectPM, Vector3 } from "@vived/core";
import { SceneRootEntity } from "../../SceneRoot/Entities/SceneRootEntity";
import { CalloutEntity } from "../Entities/CalloutEntity";
export class CalloutLinePointsPM extends AppObjectPM {
    static getById(id, appObjects) {
        return appObjects
            .get(id)
            ?.getComponent(CalloutLinePointsPM.type);
    }
}
CalloutLinePointsPM.type = "CalloutLinePointsPM";
export function makeCalloutLinePointsPM(appObject) {
    return new CalloutLinePMImp(appObject);
}
class CalloutLinePMImp extends CalloutLinePointsPM {
    get callout() {
        return this.getCachedLocalComponent(CalloutEntity.type);
    }
    get sceneRootEntity() {
        return this.getCachedSingleton(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 (!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 = Vector3.Transform(Vector3.Forward(), this.callout.rootTransform.localMatrix);
            const startBumpOffset = Vector3.NewVectorOfLength(startDir, bumpSize);
            const startBump = Vector3.Add(startPos, startBumpOffset);
            const endPos = this.callout.labelTransform.localPosition;
            const offsetDirection = this.callout.offsetDirection;
            let localEndDir = Vector3.Left();
            if (offsetDirection === "DOWN") {
                localEndDir = Vector3.Up();
            }
            else if (offsetDirection === "UP") {
                localEndDir = Vector3.Down();
            }
            else if (offsetDirection === "LEFT") {
                localEndDir = Vector3.Left();
            }
            else if (offsetDirection === "RIGHT") {
                localEndDir = Vector3.Right();
            }
            const endDir = Vector3.Transform(localEndDir, this.callout.labelTransform.localMatrix);
            const endBumpOffset = Vector3.NewVectorOfLength(endDir, bumpSize);
            const endBump = 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