import { world } from "@minecraft/server";
const girderTag = "maca_probuilder:is_girder";
world.beforeEvents.worldInitialize.subscribe((eventData) => {
  eventData.blockComponentRegistry.registerCustomComponent("maca_probuilder:girderComp", {
    onPlayerDestroy(e) {
      const { block } = e;
      girder_Manager.updateGirdersAround(block);
    },
    onPlace(e) {
      const { block } = e;
      girder_Manager.updateGirdersAround(block);
    }
  });
});
world.afterEvents.blockExplode.subscribe((e) => {
  const { block, explodedBlockPermutation: perm } = e;
  const north = block.north();
  const east = block.east();
  const south = block.south();
  const west = block.west();
  if (!perm.hasTag(girderTag) && !north?.hasTag(girderTag) && !east?.hasTag(girderTag) && !south?.hasTag(girderTag) && !west?.hasTag(girderTag))
    return;
  girder_Manager.updateGirdersAround(block);
});
class girder_Manager {
  static update_Girder_States(Girder) {
    let north = void 0;
    try {
      north = Girder.north(1);
    } catch {
    }
    let south = void 0;
    try {
      south = Girder.south(1);
    } catch {
    }
    let east = void 0;
    try {
      east = Girder.east(1);
    } catch {
    }
    let west = void 0;
    try {
      west = Girder.west(1);
    } catch {
    }
    const blocks = [
      { block: north, side: "north" },
      { block: south, side: "south" },
      { block: east, side: "east" },
      { block: west, side: "west" }
    ];
    for (const blockData of blocks) {
      Girder.setPermutation(Girder.permutation.withState("maca_probuilder:connect_" + blockData.side, false));
      if (blockData.block != void 0) {
        if (blockData.block.hasTag("maca_probuilder:is_girder")) {
          const shouldConnect = girder_Manager.isConnectable(blockData.block, Girder);
          if (shouldConnect) {
            Girder.setPermutation(Girder.permutation.withState("maca_probuilder:connect_" + blockData.side, true));
          }
        }
      }
    }
  }
  static isConnectable(block, Girder) {
    const blockDir = block.permutation.getState("minecraft:cardinal_direction");
    const GirderDir = Girder.permutation.getState("minecraft:cardinal_direction");
    const opposite = girder_Manager.getOpposite(GirderDir);
    if (!opposite) {
      return false;
    }
    if (blockDir === GirderDir || blockDir === opposite) {
      return false;
    }
    return true;
  }
  static getOpposite(GirderDir) {
    switch (GirderDir) {
      case "north":
        return "south";
      case "south":
        return "north";
      case "east":
        return "west";
      case "west":
        return "east";
      default:
        return false;
    }
  }
  static updateGirdersAround(Block2) {
    let north = void 0;
    try {
      north = Block2.north(1);
    } catch {
    }
    let south = void 0;
    try {
      south = Block2.south(1);
    } catch {
    }
    let east = void 0;
    try {
      east = Block2.east(1);
    } catch {
    }
    let west = void 0;
    try {
      west = Block2.west(1);
    } catch {
    }
    const blocks = [Block2, north, south, east, west];
    for (const block of blocks) {
      if (block != void 0) {
        if (block.hasTag(girderTag)) {
          this.update_Girder_States(block);
        }
      }
    }
  }
}
export {
  girder_Manager
};
