import { AppObjectUC, getSingletonComponent, } from "@vived/core";
import { Quaternion } from "@vived/core";
import { dispatchStateChange } from "../../Dispatcher";
import { LabelEntity } from "../../Label";
import { Selection } from "../../Selection/Entities/Selection";
import { ToolRepository } from "../../Tools";
import { LandmarkRepo } from "../Entities";
import { LandmarkGlobals } from "../Entities/LandmarkGlobals";
import { AddLandmarkUndoRedo } from "./AddLandmarkUndoRedo";
import { sceneRayPick } from "../../Babylon";
export class TryAddNewLandmarkUC extends AppObjectUC {
    static get(appObjects) {
        return getSingletonComponent(this.type, appObjects);
    }
}
TryAddNewLandmarkUC.type = "TryAddNewLandmarkUC";
export function makeTryAddLandmarkUC(appObject) {
    return new TryAddNewLandmarkUCImp(appObject);
}
class TryAddNewLandmarkUCImp extends TryAddNewLandmarkUC {
    get globals() {
        return this.getCachedSingleton(LandmarkGlobals.type);
    }
    get toolRepo() {
        return this.getCachedSingleton(ToolRepository.type);
    }
    get selection() {
        return this.getCachedSingleton(Selection.type);
    }
    allow() {
        if (this.toolRepo?.activeTool?.toolType === "LANDMARK") {
            return true;
        }
        return false;
    }
    addFromRay(ray) {
        if (!this.allow()) {
            return;
        }
        const scenePick = sceneRayPick(ray, this.appObjects);
        const appObjectID = scenePick?.appObjectID;
        if (!appObjectID ||
            !scenePick ||
            !scenePick?.worldPickPoint ||
            !scenePick?.normal) {
            return;
        }
        const landmarkRepo = LandmarkRepo.getByID(appObjectID, this.appObjects);
        if (!landmarkRepo) {
            return;
        }
        const newLandmark = landmarkRepo.createLandmark();
        newLandmark.transform.worldPosition = scenePick.worldPickPoint;
        newLandmark.transform.worldRotation = Quaternion.FromDirectionVector(scenePick.normal);
        if (this.globals) {
            newLandmark.size = this.globals.lastSize;
            newLandmark.overlayColor.overlayColor = this.globals.lastColor;
        }
        const label = LabelEntity.getByID(appObjectID, this.appObjects);
        newLandmark.label.default = label ? label.default : "";
        if (this.selection && newLandmark) {
            this.selection.selectedObjectId = newLandmark.appObject.id;
        }
        new AddLandmarkUndoRedo(newLandmark.data, this.appObjects);
        dispatchStateChange(this.appObjects);
        return newLandmark;
    }
    constructor(appObject) {
        super(appObject, TryAddNewLandmarkUC.type);
    }
}
//# sourceMappingURL=TryAddNewLandmarkUC.js.map