import { checkTopOrBottomNeighborHasTag } from "../../library/blockManager";

/**
 * Evento del componente analyzerLootComponent
 * @param {{block: Block, dimension: Dimension}} event 
 */
export const analyzerLootEvent = ({ block, dimension }) => {
    try {
        if (!block) return;
        const variant = block.permutation.getState('nps_ec:variants');
        if (checkTopOrBottomNeighborHasTag(block, 'power:on')) {
            let lootTable = '';
            switch (variant) {
                case 'skeleton':
                    lootTable = 'blocks/skeleton';
                    break;
                case 'zombie':
                    lootTable = 'blocks/zombie';
                    break;
                case 'cow':
                    lootTable = 'blocks/cow';
                    break;
                case 'sheep':
                    lootTable = 'blocks/sheep';
                    break;
                case 'cat':
                    lootTable = 'blocks/cat';
                    dimension.runCommand(`tellraw @p {"rawtext":[{"translate":"text.digital.dna_damage.message"}]}`);
                    break;
                case 'wolf':
                    lootTable = 'blocks/wolf';
                    dimension.runCommand(`tellraw @p {"rawtext":[{"translate":"text.digital.dna_damage.message"}]}`);
                    break;
                case 'iron_golem':
                    lootTable = 'blocks/iron_golem';
                    break;
                default:
                    return;
            }
            dimension.runCommand(`loot spawn ${block.x} ${block.y} ${block.z} loot "${lootTable}"`);
            dimension.runCommand(`playsound digital.spawn @a ${block.x} ${block.y} ${block.z}`);
            dimension.runCommand(`particle nps_ec:analyzer ${block.x} ${block.y + 0.5} ${block.z}`);
        } else {
            dimension.runCommand(`tellraw @p {"rawtext":[{"translate":"text.digital.dna_error.message"}]}`);
        }
        let permutation = block.permutation;
        permutation = permutation.withState('nps_ec:action', false)
                                .withState('nps_ec:capsule', false)
                                .withState('nps_ec:variants', 'none')
                                .withState('nps_ec:center', false);
        block.setPermutation(permutation);
    } catch { }
}