import { system } from "@minecraft/server";
function whiteHole_MAIN(dimensions) {
  for (const dimension of dimensions) {
    whiteHole_trow_items(dimension, 10);
    whiteHole_age(dimension, 10);
  }
}
function whiteHole_trow_items(dimension, interval) {
  system.runInterval(() => {
    dimension
      .getEntities({
        families: ["white_hole"],
      })
      .forEach((whiteHole) => {
        let age = whiteHole.getProperty("property:age");
        whiteHole.runCommand(
          '/loot spawn ~0.5 ~0.5 ~0.5 loot "tvf/random_events/white_hole"'
        );
        for (const item of dimension.getEntities({
          type: "item",
          location: whiteHole.location,
          maxDistance: 1.5,
        })) {
          const randomPoint = {
            x: Math.random() * 3 * randomOneOrMinusOne() + whiteHole.location.x,
            y: Math.random() * 3 + whiteHole.location.y,
            z: Math.random() * 3 * randomOneOrMinusOne() + whiteHole.location.z,
          };
          const vector = {
            x: randomPoint.x - whiteHole.location.x,
            y: randomPoint.y - whiteHole.location.y,
            z: randomPoint.z - whiteHole.location.z,
          };
          const vector_length = Math.sqrt(
            vector.x * vector.x + vector.y * vector.y + vector.z * vector.z
          );
          let force = 0.5;
          const vector_norm = {
            x: vector.x / vector_length,
            y: vector.y / vector_length,
            z: vector.z / vector_length,
          };
          item.applyImpulse({
            x: vector_norm.x * force,
            y: vector_norm.y * force,
            z: vector_norm.z * force,
          });
        }
      });
  }, interval);
}
function whiteHole_age(dimension, interval) {
  system.runInterval(() => {
    dimension
      .getEntities({
        families: ["white_hole"],
      })
      .forEach((whiteHole) => {
        const age = whiteHole.getProperty("property:age");
        if (age < 100) {
          whiteHole.setProperty("property:age", age + 1);
          whiteHole.getComponent("minecraft:health").setCurrentValue(100 - age);
        } else whiteHole.kill();
      });
  }, interval);
}
function randomOneOrMinusOne() {
  let mod;
  if (Math.floor(Math.random() * 2) == 1) mod = 1;
  else mod = -1;
  return mod;
}
export { whiteHole_MAIN };
