"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TryAddNewLandmarkUC = void 0;
exports.makeTryAddLandmarkUC = makeTryAddLandmarkUC;
const core_1 = require("@vived/core");
const core_2 = require("@vived/core");
const Dispatcher_1 = require("../../Dispatcher");
const Label_1 = require("../../Label");
const Selection_1 = require("../../Selection/Entities/Selection");
const Tools_1 = require("../../Tools");
const Entities_1 = require("../Entities");
const LandmarkGlobals_1 = require("../Entities/LandmarkGlobals");
const AddLandmarkUndoRedo_1 = require("./AddLandmarkUndoRedo");
const Babylon_1 = require("../../Babylon");
class TryAddNewLandmarkUC extends core_1.AppObjectUC {
    static get(appObjects) {
        return (0, core_1.getSingletonComponent)(this.type, appObjects);
    }
}
exports.TryAddNewLandmarkUC = TryAddNewLandmarkUC;
TryAddNewLandmarkUC.type = "TryAddNewLandmarkUC";
function makeTryAddLandmarkUC(appObject) {
    return new TryAddNewLandmarkUCImp(appObject);
}
class TryAddNewLandmarkUCImp extends TryAddNewLandmarkUC {
    get globals() {
        return this.getCachedSingleton(LandmarkGlobals_1.LandmarkGlobals.type);
    }
    get toolRepo() {
        return this.getCachedSingleton(Tools_1.ToolRepository.type);
    }
    get selection() {
        return this.getCachedSingleton(Selection_1.Selection.type);
    }
    allow() {
        if (this.toolRepo?.activeTool?.toolType === "LANDMARK") {
            return true;
        }
        return false;
    }
    addFromRay(ray) {
        if (!this.allow()) {
            return;
        }
        const scenePick = (0, Babylon_1.sceneRayPick)(ray, this.appObjects);
        const appObjectID = scenePick?.appObjectID;
        if (!appObjectID ||
            !scenePick ||
            !scenePick?.worldPickPoint ||
            !scenePick?.normal) {
            return;
        }
        const landmarkRepo = Entities_1.LandmarkRepo.getByID(appObjectID, this.appObjects);
        if (!landmarkRepo) {
            return;
        }
        const newLandmark = landmarkRepo.createLandmark();
        newLandmark.transform.worldPosition = scenePick.worldPickPoint;
        newLandmark.transform.worldRotation = core_2.Quaternion.FromDirectionVector(scenePick.normal);
        if (this.globals) {
            newLandmark.size = this.globals.lastSize;
            newLandmark.overlayColor.overlayColor = this.globals.lastColor;
        }
        const label = Label_1.LabelEntity.getByID(appObjectID, this.appObjects);
        newLandmark.label.default = label ? label.default : "";
        if (this.selection && newLandmark) {
            this.selection.selectedObjectId = newLandmark.appObject.id;
        }
        new AddLandmarkUndoRedo_1.AddLandmarkUndoRedo(newLandmark.data, this.appObjects);
        (0, Dispatcher_1.dispatchStateChange)(this.appObjects);
        return newLandmark;
    }
    constructor(appObject) {
        super(appObject, TryAddNewLandmarkUC.type);
    }
}
//# sourceMappingURL=TryAddNewLandmarkUC.js.map