"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TryAddCalloutUC = void 0;
exports.makeTryAddCalloutUC = makeTryAddCalloutUC;
const core_1 = require("@vived/core");
const UC_1 = require("../../Babylon/UC");
const Selection_1 = require("../../Selection/Entities/Selection");
const Tools_1 = require("../../Tools");
const PanTransformUC_1 = require("../../Transform3D/UC/PanTransformUC");
const CreateCalloutUC_1 = require("./CreateCalloutUC");
class TryAddCalloutUC extends core_1.AppObjectUC {
    static get(appObjects) {
        return (0, core_1.getSingletonComponent)(TryAddCalloutUC.type, appObjects);
    }
}
exports.TryAddCalloutUC = TryAddCalloutUC;
TryAddCalloutUC.type = "TryAddCalloutUC";
function makeTryAddCalloutUC(appObject) {
    return new TryAddCalloutImpUC(appObject);
}
class TryAddCalloutImpUC extends TryAddCalloutUC {
    get selection() {
        return this.getCachedSingleton(Selection_1.Selection.type);
    }
    get pixelToRay() {
        return this.getCachedSingleton(UC_1.PixelToSceneRayUC.type)
            ?.pixelToSceneRay;
    }
    get toolRepo() {
        return this.getCachedSingleton(Tools_1.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 = UC_1.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_1.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_1.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