import { BlockPermutation, EquipmentSlot, world, } from "@minecraft/server";
import { namespace } from "../../../config/_config";
import { getItemInSlot } from "../../helper/utils";
export class ReplaceBlock {
    constructor() {
        ReplaceBlock.initialize();
    }
    static initialize() {
        world.beforeEvents.worldInitialize.subscribe((event) => {
            event.blockComponentRegistry.registerCustomComponent(`${namespace}:replace_block`, {
                onPlayerInteract: (event) => {
                    this.checkBlock(event);
                },
            });
        });
    }
    static checkBlock(event) {
        const { block } = event;
        if (!block)
            return;
        const player = event.player;
        if (!player)
            return;
        const placeBlock = getItemInSlot(event.player, EquipmentSlot.Mainhand)?.typeId.split("_item")[0];
        if (!placeBlock)
            return;
        if (placeBlock === block.typeId) {
            return;
        }
        try {
            block.setPermutation(BlockPermutation.resolve(placeBlock));
            const sound = this.placeSounds[placeBlock] || "dig.grass";
            block.dimension.playSound(sound, block.location);
        }
        catch (error) { }
    }
}
ReplaceBlock.placeSounds = {
    "minecraft:stone": "dig.stone",
    "minecraft:grass": "dig.grass",
    "minecraft:dirt": "dig.grass",
    "minecraft:wood": "dig.wood",
    "minecraft:sand": "dig.sand",
    "minecraft:gravel": "dig.gravel",
    "minecraft:gold_ore": "dig.stone",
    "minecraft:iron_ore": "dig.stone",
    "minecraft:coal_ore": "dig.stone",
    "minecraft:log": "dig.wood",
    "minecraft:lapis_ore": "dig.stone",
    "minecraft:lapis_block": "dig.stone",
    "minecraft:sandstone": "dig.stone",
    "minecraft:emerald_ore": "dig.stone",
    "minecraft:emerald_block": "dig.stone",
    "minecraft:redstone_ore": "dig.stone",
    "minecraft:redstone_block": "dig.stone",
    "minecraft:clay": "dig.gravel",
    "minecraft:glowstone": "dig.stone",
    "minecraft:stonebrick": "dig.stone",
    "minecraft:nether_brick": "dig.stone",
    "minecraft:end_stone": "dig.stone",
    "minecraft:obsidian": "dig.stone",
    "minecraft:diamond_ore": "dig.stone",
    "minecraft:diamond_block": "dig.stone",
    "minecraft:ice": "dig.stone",
    "minecraft:netherrack": "dig.stone",
    "minecraft:packed_ice": "dig.stone",
    "minecraft:prismarine": "dig.stone",
    "minecraft:sea_lantern": "dig.stone",
    "minecraft:hay_block": "dig.grass",
    "minecraft:purpur_block": "dig.stone",
    "minecraft:quartz_ore": "dig.stone",
    "minecraft:quartz_block": "dig.stone",
    "minecraft:shulker_box": "dig.stone",
    "minecraft:chorus_flower": "dig.grass",
    "minecraft:purpur_pillar": "dig.stone",
    "minecraft:polished_granite": "dig.stone",
    "minecraft:smooth_red_sandstone": "dig.stone",
    "minecraft:mossy_stone_brick": "dig.stone",
};
