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

world.beforeEvents.worldInitialize.subscribe(init => {
  init.blockComponentRegistry.registerCustomComponent("peach:rope_logic", {
    onPlace(e) {
      const { block } = e;
      if (!block) return;
      
      system.runTimeout(() => {
        try {
          const currentBlock = block.dimension.getBlock(block.location);
          if (currentBlock && currentBlock.typeId === "peach:rope_block") {
            updateRopeConnections(currentBlock);
          }
        } catch (e) {}
      }, 1);
    }
  });
});

world.afterEvents.playerPlaceBlock.subscribe(e => {
  const { block } = e;
  if (!block) return;
  updateAdjacentRopes(block.dimension, block.location);
});

world.afterEvents.playerBreakBlock.subscribe(e => {
  const { block, dimension } = e;
  
  updateAdjacentRopes(dimension, block.location);
});

function updateRopeConnections(block) {
  if (!block || block.typeId !== "peach:rope_block") return;
  
  const { x, y, z } = block.location;
  const dimension = block.dimension;
  
  const directions = {
    north: { offset: { x: 0, y: 0, z: -1 }, state: "peach:north" },
    south: { offset: { x: 0, y: 0, z: 1 }, state: "peach:south" },
    east: { offset: { x: 1, y: 0, z: 0 }, state: "peach:east" },
    west: { offset: { x: -1, y: 0, z: 0 }, state: "peach:west" },
    up: { offset: { x: 0, y: 1, z: 0 }, state: "peach:up" },
    down: { offset: { x: 0, y: -1, z: 0 }, state: "peach:down" }
  };
  
  let newPermutation = block.permutation;
  
  for (const [direction, data] of Object.entries(directions)) {
    const neighborPos = {
      x: x + data.offset.x,
      y: y + data.offset.y,
      z: z + data.offset.z
    };
    
    try {
      const neighborBlock = dimension.getBlock(neighborPos);
      
      const shouldConnect = neighborBlock && !isAirOrLiquid(neighborBlock);
      
      newPermutation = newPermutation.withState(data.state, shouldConnect ? 1 : 0);
    } catch (err) {
      newPermutation = newPermutation.withState(data.state, 0);
    }
  }
  
  try {
    block.setPermutation(newPermutation);
  } catch (e) {}
}

function isAirOrLiquid(block) {
  const typeId = block.typeId;
  
  const nonConnectableBlocks = [
    "minecraft:air",
    "minecraft:water",
    "minecraft:flowing_water",
    "minecraft:lava",
    "minecraft:flowing_lava"
  ];
  
  return nonConnectableBlocks.includes(typeId);
}

function updateAdjacentRopes(dimension, location) {
  const directions = [
    { x: 0, y: 0, z: -1 }, // North
    { x: 0, y: 0, z: 1 },  // South
    { x: 1, y: 0, z: 0 },  // East
    { x: -1, y: 0, z: 0 }, // West
    { x: 0, y: 1, z: 0 },  // Up
    { x: 0, y: -1, z: 0 }  // Down
  ];
  
  directions.forEach(offset => {
    const adjacentPos = {
      x: location.x + offset.x,
      y: location.y + offset.y,
      z: location.z + offset.z
    };
    
    try {
      const adjacentBlock = dimension.getBlock(adjacentPos);
      if (adjacentBlock && adjacentBlock.typeId === "peach:rope_block") {
        updateRopeConnections(adjacentBlock);
      }
    } catch (e) {}
  });
}