import { AppObjectUC, getSingletonComponent, } from "@vived/core";
import { PixelToSceneRayUC, SceneRayPickUC } from "../../Babylon/UC";
import { Selection } from "../../Selection/Entities/Selection";
import { ToolRepository } from "../../Tools";
import { PanTransformUC } from "../../Transform3D/UC/PanTransformUC";
import { CreateCalloutUC } from "./CreateCalloutUC";
export class TryAddCalloutUC extends AppObjectUC {
    static get(appObjects) {
        return getSingletonComponent(TryAddCalloutUC.type, appObjects);
    }
}
TryAddCalloutUC.type = "TryAddCalloutUC";
export function makeTryAddCalloutUC(appObject) {
    return new TryAddCalloutImpUC(appObject);
}
class TryAddCalloutImpUC extends TryAddCalloutUC {
    get selection() {
        return this.getCachedSingleton(Selection.type);
    }
    get pixelToRay() {
        return this.getCachedSingleton(PixelToSceneRayUC.type)
            ?.pixelToSceneRay;
    }
    get toolRepo() {
        return this.getCachedSingleton(ToolRepository.type);
    }
    initializeWithPixel(pixel) {
        if (!this.allow())
            return false;
        if (!this.pixelToRay) {
            this.error("Missing component");
            return false;
        }
        const ray = this.pixelToRay(pixel);
        return this.initializeWithRay(ray);
    }
    constructor(appObject) {
        super(appObject, TryAddCalloutUC.type);
        this.allow = () => {
            if (this.toolRepo?.activeTool?.toolType === "CALLOUT") {
                return true;
            }
            return false;
        };
        this.moveWithPixel = (pixel) => {
            if (!this.pixelToRay) {
                this.error("Missing component");
                return;
            }
            const ray = this.pixelToRay(pixel);
            return this.moveWithRay(ray);
        };
        this.initializeWithRay = (ray) => {
            this.addingCallout = undefined;
            if (!this.allow())
                return false;
            const scenePick = SceneRayPickUC.get(this.appObjects)?.sceneRayPick(ray);
            if (!scenePick ||
                !scenePick.worldPickPoint ||
                scenePick.hit === false ||
                !scenePick.normal ||
                !scenePick.objectPickMeta) {
                return false;
            }
            const appObjectID = scenePick.objectPickMeta.appObjectID;
            if (!appObjectID) {
                return false;
            }
            const appObject = this.appObject.appObjectRepo.get(appObjectID);
            if (!appObject) {
                return false;
            }
            const createCalloutUC = CreateCalloutUC.get(appObject);
            if (!createCalloutUC) {
                return false;
            }
            this.addingCallout = createCalloutUC.fromPick(scenePick.worldPickPoint, scenePick.normal);
            if (!this.addingCallout)
                return false;
            this.dragUC = this.addingCallout.labelTransform.appObject.getComponent(PanTransformUC.type);
            if (this.dragUC) {
                this.dragUC.dispatchStateChangeOnComplete = false;
                this.dragUC.submitUndoOnComplete = false;
                this.dragUC.initializeRay(ray);
            }
            return true;
        };
        this.moveWithRay = (ray) => {
            if (this.dragUC) {
                this.dragUC.updateRay(ray);
            }
        };
        this.stop = () => {
            if (this.dragUC) {
                this.dragUC.finish();
                this.dragUC.dispatchStateChangeOnComplete = true;
                this.dragUC.submitUndoOnComplete = true;
                if (this.selection && this.addingCallout) {
                    this.selection.selectedObjectId = this.addingCallout.appObject.id;
                }
            }
            this.addingCallout = undefined;
            this.dragUC = undefined;
        };
        this.cancel = () => {
            if (!this.addingCallout)
                return;
            this.addingCallout.inRepo?.deleteCallout(this.addingCallout.id);
            this.addingCallout = undefined;
            this.dragUC = undefined;
        };
    }
}
//# sourceMappingURL=TryAddCallout.js.map