import { AppObjectUC, getSingletonComponent, } from "@vived/core";
import { ScenePixelPickUC, SceneRayPickUC } from "../../Babylon/UC";
import { ToolRepository, ToolState } from "../../Tools";
import { StickUC } from "./StickUC";
export class TryStickAppObjectUC extends AppObjectUC {
    static get(appObjects) {
        return getSingletonComponent(TryStickAppObjectUC.type, appObjects);
    }
}
TryStickAppObjectUC.type = "TryStickAppObjectUC";
export function makeTryStickAppObjectUC(appObject) {
    return new TryStickAppObjectUCImp(appObject);
}
class TryStickAppObjectUCImp extends TryStickAppObjectUC {
    get toolRepo() {
        return this.getCachedSingleton(ToolRepository.type);
    }
    get pixelPick() {
        return this.getCachedSingleton(ScenePixelPickUC.type)
            ?.scenePixelPick;
    }
    get rayPick() {
        return this.getCachedSingleton(SceneRayPickUC.type)
            ?.sceneRayPick;
    }
    stickByID(id) {
        const appObj = this.appObjects.get(id);
        if (!appObj) {
            return;
        }
        if (!appObj.hasComponent(StickUC.type)) {
            return;
        }
        const stickUC = appObj.getComponent(StickUC.type);
        if (!stickUC) {
            return;
        }
        stickUC.doStick();
    }
    constructor(appObject) {
        super(appObject, TryStickAppObjectUC.type);
        this.allow = () => {
            const peelTool = this.toolRepo?.getTool("PEEL");
            if (!peelTool)
                return false;
            if (peelTool.state !== ToolState.ACTIVE)
                return false;
            return true;
        };
        this.tryWithPixel = (pixel) => {
            if (!this.allow())
                return;
            if (!this.pixelPick) {
                return;
            }
            const appObjectID = this.pixelPick(pixel)?.appObjectID;
            if (!appObjectID) {
                return;
            }
            this.stickByID(appObjectID);
        };
        this.tryWithRay = (pickRay) => {
            if (!this.allow())
                return;
            if (!this.rayPick) {
                return;
            }
            const appObjectID = this.rayPick(pickRay)?.appObjectID;
            if (!appObjectID) {
                return;
            }
            this.stickByID(appObjectID);
        };
        this.appObjects.registerSingleton(this);
    }
}
//# sourceMappingURL=TryStickAppObjectUC.js.map