import { system, world } from "@minecraft/server";
import { namespace } from "../../../config/_config";
import { Dimensions } from "../../helper/utils";
/**
 * SummonBlock class defines the logic for placing blocks in the game when an entity meets
 * specific conditions, such as being on the ground, and optionally plays particles and sounds.
 *
 * @param {string} id - The unique identifier for the summon block.
 * @param {string} blockId - The ID of the block type to place.
 * @param {BlockState[]} states - The states of the block to be placed (e.g., orientation, shape).
 * @param {string[]} particles - Optional particle effects to emit when placing the block.
 * @param {string} sound - Optional sound to play when the block is placed.
 */
export class SummonBlockEntity {
    constructor(config) {
        this.config = config;
    }
    /**
     * Registers a summon block with the specified configuration.
     * @param {SummonBlockEntityOptions} config - The configuration for the summon block.
     */
    static registerBehavior(config) {
        const summonBlock = new SummonBlockEntity(config);
        this.entities.push(summonBlock);
    }
    /**
     * Initializes the block placement logic for registered blocks by scanning
     * through the overworld, nether, and end dimensions at regular intervals.
     */
    static initialize() {
        // Repeatedly run the logic every 20 ticks (approx 1 second).
        system.runInterval(() => {
            for (const dimension in Dimensions) {
                for (const config of this.entities) {
                    Dimensions[dimension].getEntities({ type: config.config.id }).forEach((entity) => {
                        if (entity.isOnGround) {
                            const blockLocation = entity.location; // Get the location of the entity.
                            // Retrieve the block at the entity's location and set its type to the registered block.
                            const blockToPlace = entity.dimension.getBlock(blockLocation);
                            if (blockToPlace) {
                                world.sendMessage(`Placing block at ${config.config.block.id}`);
                                blockToPlace.setType(config.config.block.id); // Set the block type.
                                // Set the states (properties) of the block, like rotation or orientation.
                                if (config.config.block.states) {
                                    config.config.block.states.forEach((state) => {
                                        blockToPlace.setPermutation(blockToPlace.permutation.withState(state.state, state.value));
                                    });
                                }
                                // Emit particle effects if any are defined for the block.
                                if (config.config.particles) {
                                    config.config.particles.forEach((particle) => {
                                        entity.dimension.spawnParticle(particle, blockLocation);
                                    });
                                }
                                // Play a sound effect if any is defined for the block.
                                if (config.config.sound) {
                                    entity.dimension.playSound(config.config.sound, blockLocation);
                                }
                                // Despawn the entity after the block is placed.
                                entity.triggerEvent(`${namespace}:despawn`);
                            }
                        }
                    });
                }
            }
        }, 20); // Run every 20 ticks (1 second).
    }
}
SummonBlockEntity.entities = [];
