import { world, ItemStack } from "@minecraft/server";

world.afterEvents.playerPlaceBlock.subscribe((event) => {
  const { player, block } = event;
  if (!player || !block) return;
  if ((block.typeId === "minecraft:web") &&
    block.isWaterlogged) {
    block.setType("minecraft:air");
    block.setType("minecraft:air");
    block.setType("minecraft:web");
  }
  const Enabled = world.getDynamicProperty("dupe") === true;
  if (Enabled && block.typeId === "minecraft:trip_wire") {
    const invComp = player.getComponent("minecraft:inventory");
    const inv = invComp?.container;
    inv.addItem(new ItemStack("minecraft:string", 2));
    if (block.isWaterlogged) {
       const { x, y, z } = block.location;
      block.dimension.runCommand(`/setblock ${x} ${y} ${z} air destroy`);
    }
  }
});

world.afterEvents.playerInteractWithBlock.subscribe(({ block, player, beforeItemStack }) => {
  const web = world.getDynamicProperty("web");
  const doorEnabled = world.getDynamicProperty("door") === true;
  if (!block || !player) return;
  const dimension = player.dimension;
  const { x, y, z } = player.location;
  // Only get blocks if the position is in the world bounds (y: 0-319 for most dimensions)
  function getBlockSafe(dimension, pos) {
    if (pos.y < 0 || pos.y > 319) return null;
    return dimension.getBlock(pos);
  }
  const blockUp = getBlockSafe(block.dimension, { x: block.x, y: block.y + 1, z: block.z });
  const blockDown = getBlockSafe(block.dimension, { x: block.x, y: block.y - 1, z: block.z });
  if (
    beforeItemStack?.typeId === "minecraft:water_bucket" &&
    block.typeId === "minecraft:web" &&
    web === 1 &&
    player?.dimension?.id !== "minecraft:nether"
  ) {block.setType("minecraft:flowing_water");}
  if (
    beforeItemStack?.typeId === "minecraft:water_bucket" &&
    block.typeId === "minecraft:web" &&
    web === 0 &&
    player?.dimension?.id !== "minecraft:nether"
  ) {
    if (blockUp && blockUp.typeId === "minecraft:air" && blockDown && blockDown.typeId != "minecraft:web") {
      blockUp.setType("minecraft:flowing_water");
      block.setType("minecraft:air");
    } else if (blockUp && blockUp.typeId === "minecraft:web" && blockDown && blockDown.typeId === "minecraft:web") {
      blockUp.setType("minecraft:air");
      block.setType("minecraft:flowing_water");
      blockDown.setType("minecraft:air");
    } else {
      block.setType("minecraft:flowing_water");
    }
  }
  if (
    beforeItemStack?.typeId.includes("_door") &&
    !block.typeId.includes("_door") &&
    doorEnabled &&
    (block.isWaterlogged || (blockUp && blockUp.isWaterlogged))
  ) {
    const playerViewDirection = player.getViewDirection();
    const direction = (Math.abs(playerViewDirection.x) > Math.abs(playerViewDirection.z))
      ? (playerViewDirection.x > 0 ? "0" : "2")
      : (playerViewDirection.z > 0 ? "1" : "3");
    dimension.runCommand(`setblock ${x} ${y + 1} ${z} air replace`);
    dimension.runCommand(`setblock ${x} ${y + 2} ${z} air replace`);
    dimension.runCommand(`setblock ${x} ${y + 1} ${z} air replace`);
    dimension.runCommand(`setblock ${x} ${y + 2} ${z} air replace`);
    dimension.runCommand(`setblock ${x} ${y + 1} ${z} ${beforeItemStack.typeId}["direction"=${direction}] replace`);
  }
});