// Custom square function for code readability
export const sqr = (num) => {
  return num * num;
};

// Custom distance function for determining the distance between two vectors (entities, blocks, etc)
export const distance3D = (v1, v2) => {
  return Math.sqrt(sqr(v1.x - v2.x) + sqr(v1.y - v2.y) + sqr(v1.z - v2.z));
};

// Custom vector magnitude function
export const magnitude3D = (vector) => {
  return Math.sqrt(sqr(vector.x) + sqr(vector.y));
};

// Custom vector normalize function
export const normalizeVector3D = (vector) => {
  return divideVector(vector, magnitude3D(vector));
};

// Generate a 3D vector from 2 provided angles
export const vector3DFromAngles = (alpha, beta) => {
  let x = Math.cos(alpha) * Math.cos(beta);
  let z = Math.sin(alpha) * Math.cos(beta);
  let y = Math.sin(beta);
  return { x, y, z };
};

// Rotate a 3D vector around the Y axis
export const rotateVectorY = function (vector, angle) {
  let previousX = vector.x;
  let previousZ = vector.z;

  let x = Math.cos(angle) * previousX - Math.sin(angle) * previousZ;
  let y = vector.y;
  let z = Math.sin(angle) * previousX + Math.cos(angle) * previousZ;

  return { x, y, z };
};

// Gives back X and Y facing angles from a 3D vector
export const anglesFromVector3D = (vector) => {
  let x = (Math.atan2(vector.x, vector.z) * -180) / Math.PI;
  let y = (Math.atan(vector.y / Math.sqrt(sqr(vector.x) + sqr(vector.z))) * 180) / Math.PI;
  return { x, y };
};

// Get vector 2D angle
export const vectorAngle2D = (vector) => {
  return (Math.atan(vector.y / vector.x) * 180) / Math.PI;
};

// Get vector 2D angle
export const vectorAngleOffset2D = (v1, v2) => {
  return (Math.atan2(v2.y * v1.x - v2.x * v1.y, v2.x * v1.x + v2.y * v1.y) * 180) / Math.PI;
};

// Subtracts one vector from another; essentially returns a vector that points from one vector to the other
export const subtractVectors = (v1, v2) => {
  return { x: v1.x - v2.x, y: v1.y - v2.y, z: v1.z - v2.z };
};

// Adds one vector to another
export const addVectors = (v1, v2) => {
  return { x: v1.x + v2.x, y: v1.y + v2.y, z: v1.z + v2.z };
};

// Returns the cross product of two vectors
export const crossProduct = (v1, v2) => {
  let x = v1.y * v2.z - v1.z * v2.y;
  let y = v1.z * v2.x - v1.x * v2.z;
  let z = v1.x * v2.y - v1.y * v2.x;
  return { x, y, z };
};

// Multiplies the vector by the number
export const multiplyVector = (vector, number) => {
  return { x: vector.x * number, y: vector.y * number, z: vector.z * number };
};

// Divides the vector by the number
export const divideVector = (vector, number) => {
  return { x: vector.x / number, y: vector.y / number, z: vector.z / number };
};

// Custom lerp function
export const lerp = (value1, value2, step) => {
  // Handle bad arguments
  if (value1 === undefined || value2 === undefined || step === undefined) error("Invalid arguments for lerp function");
  else {
    return (1 - step) * value1 + step * value2;
  }
};

// Custom clamp function

export const clamp = (value, min, max) => {
  if (value < min) return min;
  else if (value > max) return max;
  else return value;
};

// Custom map function

export const map = (num, a, b, c, d) => {
  return ((num - a) / (b - a)) * (d - c) + c;
};

// Random string generator

export const randomString = (length) => {
  const chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

  let string = "";

  for (let i = 0; i < length; i++) string += chars[Math.floor(Math.random() * chars.length)];

  return string;
};

// UTILITY

