// minecraftUtils.js
// Minecraft-specific utility functions from tsunamiUtilityFunctions.js and tsunamiVector3Functions.js
import { Entity, world } from "@minecraft/server";

export function applyImpulse(player, vector) {
  const { x, y, z } = vector;
  const horizontal = Math.sqrt(x * x + z * z) * 2.0;
  player.applyKnockback(x, z, horizontal, y);
}
export function styledMessage(message = "", player, messageStyle = "default") {
  const display = player.onScreenDisplay;
  const styles = {
    default: {
      type: "action_bar",
      styledMessage: message,
    },
    error: {
      type: "action_bar",
      styledMessage: `§c§l[!]: §r${message}`,
    },
    warning: {
      type: "action_bar",
      styledMessage: `§g§l[!]§r: §r${message}`,
    },
    hint: {
      type: "action_bar",
      styledMessage: `§b§l[!]: §r${message}`,
    },
    notification: {
      type: "action_bar",
      styledMessage: `§d§l[!]§r §r${message}`,
    },
    big_warning: {
      type: "title",
      styledMessage: `§d§l[!]§r: §r${message}`,
      titleOptions: {
        stayDuration: 40,
        fadeInDuration: 10,
        fadeOutDuration: 10,
      },
    },
  };
  const styleToUse = styles[messageStyle] || styles.default;
  const { type, styledMessage } = styleToUse;
  switch (type) {
    case "announcement":
      world.sendMessage(styledMessage);
      break;
    case "message":
      player.sendMessage(styledMessage);
      break;
    case "action_bar":
      display.setActionBar(styledMessage);
      break;
    case "title":
      display.setTitle(styledMessage, styleToUse.titleOptions);
      break;
  }
}
export function clearTags(entity) {
  if (!(entity instanceof Entity)) {
    world.sendMessage("Can't clear tags: input not an entity");
  }
  while (entity.getTags().length > 0) {
    entity.removeTag(entity.getTags()[0]);
  }
  entity.sendMessage("Tags cleared!");
}
export function isSolid(block) {
  const isSolid = block?.dimension.getBlockFromRay(
    block.center(),
    { x: 0, y: 0.1, z: 0 },
    {
      maxDistance: 1,
      includeLiquidBlocks: true,
      includePassableBlocks: false,
    }
  );
  return isSolid ? true : false;
}
export function isAiry(block) {
  const nonSolidBlocksSet = new Set([
    "minecraft:activator_rail",
    "minecraft:air",
    "minecraft:bamboo",
    "minecraft:banner",
    "minecraft:beetroot",
    "minecraft:carpet",
    "minecraft:carrot",
    "minecraft:chorus_flower",
    "minecraft:chorus_plant",
    "minecraft:cobweb",
    "minecraft:cocoa",
    "minecraft:dead_bush",
    "minecraft:detector_rail",
    "minecraft:end_gateway",
    "minecraft:end_portal",
    "minecraft:fire",
    "minecraft:flower",
    "minecraft:flower_pot",
    "minecraft:frogspawn",
    "minecraft:fungus",
    "minecraft:glow_berries",
    "minecraft:glow_lichen",
    "minecraft:short_grass",
    "minecraft:tall_grass",
    "minecraft:hanging_roots",
    "minecraft:kelp",
    "minecraft:ladder",
    "minecraft:lever",
    "minecraft:light_block",
    "minecraft:lily_pad",
    "minecraft:mangrove_propagule",
    "minecraft:melon_seeds",
    "minecraft:moss_carpet",
    "minecraft:mushroom",
    "minecraft:nether_portal",
    "minecraft:nether_sprouts",
    "minecraft:nether_wart",
    "minecraft:pink_petals",
    "minecraft:pitcher_plant",
    "minecraft:pitcher_pod",
    "minecraft:potato",
    "minecraft:powder_snow",
    "minecraft:snow_layer",
    "minecraft:powered_rail",
    "minecraft:pressure_plate",
    "minecraft:pumpkin_seeds",
    "minecraft:rail",
    "minecraft:redstone_comparator",
    "minecraft:redstone_dust",
    "minecraft:redstone_repeater",
    "minecraft:redstone_torch",
    "minecraft:roots",
    "minecraft:sapling",
    "minecraft:sculk_vein",
    "minecraft:shrub",
    "minecraft:sign",
    "minecraft:small_dripleaf",
    "minecraft:snow",
    "minecraft:sporepuff",
    "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_seeds",
  ]);
  if (block) {
    return block.isAir || nonSolidBlocksSet.has(block.typeId) || block.isLiquid;
  } else {
    return false;
  }
}
export function debugMessage(message) {
  if (world.getDynamicProperty("tsu_nat:debug")) {
    world.sendMessage(message);
  }
}
export function debugWarn(message) {
  if (world.getDynamicProperty("tsu_nat:debug")) {
    console.warn(message);
  }
}
export function blockToChunkCoords(coords, chunkSize = 16) {
  const x = Math.floor(coords.x / chunkSize);
  const y = Math.floor(coords.z / chunkSize);
  const z = Math.floor(coords.y / chunkSize);
  return { x, y, z };
}
