import { world, system, BlockPermutation, GameMode } from "@minecraft/server";
import { decreaseItemStack } from "../ntk_functions.js";

world.beforeEvents.playerInteractWithBlock.subscribe(event => {
  const player = event.player;
  const itemStack = event.itemStack;
  const block = event.block;
  const blockAllState = block.permutation.getAllStates();
  const strippedWoods = [
    "ntk:stripped_mosshroom_hyphae",
    "ntk:stripped_mosshroom_stem"
  ]
  if (itemStack === undefined) return;
  if (strippedWoods.includes(block.typeId)) {
    if (itemStack.typeId === "minecraft:bone_meal" || itemStack.hasTag("ntk:bone_meal")) {
      system.run(() => {
        decreaseItemStack(player, itemStack, 1, "Mainhand");
        block.dimension.playSound("item.bone_meal.use", block.location, { pitch: 1.0, volume: 1.0 });
        block.dimension.spawnParticle("ntk:barked_2_particle", { x: block.location.x, y: block.location.y, z: block.location.z });
        if (block.permutation.matches("ntk:stripped_mosshroom_hyphae")) {
          block.setPermutation(BlockPermutation.resolve("ntk:mosshroom_hyphae", blockAllState ));
          return;
        }
        if (block.permutation.matches("ntk:stripped_mosshroom_stem")) {
          block.setPermutation(BlockPermutation.resolve("ntk:mosshroom_stem", blockAllState ));
          return;
        }
      });
    }
    else return;
  }
  else return;
});