export const CarpetBlocks = [
  "minecraft:black_carpet",
  "minecraft:blue_carpet",
  "minecraft:brown_carpet",
  "minecraft:cyan_carpet",
  "minecraft:gray_carpet",
  "minecraft:green_carpet",
  "minecraft:light_blue_carpet",
  "minecraft:light_gray_carpet",
  "minecraft:lime_carpet",
  "minecraft:magenta_carpet",
  "minecraft:orange_carpet",
  "minecraft:pink_carpet",
  "minecraft:purple_carpet",
  "minecraft:red_carpet",
  "minecraft:white_carpet",
  "minecraft:yellow_carpet",
  "minecraft:moss_carpet",
  "minecraft:snow_layer",
  "minecraft:activator_rail",
  "minecraft:detector_rail",
  "minecraft:golden_rail",
  "minecraft:rail",
  "minecraft:acacia_pressure_plate",
  "minecraft:bamboo_pressure_plate",
  "minecraft:birch_pressure_plate",
  "minecraft:cherry_pressure_plate",
  "minecraft:crimson_pressure_plate",
  "minecraft:dark_oak_pressure_plate",
  "minecraft:heavy_weighted_pressure_plate",
  "minecraft:jungle_pressure_plate",
  "minecraft:light_weighted_pressure_plate",
  "minecraft:mangrove_pressure_plate",
  "minecraft:polished_blackstone_pressure_plate",
  "minecraft:spruce_pressure_plate",
  "minecraft:stone_pressure_plate",
  "minecraft:warped_pressure_plate",
  "minecraft:wooden_pressure_plate",
  "minecraft:acacia_button",
  "minecraft:bamboo_button",
  "minecraft:birch_button",
  "minecraft:cherry_button",
  "minecraft:crimson_button",
  "minecraft:dark_oak_button",
  "minecraft:jungle_button",
  "minecraft:mangrove_button",
  "minecraft:polished_blackstone_button",
  "minecraft:spruce_button",
  "minecraft:stone_button",
  "minecraft:warped_button",
  "minecraft:wooden_button",
  "minecraft:redstone_wire",
  "minecraft:powered_comparator",
  "minecraft:unpowered_comparator",
  "minecraft:powered_repeater",
  "minecraft:unpowered_repeater",
  "minecraft:crimson_roots",
  "minecraft:sea_pickle",
  "minecraft:short_grass",
  "minecraft:tall_grass",
  "minecraft:fern",
  "minecraft:large_fern",
  "minecraft:turtle_egg",
  "minecraft:crimson_fungus",
  "minecraft:warped_fungus",
  "minecraft:brown_mushroom",
  "minecraft:red_mushroom",
  "minecraft:fire",
  "minecraft:soul_fire",
  "minecraft:campfire",
  "minecraft:soul_campfire",
];

export const FoodList = {
  apple_pie: {
    lore: ["§r§9Combine Items", "§r§7Speed (2:00)"],
    effect: {
      name: "speed",
      duration: 120,
      amplifier: 0,
    },
  },
  cheese: {
    lore: ["§r§9Combine Items", "§r§7Resistance (2:00)"],
    effect: {
      name: "resistance",
      duration: 120,
      amplifier: 0,
    },
  },
  chips: {
    lore: ["§r§9Combine Items", "§r§7Absorption (1:00)"],
    effect: {
      name: "absorption",
      duration: 60,
      amplifier: 0,
    },
  },
  fish_and_chips: {
    lore: ["§r§9Combine Items", "§r§7Water Breathing (2:00)"],
    effect: {
      name: "water_breathing",
      duration: 120,
      amplifier: 0,
    },
  },
  kebab: {
    lore: ["§r§9Combine Items", "§r§7Strength II (2:00)"],
    effect: {
      name: "strength",
      duration: 120,
      amplifier: 1,
    },
  },
  mac_and_cheese: {
    lore: ["§r§9Combine Items", "§r§7Regeneration (0:30)"],
    effect: {
      name: "regeneration",
      duration: 30,
      amplifier: 0,
    },
  },
  pasta: {
    lore: ["§r§9Combine Items", "§r§7Strength (1:00)"],
    effect: {
      name: "strength",
      duration: 60,
      amplifier: 0,
    },
  },
  peking_duck: {
    lore: ["§r§9Combine Items", "§r§7Strength II (0:30)"],
    effect: {
      name: "strength",
      duration: 30,
      amplifier: 1,
    },
  },
  pizza: {
    lore: ["§r§9Combine Items", "§r§7Jump Boost (1:00)"],
    effect: {
      name: "jump_boost",
      duration: 60,
      amplifier: 0,
    },
  },
  pizza_extra_el_formaggio: {
    lore: ["§r§9Combine Items", "§r§7Speed II (1:00)"],
    effect: {
      name: "speed",
      duration: 60,
      amplifier: 1,
    },
  },
  sushi: {
    lore: ["§r§9Combine Items", "§r§7Instant Health"],
    effect: {
      name: "instant_health",
      duration: 1,
      amplifier: 0,
    },
  },
};

