import { BlockPermutation, EntityEquippableComponent, EquipmentSlot, GameMode, system, world } from "@minecraft/server";
import { ItemManager } from "../../Item/manager";
export class BlockComponentLogStrip {
    logs;
    constructor(logs) {
        this.logs = logs;
        world.beforeEvents.playerInteractWithBlock.subscribe((data) => {
            system.runTimeout(() => {
                if (!data.isFirstEvent)
                    return;
                const { player, block } = data;
                if (!player || !block)
                    return;
                const mainhand = player.getComponent(EntityEquippableComponent.componentId).getEquipmentSlot(EquipmentSlot.Mainhand);
                if (!mainhand)
                    return;
                const item = mainhand.getItem();
                if (!item || !item.hasTag("minecraft:is_axe"))
                    return;
                const found = this.logs.find((f) => f.fromId == block.typeId);
                if (!found)
                    return;
                const states = block.permutation.getAllStates();
                block.setPermutation(BlockPermutation.resolve(found.toId));
                if (found.keepStates) {
                    for (const state in states) {
                        block.setPermutation(block.permutation.withState(state, states[state]));
                    }
                }
                const sound = found.sound;
                if (sound)
                    player.dimension.playSound(sound.id, block.location, { volume: sound.volume, pitch: sound.pitch });
                if (player.getGameMode() != GameMode.creative)
                    mainhand.setItem(ItemManager.reduceDurability(item));
            });
        });
    }
}
