import { AppObjectUC, getSingletonComponent, } from "@vived/core";
import { Quaternion } from "@vived/core";
import { sceneRayPick } from "../../Babylon";
import { ToolRepository } from "../../Tools";
import { LandmarkEntity } from "../Entities/LandmarkEntity";
import { LandmarkRepo } from "../Entities/LandmarkRepo";
export class TryPreviewLandmarkUC extends AppObjectUC {
    static get(appObjects) {
        return getSingletonComponent(this.type, appObjects);
    }
}
TryPreviewLandmarkUC.type = "TryPreviewLandmarkUC";
export function makeTryPreviewLandmark(appObject) {
    const uc = new TryPreviewLandmarkUCImp(appObject);
    return uc;
}
class TryPreviewLandmarkUCImp extends TryPreviewLandmarkUC {
    get landmark() {
        return this.getCachedLocalComponent(LandmarkEntity.type);
    }
    get toolRepo() {
        return this.getCachedSingleton(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 = sceneRayPick(ray, this.appObjects);
            if (!scenePick ||
                !scenePick.worldPickPoint ||
                !scenePick.normal ||
                !scenePick.appObjectID) {
                this.hidePreview();
                return;
            }
            const aoRepo = this.appObjects.getAppObjectComponent(scenePick.appObjectID, LandmarkRepo.type);
            if (!aoRepo) {
                this.hidePreview();
                return;
            }
            this.landmark.hideable.isHidden = false;
            this.landmark.position = scenePick.worldPickPoint;
            this.landmark.rotation = Quaternion.FromDirectionVector(scenePick.normal);
            this.landmark.selectable.pressed = pressed;
        };
    }
}
//# sourceMappingURL=TryPreviewLandmarkUC.js.map