import { world, system } from "@minecraft/server";
export default function growTreeSmall(location, fruit, playerplaced, dimension) {
  const overworld = world.getDimension("overworld");
  const rotations = ["0_degrees", "180_degrees", "270_degrees", "90_degrees"];
  const offsets = [0, 0, 1, 2];
  var treernd, randomIndex, leaf, fruitParts, fruitName, command;
  try {
    if (location != undefined) {
      dimension.runCommand(`setblock ${location.x} ${location.y} ${location.z} air replace`);
      randomIndex = Math.floor(Math.random() * 4.0);
      if (playerplaced) {
        treernd = Math.floor(Math.random() * 4.0);
      } else {
        treernd = Math.floor(Math.random() * 4.0);
      }
      leaf = fruit.replace("sapling", "leaf");
      fruitParts = fruit.split(":");
      fruitName = fruitParts.length > 1 ? fruitParts[1].split("_")[0] : fruitParts[0];
      if (treernd == 0) {
        dimension.runCommand(
          `structure load "float_fc/${fruitName}_tree_5x5_1" ${location.x - 2} ${location.y} ${location.z - 2} ${
            rotations[randomIndex]
          } none layer_by_layer 3`
        );
      } else if (treernd == 1) {
        dimension.runCommand(
          `structure load "float_fc/${fruitName}_tree_5x5_2" ${location.x - 2} ${location.y} ${location.z - 2} ${
            rotations[randomIndex]
          } none layer_by_layer 3`
        );
      } else if (treernd == 2) {
        dimension.runCommand(
          `structure load "float_fc/${fruitName}_tree_5x5_3" ${location.x - 2} ${location.y} ${location.z - 2} ${
            rotations[randomIndex]
          } none layer_by_layer 3`
        );
      } else {
        dimension.runCommand(
          `structure load "float_fc/${fruitName}_tree_5x5_4" ${location.x - 2} ${location.y} ${location.z - 2} ${
            rotations[randomIndex]
          } none layer_by_layer 3`
        );
      }
    }
  } catch (e) {}
  treernd = fruitParts = fruitName = command = undefined;
  return `event entity @e[type=${leaf},x=${location.x - 2},y=${location.y},z=${
    location.z - 2
  },r=20] float_fc:fully_grown`;
}
