import { world, Player } from "@minecraft/server";
world.beforeEvents.worldInitialize.subscribe(({ blockComponentRegistry }) => {
  blockComponentRegistry.registerCustomComponent("maca_probuilder:stairComponent", stairComponent);
});
const stairComponent = {
  onPlace(e) {
    const { block, dimension } = e;
    const direction = block.permutation.getState("minecraft:cardinal_direction");
    let entityOffset = { x: 0, z: 0 };
    switch (direction) {
      case "north":
        entityOffset = { x: 0, z: -0.25 };
        break;
      case "east":
        entityOffset = { x: 0.25, z: 0 };
        break;
      case "south":
        entityOffset = { x: 0, z: 0.25 };
        break;
      case "west":
        entityOffset = { x: -0.25, z: 0 };
        break;
    }
    const loc = block.center();
    const offsetLoc = {
      x: loc.x + entityOffset.x,
      y: loc.y,
      z: loc.z + entityOffset.z
    };
    dimension.spawnEntity("maca_probuilder:stair_entity", offsetLoc);
  },
  onStepOn(e) {
    const { entity, block, dimension } = e;
    if (!(entity instanceof Player))
      return;
    const nearbyStairEntity = dimension.getEntities({
      location: block.center(),
      maxDistance: 0.5
    }).filter((x) => x?.typeId === "maca_probuilder:stair_entity");
    if (!(nearbyStairEntity == null || nearbyStairEntity.length == 0 || nearbyStairEntity == void 0))
      return;
    const direction = block.permutation.getState("minecraft:cardinal_direction");
    let entityOffset = { x: 0, z: 0 };
    switch (direction) {
      case "north":
        entityOffset = { x: 0, z: -0.25 };
        break;
      case "east":
        entityOffset = { x: 0.25, z: 0 };
        break;
      case "south":
        entityOffset = { x: 0, z: 0.25 };
        break;
      case "west":
        entityOffset = { x: -0.25, z: 0 };
        break;
    }
    const loc = block.center();
    const offsetLoc = {
      x: loc.x + entityOffset.x,
      y: loc.y,
      z: loc.z + entityOffset.z
    };
    dimension.spawnEntity("maca_probuilder:stair_entity", offsetLoc);
  }
};
