import { Block, Dimension, Player } from "@minecraft/server";
import { decrementItemStack } from "../../library/itemManager";

/**
 * Evento del componente furnaceEnergyActionComponent
 * @param {{player: Player, block: Block, dimension: Dimension}} event 
 */
export const furnaceEnergyActionEvent = ({ player, block, dimension }) => {
    const inventory = player.getComponent('minecraft:inventory');
    if (!inventory) return;
    const item = inventory.container.getItem(player.selectedSlotIndex);
    if (!item) return;
    let permutation = block.permutation;

    const itemTypeId = item.typeId;

    if(itemTypeId === 'minecraft:coal_block' && !permutation.getState('nps_ec:power')) {
        permutation = permutation.withState('nps_ec:power', 1)
        permutation = permutation.withState('nps_ec:state', 4)
        decrementItemStack(item, player);
    }

    block.setPermutation(permutation);
};