import { EquipmentSlot, ItemStack, system } from "@minecraft/server";
const placeLanternComponent = {
  beforeOnPlayerPlace(e) {
    const { block, dimension, player } = e;
    if (!player)
      return;
    const inventory = player.getComponent("inventory");
    const heldItem = inventory.container.getItem(player.selectedSlotIndex);
    const loc = block.location;
    if (heldItem) {
      e.cancel = true;
      const equipment = player.getComponent("equippable");
      const selectedItem = equipment.getEquipment("Mainhand");
      system.runTimeout(() => {
        if (player.getGameMode() !== "creative") {
          if (selectedItem.amount > 1) {
            selectedItem.amount -= 1;
            equipment.setEquipment("Mainhand", selectedItem);
          } else {
            equipment.setEquipment("Mainhand", void 0);
          }
        }
        if (canPlaceLantern(dimension, loc)) {
          PlaceLantern(dimension, loc);
        } else {
          player.runCommand(
            `title @s actionbar There are blocks in the way, 3 blocks of vertical space required`
          );
        }
      });
    }
  }
};
const toggleLanternComponent = {
  onPlayerInteract(e) {
    const { block, dimension, player } = e;
    const equipment = player.getComponent("equippable");
    const selectedItem = equipment.getEquipment(EquipmentSlot.Mainhand);
    if (selectedItem) {
      if (selectedItem.typeId === "maca_probuilder:lantern_pole") {
        extendLantern(block, player, dimension);
        return;
      }
    }
    if (block.typeId.startsWith("maca_probuilder:lantern")) {
      const lanterPOS = getTopPosition(block.location, dimension, block);
      const topLanternBlock = dimension.getBlock(lanterPOS);
      if (topLanternBlock.typeId === "maca_probuilder:lantern") {
        const currentState = topLanternBlock.permutation.getState("maca:is_lit");
        const newState = !currentState;
        topLanternBlock.setPermutation(
          topLanternBlock.permutation.withState("maca:is_lit", newState)
        );
        player.runCommand(
          `title @s actionbar Lantern is now ${newState ? "lit" : "unlit"}`
        );
      } else {
        console.warn("Top lantern block not found");
      }
    }
  }
};
const breakLanternComponent = {
  onPlayerDestroy(e) {
    const { block, destroyedBlockPermutation, dimension, player } = e;
    if (destroyedBlockPermutation.type.id.startsWith("maca_probuilder:lantern")) {
      const basePosition = getBasePosition(
        block.location,
        dimension,
        destroyedBlockPermutation
      );
      const topPosition = getTopPosition(
        block.location,
        dimension,
        destroyedBlockPermutation
      );
      for (let y = 0; y < 8; y++) {
        const pos = {
          x: basePosition.x,
          y: basePosition.y + y,
          z: basePosition.z
        };
        const blockToBreak = dimension.getBlock(pos);
        if (blockToBreak.typeId.startsWith("maca_probuilder:lantern")) {
          dimension.getBlock(pos).setType("minecraft:air");
        }
      }
      blockDrops(player, basePosition, topPosition);
    }
  }
};
function blockDrops(player, basePosition, topPosition) {
  if (player.getGameMode() === "creative") {
    return;
  }
  const heightDifference = topPosition.y - basePosition.y;
  let polesToDrop = 0;
  if (heightDifference > 2) {
    polesToDrop = heightDifference - 2;
  }
  if (polesToDrop > 0) {
    const poleItem = new ItemStack("maca_probuilder:lantern_pole", polesToDrop);
    const dropLocation = {
      x: basePosition.x + 0.5,
      y: basePosition.y + 0.5,
      z: basePosition.z + 0.5
    };
    player.dimension.spawnItem(poleItem, dropLocation);
  }
}
function canPlaceLantern(dimension, loc) {
  const checkPositions = getPlacePositions(loc);
  return checkPositions.every(
    (pos) => dimension.getBlock(pos).typeId === "minecraft:air"
  );
}
function PlaceLantern(dimension, loc) {
  const blockTypes = [
    "maca_probuilder:lantern_base",
    "maca_probuilder:lantern_pole",
    "maca_probuilder:lantern"
  ];
  const adjustedPositions = getPlacePositions(loc);
  adjustedPositions.forEach((pos, index) => {
    const block = dimension.getBlock(pos);
    if (blockTypes[index] !== "minecraft:air") {
      block.setType(blockTypes[index]);
    }
  });
}
function getPlacePositions(loc) {
  const x = loc.x ?? 0;
  const y = loc.y ?? 0;
  const z = loc.z ?? 0;
  return [
    { x, y, z },
    { x, y: y + 1, z },
    { x, y: y + 2, z }
  ];
}
function getBasePosition(brokenBlockLocation, dimension, block) {
  let currentBlock = block;
  let yOffset = 0;
  while (currentBlock && yOffset < 7) {
    if (currentBlock.type.id === "maca_probuilder:lantern_base") {
      return {
        x: brokenBlockLocation.x,
        y: brokenBlockLocation.y - yOffset,
        z: brokenBlockLocation.z
      };
    }
    yOffset++;
    currentBlock = dimension.getBlock({
      x: brokenBlockLocation.x,
      y: brokenBlockLocation.y - yOffset,
      z: brokenBlockLocation.z
    });
  }
  return {
    x: brokenBlockLocation.x,
    y: brokenBlockLocation.y,
    z: brokenBlockLocation.z
  };
}
function getTopPosition(loc, dimension, block) {
  let currentBlock = block;
  let yOffset = 0;
  while (currentBlock && yOffset < 8) {
    if (currentBlock.type.id === "maca_probuilder:lantern") {
      return {
        x: loc.x,
        y: loc.y + yOffset,
        z: loc.z
      };
    }
    yOffset++;
    currentBlock = dimension.getBlock({
      x: loc.x,
      y: loc.y + yOffset,
      z: loc.z
    });
  }
  return {
    x: loc.x,
    y: loc.y,
    z: loc.z
  };
}
function extendLantern(block, player, dimension) {
  const lanternLoc = getTopPosition(block.location, dimension, block);
  const baseLoc = getBasePosition(block.location, dimension, block);
  if (dimension.getBlock({ x: lanternLoc.x, y: lanternLoc.y + 1, z: lanternLoc.z }).typeId !== "minecraft:air")
    return;
  if (lanternLoc.y >= baseLoc.y + 7) {
    player.runCommand(
      `title @s actionbar max height of 7 blocks has been reached`
    );
  } else {
    dimension.getBlock({ x: lanternLoc.x, y: lanternLoc.y + 1, z: lanternLoc.z }).setType("maca_probuilder:lantern");
    dimension.getBlock(lanternLoc).setType("maca_probuilder:lantern_pole");
    if (player.getGameMode() !== "creative") {
      const equipment = player.getComponent("equippable");
      const selectedItem = equipment.getEquipment("Mainhand");
      if (selectedItem) {
        if (selectedItem.amount > 1) {
          selectedItem.amount -= 1;
          equipment.setEquipment("Mainhand", selectedItem);
        } else {
          equipment.setEquipment("Mainhand", void 0);
        }
      }
    }
  }
}
export {
  breakLanternComponent,
  placeLanternComponent,
  toggleLanternComponent
};
