import { getOrientation, getOrientationX } from "../../functions";

const shovelDestroyList = [
  "grass",
  "gravel",
  "dirt",
  "snow",
  "sand"
];

export default {
  async execute(item, player, destroyedBlock) {
    const tag = item?.getTags()?.find(tag => tag == "break:shovel");

    if (!tag || !destroyedBlock) return;
    const { x: bX, y: bY, z: bZ } = destroyedBlock.location;
    const playerOrientationY = getOrientation(player.getRotation().y);
    const playerOrientationX = getOrientationX(player.getRotation().x);

    for (let deltaY = -1; deltaY <= 1; deltaY += 1) {
      for (let deltaXZ = -1; deltaXZ <= 1; deltaXZ += 1) {
        if (deltaXZ === 0 && deltaY === 0) continue; // Saltar la coordenada del bloque 'c'
        let x, y, z;

        if (playerOrientationX >= 0) {
          x = bX < 0 ? bX - deltaY : bX + deltaY;
          y = bY;
          z = bZ < 0 ? bZ - deltaXZ : bZ + deltaXZ;
        } else {
          x = playerOrientationY >= 3 ? bX : bX < 0 ? bX - deltaXZ : bX + deltaXZ;
          y = bY < 0 ? bY - deltaY :  bY + deltaY;
          z = playerOrientationY < 3 ? bZ : bZ < 0 ? bZ - deltaXZ : bZ + deltaXZ;
        }
        const block = player.dimension.getBlock({ x, y, z });
        
        if (!block || !shovelDestroyList.find(tag => block.hasTag(tag))) continue;
          await player.runCommandAsync(`setblock ${x} ${y} ${z} air destroy`);
      }
    }
  }
}
