import {
  world,
  BlockPermutation,
  system
} from "@minecraft/server";

world.beforeEvents.playerBreakBlock.subscribe((event) => {

  const player = event.player;

  if (player.matches({
      gameMode: 'creative'
  })) return;


  const item = event.itemStack;
  const block = event.block;
  const permutation = block.permutation;

  const {
    x,
    y,
    z
  } = block;

  if (!item) return;

  if (block.hasTag('we:leaves')) {
    system.run(() => {

        if (item.typeId === 'minecraft:shears') {

          const newPermutation = permutation.withState('we:drop_loot', false);
          block.setPermutation(newPermutation);
          block.dimension.runCommandAsync(`/setblock ${x} ${y} ${z} air destroy`);
          return;
        } else {
          const newPermutation = permutation.withState('we:drop_loot', true);
          block.setPermutation(newPermutation);
          block.dimension.runCommandAsync(`/setblock ${x} ${y} ${z} air destroy`);
          return;
        }
      }
    )
  }
});