import { system } from "@minecraft/server";
import { Dimensions } from "../../helper/utils";
export class RideEntity {
    //constructor
    constructor(config) {
        this.config = config;
    }
    static registerBehavior(config) {
        const eventEntity = new RideEntity(config);
        this.entities.push(eventEntity);
    }
    static getEntity(id) {
        return RideEntity.entities.find((entity) => entity.config.id === id);
    }
    static initialize() {
        system.runInterval(() => {
            for (const dimension in Dimensions) {
                for (const config of this.entities) {
                    Dimensions[dimension].getEntities({ type: config.config.id }).forEach((entity) => {
                        const component = entity.getComponent("minecraft:rideable");
                        if (!component)
                            return;
                        const currentRider = component.getRiders()[0];
                        if (!currentRider) {
                            if (this.entityLinkedToPlayer.get(entity)) {
                                if (config.config.events?.onRideEnd) {
                                    const player = this.entityLinkedToPlayer.get(entity);
                                    if (player) {
                                        config.config.events.onRideEnd(entity, player);
                                    }
                                }
                                this.entityLinkedToPlayer.delete(entity);
                            }
                            return;
                        }
                        if (this.entityLinkedToPlayer.get(entity) == undefined) {
                            this.entityLinkedToPlayer.set(entity, currentRider);
                            if (config.config.events?.onRideStart) {
                                config.config.events.onRideStart(entity, currentRider);
                            }
                        }
                        if (this.entityLinkedToPlayer.get(entity) === currentRider) {
                            if (config.config.events?.onRideInterval) {
                                config.config.events.onRideInterval(entity, currentRider);
                            }
                            if (config.config.playerNavigation) {
                                this.rideNavigation(entity, currentRider, config);
                            }
                            if (config.config.playerAnimation) {
                                currentRider.playAnimation(config.config.playerAnimation.animation, config.config.playerAnimation.options);
                            }
                        }
                    });
                }
            }
        }, 1);
    }
    static rideNavigation(entity, player, config) {
        const direction = player.getViewDirection();
        const velocity = entity.getVelocity();
        if (velocity.x !== 0 && velocity.z !== 0) {
            if (config.config.playerAnimation && config.config.playerNavigation?.verticalSpeed) {
                entity.applyKnockback(0, 0, 0, direction.y * config.config.playerNavigation?.verticalSpeed);
            }
        }
        else {
            if (config.config.playerNavigation && config.config.playerNavigation.hover == true) {
                this.hover(entity);
            }
        }
        //entity.applyImpulse({ x: 0, y: 0.1567, z: 0 });
    }
    static hover(entity) {
        entity.addEffect("minecraft:levitation", 20, {
            amplifier: 2,
            showParticles: false
        });
        entity.applyKnockback(0, 0, 0, -0.001);
    }
}
RideEntity.entities = [];
RideEntity.entityLinkedToPlayer = new Map();