export const EntityBabyList = {
  bee: "minecraft:entity_born",
  cat: "minecraft:entity_born",
  chicken: "minecraft:entity_born",
  cow: "minecraft:entity_born",
  donkey: "minecraft:entity_born",
  fox: "minecraft:entity_born",
  hoglin: "minecraft:entity_born",
  horse: "minecraft:entity_born",
  mooshroom: "minecraft:entity_born",
  mule: "minecraft:entity_born",
  panda: "minecraft:entity_born",
  polar_bear: "minecraft:entity_born",
  ocelot: "minecraft:entity_born",
  pig: "minecraft:entity_born",
  piglin: "minecraft:entity_born",
  rabbit: "minecraft:entity_born",
  sheep: "minecraft:entity_born",
  strider: "minecraft:entity_born",
  turtle: "minecraft:entity_born",
  goat: "minecraft:entity_born",
  villager: "minecraft:entity_born",
  wolf: "minecraft:entity_born",
  zombie: "minecraft:as_baby",
  drowned: "minecraft:as_baby",
};

export const non_solid_blocks = [
  "minecraft:orange_tulip",
  "minecraft:white_tulip",
  "minecraft:red_tulip",
  "minecraft:pink_tulip",
  "minecraft:lily_of_the_valley",
  "minecraft:sunflower",
  "minecraft:rose_bush",
  "minecraft:activator_rail",
  "minecraft:bamboo",
  "minecraft:banner",
  "minecraft:blue_orchid",
  "minecraft:standing_banner",
  "minecraft:beetroot_seeds",
  "minecraft:carpet",
  "minecraft:carrots",
  "minecraft:chorus_flower",
  "minecraft:chorus_plant",
  "minecraft:cobweb",
  "minecraft:cocoa",
  "minecraft:cornflower",
  "minecraft:coral",
  "minecraft:coral_fan",
  "minecraft:dead_bush",
  "minecraft:detector_rail",
  "minecraft:flower",
  "minecraft:flower_pot",
  "minecraft:frogspawn",
  "minecraft:fungus",
  "minecraft:glow_berries",
  "minecraft:glow_lichen",
  "minecraft:grass",
  "minecraft:hanging_roots",
  "minecraft:head",
  "minecraft:kelp",
  "minecraft:lever",
  "minecraft:light_block",
  "minecraft:lily_pad",
  "minecraft:oak_sapling",
  "minecraft:lilac",
  "minecraft:deadbush",
  "minecraft:cherry_sapling",
  "minecraft:birch_sapling",
  "minecraft:jungle_sapling",
  "minecraft:acacia_sapling",
  "minecraft:dark_oak_sapling",
  "minecraft:spruce_sapling",
  "minecraft:mangrove_propagule",
  "minecraft:melon_seeds",
  "minecraft:moss_carpet",
  "minecraft:mushroom",
  "minecraft:torchflower",
  "minecraft:crimson_roots",
  "minecraft:warped_roots",
  "minecraft:wither_rose",
  "minecraft:nether_sprouts",
  "minecraft:nether_wart",
  "minecraft:pink_petals",
  "minecraft:poppy",
  "minecraft:pitcher_plant",
  "minecraft:pitcher_pod",
  "minecraft:potato",
  "minecraft:powered_rail",
  "minecraft:pressure_plate",
  "minecraft:pumpkin_stem",
  "minecraft:rail",
  "minecraft:redstone_comparator",
  "minecraft:redstone",
  "minecraft:redstone_repeater",
  "minecraft:redstone_torch",
  "minecraft:roots",
  "minecraft:sapling",
  "minecraft:sculk_vein",
  "minecraft:sea_pickle",
  "minecraft:seagrass",
  "minecraft:shrub",
  "minecraft:sign",
  "minecraft:small_dripleaf",
  "minecraft:spore_blossom",
  "minecraft:string",
  "minecraft:structure_void",
  "minecraft:sugar_cane",
  "minecraft:sweet_berries",
  "minecraft:torch",
  "minecraft:torchflower_seeds",
  "minecraft:tripwire_hook",
  "minecraft:turtle_egg",
  "minecraft:twisting_vines",
  "minecraft:vine",
  "minecraft:weeping_vines",
  "minecraft:wheat",
  "minecraft:large_fern",
  "minecraft:fern",
  "minecraft:brown_mushroom",
  "minecraft:red_mushroom",
  "minecraft:dandelion",
  "minecraft:peony",
  "minecraft:sweet_berry_bush",
  "minecraft:short_grass"
];
