"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TryPreviewLandmarkUC = void 0;
exports.makeTryPreviewLandmark = makeTryPreviewLandmark;
const core_1 = require("@vived/core");
const core_2 = require("@vived/core");
const Babylon_1 = require("../../Babylon");
const Tools_1 = require("../../Tools");
const LandmarkEntity_1 = require("../Entities/LandmarkEntity");
const LandmarkRepo_1 = require("../Entities/LandmarkRepo");
class TryPreviewLandmarkUC extends core_1.AppObjectUC {
    static get(appObjects) {
        return (0, core_1.getSingletonComponent)(this.type, appObjects);
    }
}
exports.TryPreviewLandmarkUC = TryPreviewLandmarkUC;
TryPreviewLandmarkUC.type = "TryPreviewLandmarkUC";
function makeTryPreviewLandmark(appObject) {
    const uc = new TryPreviewLandmarkUCImp(appObject);
    return uc;
}
class TryPreviewLandmarkUCImp extends TryPreviewLandmarkUC {
    get landmark() {
        return this.getCachedLocalComponent(LandmarkEntity_1.LandmarkEntity.type);
    }
    get toolRepo() {
        return this.getCachedSingleton(Tools_1.ToolRepository.type);
    }
    allow() {
        if (this.toolRepo?.activeTool?.toolType === "LANDMARK") {
            return true;
        }
        return false;
    }
    constructor(appObject) {
        super(appObject, TryPreviewLandmarkUC.type);
        this.hidePreview = () => {
            if (!this.landmark)
                return;
            this.landmark.hideable.isHidden = true;
        };
        this.tryPreviewLandmark = (ray, pressed) => {
            if (!this.landmark) {
                return false;
            }
            if (!this.allow()) {
                this.hidePreview();
                return false;
            }
            const scenePick = (0, Babylon_1.sceneRayPick)(ray, this.appObjects);
            if (!scenePick ||
                !scenePick.worldPickPoint ||
                !scenePick.normal ||
                !scenePick.appObjectID) {
                this.hidePreview();
                return;
            }
            const aoRepo = this.appObjects.getAppObjectComponent(scenePick.appObjectID, LandmarkRepo_1.LandmarkRepo.type);
            if (!aoRepo) {
                this.hidePreview();
                return;
            }
            this.landmark.hideable.isHidden = false;
            this.landmark.position = scenePick.worldPickPoint;
            this.landmark.rotation = core_2.Quaternion.FromDirectionVector(scenePick.normal);
            this.landmark.selectable.pressed = pressed;
        };
    }
}
//# sourceMappingURL=TryPreviewLandmarkUC.js.map