"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TryStickAppObjectUC = void 0;
exports.makeTryStickAppObjectUC = makeTryStickAppObjectUC;
const core_1 = require("@vived/core");
const UC_1 = require("../../Babylon/UC");
const Tools_1 = require("../../Tools");
const StickUC_1 = require("./StickUC");
class TryStickAppObjectUC extends core_1.AppObjectUC {
    static get(appObjects) {
        return (0, core_1.getSingletonComponent)(TryStickAppObjectUC.type, appObjects);
    }
}
exports.TryStickAppObjectUC = TryStickAppObjectUC;
TryStickAppObjectUC.type = "TryStickAppObjectUC";
function makeTryStickAppObjectUC(appObject) {
    return new TryStickAppObjectUCImp(appObject);
}
class TryStickAppObjectUCImp extends TryStickAppObjectUC {
    get toolRepo() {
        return this.getCachedSingleton(Tools_1.ToolRepository.type);
    }
    get pixelPick() {
        return this.getCachedSingleton(UC_1.ScenePixelPickUC.type)
            ?.scenePixelPick;
    }
    get rayPick() {
        return this.getCachedSingleton(UC_1.SceneRayPickUC.type)
            ?.sceneRayPick;
    }
    stickByID(id) {
        const appObj = this.appObjects.get(id);
        if (!appObj) {
            return;
        }
        if (!appObj.hasComponent(StickUC_1.StickUC.type)) {
            return;
        }
        const stickUC = appObj.getComponent(StickUC_1.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 !== Tools_1.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