import { AlNodeEvents } from ".";
import { ThreeUtils } from "../utils";
export class AlNodeSpawner {
    static get Object() {
        return {
            dependencies: ["raycaster"],
            schema: {
                nodesEnabled: { type: "boolean" }
            },
            bindListeners() {
                this.canvasMouseDown = this.canvasMouseDown.bind(this);
                this.elRaycasterIntersected = this.elRaycasterIntersected.bind(this);
                this.elRaycasterIntersectedCleared = this.elRaycasterIntersectedCleared.bind(this);
                this.elClick = this.elClick.bind(this);
                this.canvasMouseUp = this.canvasMouseUp.bind(this);
                this.elMouseDown = this.elMouseDown.bind(this);
                this.elMouseUp = this.elMouseUp.bind(this);
            },
            addListeners() {
                this.el.sceneEl.canvas.addEventListener("mousedown", this.canvasMouseDown, { capture: false, once: false, passive: true });
                this.el.sceneEl.canvas.addEventListener("mouseup", this.canvasMouseUp, {
                    capture: false,
                    once: false,
                    passive: true
                });
                this.el.addEventListener("raycaster-intersected", this.elRaycasterIntersected, { capture: false, once: false, passive: true });
                this.el.addEventListener("raycaster-intersected-cleared", this.elRaycasterIntersectedCleared, false);
                this.el.addEventListener("click", this.elClick, {
                    capture: false,
                    once: false,
                    passive: true
                });
                this.el.addEventListener("mousedown", this.elMouseDown, {
                    capture: false,
                    once: false,
                    passive: true
                });
                this.el.addEventListener("mouseup", this.elMouseUp, {
                    capture: false,
                    once: false,
                    passive: true
                });
            },
            removeListeners() {
                this.el.sceneEl.canvas.removeEventListener("mousedown", this.canvasMouseDown);
                this.el.sceneEl.canvas.removeEventListener("mouseup", this.canvasMouseUp);
                this.el.removeEventListener("raycaster-intersected", this.elRaycasterIntersected);
                this.el.removeEventListener("raycaster-intersected-cleared", this.elRaycasterIntersectedCleared);
                this.el.removeEventListener("click", this.elClick);
                this.el.removeEventListener("mousedown", this.elMouseDown);
                this.el.removeEventListener("mouseup", this.elMouseUp);
            },
            canvasMouseDown(event) {
                this.state.left = event.button === 0;
            },
            canvasMouseUp(_event) {
                ThreeUtils.waitOneFrame(() => {
                    this.state.left = false;
                });
            },
            elRaycasterIntersected(_event) {
                this.state.intersecting = true;
                this.el.sceneEl.emit(AlNodeSpawnerEvents.VALID_TARGET, { valid: true }, false);
            },
            elRaycasterIntersectedCleared(_event) {
                this.state.intersecting = false;
                this.el.sceneEl.emit(AlNodeSpawnerEvents.VALID_TARGET, { valid: false }, false);
            },
            elMouseDown(_event) {
                if (this.data.nodesEnabled) {
                    this.el.sceneEl.emit(AlNodeEvents.CONTROLS_DISABLED, {}, false);
                }
            },
            elMouseUp(_event) {
                if (this.data.nodesEnabled) {
                    this.el.sceneEl.emit(AlNodeEvents.CONTROLS_ENABLED, {}, false);
                }
            },
            elClick(event) {
                if (this.state.left && this.data.nodesEnabled) {
                    this.el.sceneEl.emit(AlNodeSpawnerEvents.ADD_NODE, event, false);
                }
            },
            init() {
                this.bindListeners();
                this.addListeners();
                this.state = {
                    left: false,
                    intersecting: false
                };
            },
            remove() {
                this.removeListeners();
            }
        };
    }
    static get Tag() {
        return "al-node-spawner";
    }
}
export class AlNodeSpawnerEvents {
}
AlNodeSpawnerEvents.VALID_TARGET = "al-valid-target";
AlNodeSpawnerEvents.ADD_NODE = "al-add-node";
