export function getAdjacentBlock(block, side) {
  const facing =
    block.permutation.getState("minecraft:cardinal_direction") || "north";

  const relativeOffsets = {
    left: {
      north: { x: -1, y: 0, z: 0 },
      south: { x: 1, y: 0, z: 0 },
      east: { x: 0, y: 0, z: -1 },
      west: { x: 0, y: 0, z: 1 },
    },
    right: {
      north: { x: 1, y: 0, z: 0 },
      south: { x: -1, y: 0, z: 0 },
      east: { x: 0, y: 0, z: 1 },
      west: { x: 0, y: 0, z: -1 },
    },
    above: { x: 0, y: 1, z: 0 },
    below: { x: 0, y: -1, z: 0 },
  };

  let offset = null;

  if (side === "above" || side === "below") {
    offset = relativeOffsets[side];
  } else if (relativeOffsets[side] && relativeOffsets[side][facing]) {
    offset = relativeOffsets[side][facing];
  }

  if (!offset) return null;

  return block.offset(offset);
}

export function playerEquippedItem(player) {
  const equipment = player.getComponent("equippable");
  const selectedItem = equipment?.getEquipment("Mainhand");

  return { selectedItem, equipment };
}

const handEquipped = [
  "minecraft:stick",
  "minecraft:breeze_rod",
  "minecraft:blaze_rod",
  "minecraft:bone",
  "minecraft:nautilus_shell",
  "minecraft:diamond_axe",
  "minecraft:lightning_rod",
];

const blockEquipped = [
  "minecraft:glass",
  "minecraft:obsidian",
  "minecraft:stone",
  "minecraft:oak_planks",
  "minecraft:spruce_planks",
  "minecraft:birch_planks",
  "minecraft:jungle_planks",
  "minecraft:acacia_planks",
  "minecraft:dark_oak_planks",
  "minecraft:mangrove_planks",
  "minecraft:cherry_planks",
  "minecraft:pale_oak_planks",
  "minecraft:bamboo_planks",
  "minecraft:crimson_planks",
  "minecraft:warped_planks",
  "minecraft:scaffolding",
  "minecraft:glass_pane",
  "minecraft:furnace",
  "minecraft:copper_block",
  "minecraft:iron_block",
  "minecraft:gold_block",
  "minecraft:emerald_block",
  "minecraft:diamond_block",
  "pb_struct:transmutation_powder_block",
  "minecraft:snow",
  "minecraft:shroomlight",
  "minecraft:chiseled_sandstone",
  "minecraft:dirt",
  "minecraft:grass_block",
  "minecraft:iron_ore",
  "minecraft:dispenser",
  "minecraft:cobblestone_wall",
  "minecraft:acacia_log",
  "minecraft:gravel",
  "minecraft:mycelium",
];

export function mbEntityGiveItem(mbEntity, selectedItem) {
  const entityInv = mbEntity.getComponent("minecraft:inventory").container;
  const addItem = selectedItem.clone();
  addItem.amount = 1;
  entityInv.addItem(addItem);
  mbEntity.runCommandAsync(
    `/replaceitem entity @s slot.weapon.mainhand 1 ${selectedItem.typeId}`
  );
  mbEntity.setDynamicProperty("pb_gat_altar_item", selectedItem.typeId);

  mbEntity.setProperty("pb_gat:has_hand_equipped", false);
  mbEntity.setProperty("pb_gat:has_block_equipped", false);

  if (handEquipped.includes(selectedItem.typeId)) {
    mbEntity.setProperty("pb_gat:has_hand_equipped", true);
  }
  if (blockEquipped.includes(selectedItem.typeId)) {
    mbEntity.setProperty("pb_gat:has_block_equipped", true);
  }
  if (selectedItem.typeId === "minecraft:dragon_head" || selectedItem.typeId === "minecraft:wither_skeleton_skull") {
    mbEntity.setProperty("pb_gat:has_dragon_head_equipped", true);
  }
  if (selectedItem.typeId === "minecraft:heavy_core") {
    mbEntity.setProperty("pb_gat:has_heavy_core_equipped", true);
  }
  if (selectedItem.typeId === "minecraft:crossbow") {
    mbEntity.setProperty("pb_gat:has_crossbow_equipped", true);
  }
  if (selectedItem.typeId === "minecraft:lightning_rod") {
    mbEntity.setProperty("pb_gat:has_lightning_rod_equipped", true);
  }
}
export function consumeItem(player, selectedItem, equipment) {
  // Handle item consumption
  if (player.getGameMode() !== "creative") {
    if (selectedItem.amount > 1) {
      selectedItem.amount -= 1;
      equipment.setEquipment("Mainhand", selectedItem);
    } else if (selectedItem.amount === 1) {
      equipment.setEquipment("Mainhand", undefined); // Clear the slot if only 1 item left
    }
  }
}
export function getMultiblockEntity(block, family) {
  return block.dimension.getEntities({
    location: {
      x: block.location.x + 0.5,
      y: block.location.y,
      z: block.location.z + 0.5,
    },
    maxDistance: 1.2,
    closest: 1,
    families: [family],
  })[0];
}
export function getBlockFromLocation(dimension, location) {
  if (!dimension || !location) return undefined;
  return dimension.getBlock({
    x: Math.floor(location.x),
    y: Math.floor(location.y),
    z: Math.floor(location.z),
  });
}
export function mbEntityDropItem(mbEntity, block, player) {
  const entityInv = mbEntity.getComponent("minecraft:inventory")?.container;
  const dropItem = entityInv?.getItem(0);
  if (!dropItem) return;

  const entityPos = mbEntity.location;
  const playerPos = player.location;

  // Calculate direction from entity to player
  let dx = playerPos.x - entityPos.x;
  let dz = playerPos.z - entityPos.z;

  const length = Math.sqrt(dx * dx + dz * dz);

  // Prevent division by zero
  if (length === 0) {
    dx = 0;
    dz = 0;
  } else {
    dx /= length;
    dz /= length;
  }

  // Offset 1 block in the direction of the player
  const dropLocation = {
    x: entityPos.x + dx,
    y: entityPos.y,
    z: entityPos.z + dz,
  };

  // Drop the item
  mbEntity.dimension.spawnItem(dropItem, dropLocation);

  // Reset block and remove entity
  if (block) {
    block.setPermutation(
      block.permutation.withState("pb_gat:mb_has_item", false)
    );
  }

  mbEntity.remove();
}
export function mbEntityBlockBroken(mbEntity, block) {
  const entityInv = mbEntity.getComponent("minecraft:inventory")?.container;
  const dropItem = entityInv?.getItem(0);
  if (!dropItem) return;

  // Drop the item
  mbEntity.dimension.spawnItem(dropItem, mbEntity.location);
  mbEntity.remove();
}
export function getInFrontOf(block, direction) {
  const offsets = {
    south: { x: 0, z: -1 },
    north: { x: 0, z: 1 },
    west: { x: 1, z: 0 },
    east: { x: -1, z: 0 },
  };

  const offset = offsets[direction];
  if (!offset) return null;

  return {
    x: block.location.x + offset.x + 0.5,
    y: block.location.y,
    z: block.location.z + offset.z + 0.5,
  };
}
