import { system, world, } from "@minecraft/server";
import { namespace } from "../../../config/_config";
// SeatBlock class allows a block to function as a seat that players can interact with to sit on.
export class SeatBlock {
    constructor({ id, position }) {
        this.id = id;
        this.position = position;
    }
    static registerBehavior(config) {
        const block = new SeatBlock(config);
        this.register(block);
    }
    // Method to register a SeatBlock
    static register(block) {
        SeatBlock.blocks.push(block);
    }
    // Initialization method to register the seat component with the world
    static initialize() {
        world.beforeEvents.worldInitialize.subscribe((event) => {
            event.blockComponentRegistry.registerCustomComponent(`${namespace}:seat`, {
                onPlayerInteract: (event) => this.handleSit(event),
            });
        });
    }
    // Method to handle player interaction with the seat block (when a player tries to sit)
    static handleSit(event) {
        const block = event.block; // The block the player is interacting with
        const player = event.player; // The player interacting with the block
        // Exit if no block or player is present
        if (!block || !player)
            return;
        // Find the SeatBlock data by the block's type ID
        const seatBlock = SeatBlock.blocks.find((b) => b.id === block.typeId);
        // Exit if no seat block data is found
        if (!seatBlock)
            return;
        // Calculate the location where the player should sit based on the seat's position
        const location = {
            x: block.location.x + seatBlock.position.x,
            y: block.location.y + seatBlock.position.y,
            z: block.location.z + seatBlock.position.z,
        };
        // Get the cardinal direction of the block to determine player orientation
        const cardinalDirection = block.permutation.getState("minecraft:cardinal_direction");
        // Get the player's rotation based on the block's direction
        const rotation = this.getRotationFromCardinalDirection(cardinalDirection);
        // Spawn a seat entity at the calculated location
        const entity = block.dimension.spawnEntity(`${namespace}:seat`, location);
        // Set the entity's rotation to match the seat's direction
        entity.setRotation(rotation);
        // Command the player to start riding the seat entity
        entity.runCommandAsync(`/ride ${player.name} start_riding @s`);
        // Run a delayed event to trigger the 'has_rider' event for the seat entity
        system.runTimeout(() => {
            entity.triggerEvent("cc:has_rider");
        }, 1);
    }
    // Method to get the rotation (direction) of the seat based on the block's cardinal direction
    static getRotationFromCardinalDirection(cardinalDirection) {
        // Return rotation values based on the block's cardinal direction
        switch (cardinalDirection) {
            case "north":
                return { x: 0, y: 0 }; // Facing north
            case "south":
                return { x: 0, y: 180 }; // Facing south
            case "east":
                return { x: 0, y: 90 }; // Facing east
            case "west":
                return { x: 0, y: 270 }; // Facing west
            default:
                return { x: 0, y: 0 }; // Default to north-facing if no valid direction is found
        }
    }
}
// Array to store all registered SeatBlock instances
SeatBlock.blocks = [];
