import { world, system } from "@minecraft/server";
export default function growTreeMedium(location, fruit, playerplaced, dimension) {
  const overworld = world.getDimension("overworld");
  const rotations = ["0_degrees", "180_degrees", "270_degrees", "90_degrees"];
  const offsets = [0.0, 0.0, 1.0, 2.0];
  var treernd, fruitParts, fruitName, returnCommand;
  returnCommand = null;
  if (location != undefined) {
    try {
      dimension.runCommand(`setblock ${location.x} ${location.y} ${location.z} air replace`);
      let randomIndex = 0;
      randomIndex = Math.floor(Math.random() * 4.0);
      if (playerplaced) {
        treernd = Math.floor(Math.random() * 4.0);
      } else {
        treernd = Math.floor(Math.random() * 4.0);
      }
      let leaf = fruit.replace("sapling", "leaf");
      fruitParts = fruit.split(":");
      fruitName = fruitParts.length > 1 ? fruitParts[1].split("_")[0] : fruitParts[0];
      if (fruitName == "maple") {
        leaf = "float_fc:maple_sap";
      }
      if (treernd == 0) {
        dimension.runCommand(
          `structure load "float_fc/${fruitName}_tree_9x9_1" ${location.x - 4} ${location.y - 1} ${location.z - 4} ${
            rotations[randomIndex]
          } none layer_by_layer 3`
        );
        returnCommand = `event entity @e[type=${leaf},x=${location.x - 4},y=${location.y - 1},z=${
          location.z - 4
        },r=30] float_fc:fully_grown`;
      } else if (treernd == 1) {
        dimension.runCommand(
          `structure load "float_fc/${fruitName}_tree_9x9_2" ${location.x - 4} ${location.y - 1} ${location.z - 4} ${
            rotations[randomIndex]
          } none layer_by_layer 3`
        );
        returnCommand = `event entity @e[type=${leaf},x=${location.x - 4},y=${location.y - 1},z=${
          location.z - 4
        },r=30] float_fc:fully_grown`;
      } else if (treernd == 2) {
        dimension.runCommand(
          `structure load "float_fc/${fruitName}_tree_11x11_3" ${location.x - 5} ${location.y - 1} ${location.z - 5} ${
            rotations[randomIndex]
          } none layer_by_layer 3`
        );
        returnCommand = `event entity @e[type=${leaf},x=${location.x - 5},y=${location.y - 1},z=${
          location.z - 5
        },r=40] float_fc:fully_grown`;
      } else {
        dimension.runCommand(
          `structure load "float_fc/${fruitName}_tree_11x11_4" ${location.x - 5} ${location.y - 1} ${location.z - 5} ${
            rotations[randomIndex]
          } none layer_by_layer 3`
        );
        returnCommand = `event entity @e[type=${leaf},x=${location.x - 5},y=${location.y - 1},z=${
          location.z - 5
        },r=40] float_fc:fully_grown`;
      }
      treernd = randomIndex = leaf = fruitParts = fruitName = undefined;
      return returnCommand;
    } catch (e) {}
  }
}
