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

export function checkStair(block, northBlock, southBlock, eastBlock, westBlock, vertical_half, shape, cardinal_direction) {
  let southBlockAirFlag = false;
  let northBlockAirFlag = false;
  let eastBlockAirFlag = false;
  let westBlockAirFlag = false;
  if (southBlock == undefined) southBlockAirFlag = true;
  if (northBlock == undefined) northBlockAirFlag = true;
  if (eastBlock == undefined) eastBlockAirFlag = true;
  if (westBlock == undefined) westBlockAirFlag = true;

  // world.sendMessage("OLD north block: " + northBlock.typeId);
  // world.sendMessage("OLD southBlockRoof: " + southBlock.typeId.includes("roof"));
  // world.sendMessage("OLD hb_hr_custom_stairs_up: " + southBlock.hasTag("hb_hr_custom_stairs_up"));
  // world.sendMessage("OLD shape: " + shape);
  // world.sendMessage("OLD cardinal_direction: " + cardinal_direction);

  if (vertical_half == "bottom") {
    block.setPermutation(block.permutation.withState("hb_hr:placed_bit", true));
    if (southBlockAirFlag && southBlock.typeId.includes("roof") && southBlock.hasTag("hb_hr_custom_stairs_up") && southBlock.hasTag("hb_hr_custom_stairs_east") && shape == "straight" && cardinal_direction == "north") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "inner_right"));
    }
    if (southBlockAirFlag && !southBlock.typeId.includes("roof") && !southBlock.hasTag("hb_hr_custom_stairs_up") && !southBlock.hasTag("hb_hr_custom_stairs_east") && shape == "inner_right" && cardinal_direction == "north") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "straight"));
    }
    if (southBlockAirFlag && southBlock.typeId.includes("roof") && southBlock.hasTag("hb_hr_custom_stairs_up") && southBlock.hasTag("hb_hr_custom_stairs_west") && shape == "straight" && cardinal_direction == "north") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "inner_left"));
    }
    if (southBlockAirFlag && !southBlock.typeId.includes("roof") && !southBlock.hasTag("hb_hr_custom_stairs_up") && !southBlock.hasTag("hb_hr_custom_stairs_west") && shape == "inner_left" && cardinal_direction == "north") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "straight"));
    }
    if (northBlockAirFlag && northBlock.typeId.includes("roof") && northBlock.hasTag("hb_hr_custom_stairs_up") && northBlock.hasTag("hb_hr_custom_stairs_west") && shape == "straight" && cardinal_direction == "south") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "inner_left"));
    }
    if (northBlockAirFlag && !northBlock.typeId.includes("roof") && !northBlock.hasTag("hb_hr_custom_stairs_up") && !northBlock.hasTag("hb_hr_custom_stairs_west") && shape == "inner_left" && cardinal_direction == "south") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "straight"));
    }
    if (northBlockAirFlag && northBlock.typeId.includes("roof") && northBlock.hasTag("hb_hr_custom_stairs_up") && northBlock.hasTag("hb_hr_custom_stairs_east") && shape == "straight" && cardinal_direction == "south") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "inner_right"));
    }
    if (northBlockAirFlag && !northBlock.typeId.includes("roof") && !northBlock.hasTag("hb_hr_custom_stairs_up") && !northBlock.hasTag("hb_hr_custom_stairs_east") && shape == "inner_right" && cardinal_direction == "south") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "straight"));
    }
    if (eastBlockAirFlag && eastBlock.typeId.includes("roof") && eastBlock.hasTag("hb_hr_custom_stairs_up") && eastBlock.hasTag("hb_hr_custom_stairs_south") && shape == "straight" && cardinal_direction == "west") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "inner_left"));
    }
    if (eastBlockAirFlag && !eastBlock.typeId.includes("roof") && !eastBlock.hasTag("hb_hr_custom_stairs_up") && !eastBlock.hasTag("hb_hr_custom_stairs_south") && shape == "inner_left" && cardinal_direction == "west") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "straight"));
    }
    if (eastBlockAirFlag && eastBlock.typeId.includes("roof") && eastBlock.hasTag("hb_hr_custom_stairs_up") && eastBlock.hasTag("hb_hr_custom_stairs_north") && shape == "straight" && cardinal_direction == "west") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "inner_right"));
    }
    if (eastBlockAirFlag && !eastBlock.typeId.includes("roof") && !eastBlock.hasTag("hb_hr_custom_stairs_up") && !eastBlock.hasTag("hb_hr_custom_stairs_north") && shape == "inner_right" && cardinal_direction == "west") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "straight"));
    }
    if (westBlockAirFlag && westBlock.typeId.includes("roof") && westBlock.hasTag("hb_hr_custom_stairs_up") && westBlock.hasTag("hb_hr_custom_stairs_north") && shape == "straight" && cardinal_direction == "east") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "inner_left"));
    }
    if (westBlockAirFlag && !westBlock.typeId.includes("roof") && !westBlock.hasTag("hb_hr_custom_stairs_up") && !westBlock.hasTag("hb_hr_custom_stairs_north") && shape == "inner_left" && cardinal_direction == "east") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "straight"));
    }
    if (westBlockAirFlag && westBlock.typeId.includes("roof") && westBlock.hasTag("hb_hr_custom_stairs_up") && westBlock.hasTag("hb_hr_custom_stairs_south") && shape == "straight" && cardinal_direction == "east") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "inner_right"));
    }
    if (westBlockAirFlag && !westBlock.typeId.includes("roof") && !westBlock.hasTag("hb_hr_custom_stairs_up") && !westBlock.hasTag("hb_hr_custom_stairs_south") && shape == "inner_right" && cardinal_direction == "east") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "straight"));
    }
    if (northBlockAirFlag && northBlock.typeId.includes("roof") && northBlock.hasTag("hb_hr_custom_stairs_up") && northBlock.hasTag("hb_hr_custom_stairs_west") && shape == "straight" && cardinal_direction == "north") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "outer_right"));
    }
    if (northBlockAirFlag && !northBlock.typeId.includes("roof") && !northBlock.hasTag("hb_hr_custom_stairs_up") && !northBlock.hasTag("hb_hr_custom_stairs_west") && shape == "outer_right" && cardinal_direction == "north") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "straight"));
    }
    if (northBlockAirFlag && northBlock.typeId.includes("roof") && northBlock.hasTag("hb_hr_custom_stairs_up") && northBlock.hasTag("hb_hr_custom_stairs_east") && shape == "straight" && cardinal_direction == "north") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "outer_left"));
    }
    if (northBlockAirFlag && !northBlock.typeId.includes("roof") && !northBlock.hasTag("hb_hr_custom_stairs_up") && !northBlock.hasTag("hb_hr_custom_stairs_east") && shape == "outer_left" && cardinal_direction == "north") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "straight"));
    }
    if (southBlockAirFlag && southBlock.typeId.includes("roof") && southBlock.hasTag("hb_hr_custom_stairs_up") && southBlock.hasTag("hb_hr_custom_stairs_west") && shape == "straight" && cardinal_direction == "south") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "outer_right"));
    }
    if (southBlockAirFlag && !southBlock.typeId.includes("roof") && !southBlock.hasTag("hb_hr_custom_stairs_up") && !southBlock.hasTag("hb_hr_custom_stairs_west") && shape == "outer_right" && cardinal_direction == "south") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "straight"));
    }
    if (southBlockAirFlag && southBlock.typeId.includes("roof") && southBlock.hasTag("hb_hr_custom_stairs_up") && southBlock.hasTag("hb_hr_custom_stairs_east") && shape == "straight" && cardinal_direction == "south") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "outer_left"));
    }
    if (southBlockAirFlag && !southBlock.typeId.includes("roof") && !southBlock.hasTag("hb_hr_custom_stairs_up") && !southBlock.hasTag("hb_hr_custom_stairs_east") && shape == "outer_left" && cardinal_direction == "south") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "straight"));
    }
    if (westBlockAirFlag && westBlock.typeId.includes("roof") && westBlock.hasTag("hb_hr_custom_stairs_up") && westBlock.hasTag("hb_hr_custom_stairs_north") && shape == "straight" && cardinal_direction == "west") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "outer_right"));
    }
    if (westBlockAirFlag && !westBlock.typeId.includes("roof") && !westBlock.hasTag("hb_hr_custom_stairs_up") && !westBlock.hasTag("hb_hr_custom_stairs_north") && shape == "outer_right" && cardinal_direction == "west") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "straight"));
    }
    if (westBlockAirFlag && westBlock.typeId.includes("roof") && westBlock.hasTag("hb_hr_custom_stairs_up") && westBlock.hasTag("hb_hr_custom_stairs_south") && shape == "straight" && cardinal_direction == "west") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "outer_left"));
    }
    if (westBlockAirFlag && !westBlock.typeId.includes("roof") && !westBlock.hasTag("hb_hr_custom_stairs_up") && !westBlock.hasTag("hb_hr_custom_stairs_south") && shape == "outer_left" && cardinal_direction == "west") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "straight"));
    }
    if (eastBlockAirFlag && eastBlock.typeId.includes("roof") && eastBlock.hasTag("hb_hr_custom_stairs_up") && eastBlock.hasTag("hb_hr_custom_stairs_south") && shape == "straight" && cardinal_direction == "east") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "outer_right"));
    }
    if (eastBlockAirFlag && !eastBlock.typeId.includes("roof") && !eastBlock.hasTag("hb_hr_custom_stairs_up") && !eastBlock.hasTag("hb_hr_custom_stairs_south") && shape == "outer_right" && cardinal_direction == "east") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "straight"));
    }
    if (eastBlockAirFlag && eastBlock.typeId.includes("roof") && eastBlock.hasTag("hb_hr_custom_stairs_up") && eastBlock.hasTag("hb_hr_custom_stairs_north") && shape == "straight" && cardinal_direction == "east") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "outer_left"));
    }
    if (eastBlockAirFlag && !eastBlock.typeId.includes("roof") && !eastBlock.hasTag("hb_hr_custom_stairs_up") && !eastBlock.hasTag("hb_hr_custom_stairs_north") && shape == "outer_left" && cardinal_direction == "east") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "straight"));
    }
  }
  if (vertical_half == "top") {
    block.setPermutation(block.permutation.withState("hb_hr:placed_bit", true));
    if (southBlockAirFlag && southBlock.typeId.includes("roof") && southBlock.hasTag("hb_hr_custom_stairs_down") && southBlock.hasTag("hb_hr_custom_stairs_east") && shape == "straight" && cardinal_direction == "north") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "inner_right"));
    }
    if (southBlockAirFlag && !southBlock.typeId.includes("roof") && !southBlock.hasTag("hb_hr_custom_stairs_down") && !southBlock.hasTag("hb_hr_custom_stairs_east") && shape == "inner_right" && cardinal_direction == "north") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "straight"));
    }
    if (southBlockAirFlag && southBlock.typeId.includes("roof") && southBlock.hasTag("hb_hr_custom_stairs_down") && southBlock.hasTag("hb_hr_custom_stairs_west") && shape == "straight" && cardinal_direction == "north") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "inner_left"));
    }
    if (southBlockAirFlag && !southBlock.typeId.includes("roof") && !southBlock.hasTag("hb_hr_custom_stairs_down") && !southBlock.hasTag("hb_hr_custom_stairs_west") && shape == "inner_left" && cardinal_direction == "north") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "straight"));
    }
    if (northBlockAirFlag && northBlock.typeId.includes("roof") && northBlock.hasTag("hb_hr_custom_stairs_down") && northBlock.hasTag("hb_hr_custom_stairs_west") && shape == "straight" && cardinal_direction == "south") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "inner_left"));
    }
    if (northBlockAirFlag && !northBlock.typeId.includes("roof") && !northBlock.hasTag("hb_hr_custom_stairs_down") && !northBlock.hasTag("hb_hr_custom_stairs_west") && shape == "inner_left" && cardinal_direction == "south") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "straight"));
    }
    if (northBlockAirFlag && northBlock.typeId.includes("roof") && northBlock.hasTag("hb_hr_custom_stairs_down") && northBlock.hasTag("hb_hr_custom_stairs_east") && shape == "straight" && cardinal_direction == "south") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "inner_right"));
    }
    if (northBlockAirFlag && !northBlock.typeId.includes("roof") && !northBlock.hasTag("hb_hr_custom_stairs_down") && !northBlock.hasTag("hb_hr_custom_stairs_east") && shape == "inner_right" && cardinal_direction == "south") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "straight"));
    }
    if (eastBlockAirFlag && eastBlock.typeId.includes("roof") && eastBlock.hasTag("hb_hr_custom_stairs_down") && eastBlock.hasTag("hb_hr_custom_stairs_south") && shape == "straight" && cardinal_direction == "west") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "inner_left"));
    }
    if (eastBlockAirFlag && !eastBlock.typeId.includes("roof") && !eastBlock.hasTag("hb_hr_custom_stairs_down") && !eastBlock.hasTag("hb_hr_custom_stairs_south") && shape == "inner_left" && cardinal_direction == "west") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "straight"));
    }
    if (eastBlockAirFlag && eastBlock.typeId.includes("roof") && eastBlock.hasTag("hb_hr_custom_stairs_down") && eastBlock.hasTag("hb_hr_custom_stairs_north") && shape == "straight" && cardinal_direction == "west") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "inner_right"));
    }
    if (eastBlockAirFlag && !eastBlock.typeId.includes("roof") && !eastBlock.hasTag("hb_hr_custom_stairs_down") && !eastBlock.hasTag("hb_hr_custom_stairs_north") && shape == "inner_right" && cardinal_direction == "west") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "straight"));
    }
    if (westBlockAirFlag && westBlock.typeId.includes("roof") && westBlock.hasTag("hb_hr_custom_stairs_down") && westBlock.hasTag("hb_hr_custom_stairs_north") && shape == "straight" && cardinal_direction == "east") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "inner_left"));
    }
    if (westBlockAirFlag && !westBlock.typeId.includes("roof") && !westBlock.hasTag("hb_hr_custom_stairs_down") && !westBlock.hasTag("hb_hr_custom_stairs_north") && shape == "inner_left" && cardinal_direction == "east") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "straight"));
    }
    if (westBlockAirFlag && westBlock.typeId.includes("roof") && westBlock.hasTag("hb_hr_custom_stairs_down") && westBlock.hasTag("hb_hr_custom_stairs_south") && shape == "straight" && cardinal_direction == "east") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "inner_right"));
    }
    if (westBlockAirFlag && !westBlock.typeId.includes("roof") && !westBlock.hasTag("hb_hr_custom_stairs_down") && !westBlock.hasTag("hb_hr_custom_stairs_south") && shape == "inner_right" && cardinal_direction == "east") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "straight"));
    }
    if (northBlockAirFlag && northBlock.typeId.includes("roof") && northBlock.hasTag("hb_hr_custom_stairs_down") && northBlock.hasTag("hb_hr_custom_stairs_west") && shape == "straight" && cardinal_direction == "north") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "outer_right"));
    }
    if (northBlockAirFlag && !northBlock.typeId.includes("roof") && !northBlock.hasTag("hb_hr_custom_stairs_down") && !northBlock.hasTag("hb_hr_custom_stairs_west") && shape == "outer_right" && cardinal_direction == "north") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "straight"));
    }
    if (northBlockAirFlag && northBlock.typeId.includes("roof") && northBlock.hasTag("hb_hr_custom_stairs_down") && northBlock.hasTag("hb_hr_custom_stairs_east") && shape == "straight" && cardinal_direction == "north") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "outer_left"));
    }
    if (northBlockAirFlag && !northBlock.typeId.includes("roof") && !northBlock.hasTag("hb_hr_custom_stairs_down") && !northBlock.hasTag("hb_hr_custom_stairs_east") && shape == "outer_left" && cardinal_direction == "north") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "straight"));
    }
    if (southBlockAirFlag && southBlock.typeId.includes("roof") && southBlock.hasTag("hb_hr_custom_stairs_down") && southBlock.hasTag("hb_hr_custom_stairs_west") && shape == "straight" && cardinal_direction == "south") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "outer_right"));
    }
    if (southBlockAirFlag && !southBlock.typeId.includes("roof") && !southBlock.hasTag("hb_hr_custom_stairs_down") && !southBlock.hasTag("hb_hr_custom_stairs_west") && shape == "outer_right" && cardinal_direction == "south") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "straight"));
    }
    if (southBlockAirFlag && southBlock.typeId.includes("roof") && southBlock.hasTag("hb_hr_custom_stairs_down") && southBlock.hasTag("hb_hr_custom_stairs_east") && shape == "straight" && cardinal_direction == "south") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "outer_left"));
    }
    if (southBlockAirFlag && !southBlock.typeId.includes("roof") && !southBlock.hasTag("hb_hr_custom_stairs_down") && !southBlock.hasTag("hb_hr_custom_stairs_east") && shape == "outer_left" && cardinal_direction == "south") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "straight"));
    }
    if (westBlockAirFlag && westBlock.typeId.includes("roof") && westBlock.hasTag("hb_hr_custom_stairs_down") && westBlock.hasTag("hb_hr_custom_stairs_north") && shape == "straight" && cardinal_direction == "west") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "outer_right"));
    }
    if (westBlockAirFlag && !westBlock.typeId.includes("roof") && !westBlock.hasTag("hb_hr_custom_stairs_down") && !westBlock.hasTag("hb_hr_custom_stairs_north") && shape == "outer_right" && cardinal_direction == "west") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "straight"));
    }
    if (westBlockAirFlag && westBlock.typeId.includes("roof") && westBlock.hasTag("hb_hr_custom_stairs_down") && westBlock.hasTag("hb_hr_custom_stairs_south") && shape == "straight" && cardinal_direction == "west") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "outer_left"));
    }
    if (westBlockAirFlag && !westBlock.typeId.includes("roof") && !westBlock.hasTag("hb_hr_custom_stairs_down") && !westBlock.hasTag("hb_hr_custom_stairs_south") && shape == "outer_left" && cardinal_direction == "west") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "straight"));
    }
    if (eastBlockAirFlag && eastBlock.typeId.includes("roof") && eastBlock.hasTag("hb_hr_custom_stairs_down") && eastBlock.hasTag("hb_hr_custom_stairs_south") && shape == "straight" && cardinal_direction == "east") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "outer_right"));
    }
    if (eastBlockAirFlag && !eastBlock.typeId.includes("roof") && !eastBlock.hasTag("hb_hr_custom_stairs_down") && !eastBlock.hasTag("hb_hr_custom_stairs_south") && shape == "outer_right" && cardinal_direction == "east") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "straight"));
    }
    if (eastBlockAirFlag && eastBlock.typeId.includes("roof") && eastBlock.hasTag("hb_hr_custom_stairs_down") && eastBlock.hasTag("hb_hr_custom_stairs_north") && shape == "straight" && cardinal_direction == "east") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "outer_left"));
    }
    if (eastBlockAirFlag && !eastBlock.typeId.includes("roof") && !eastBlock.hasTag("hb_hr_custom_stairs_down") && !eastBlock.hasTag("hb_hr_custom_stairs_north") && shape == "outer_left" && cardinal_direction == "east") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "straight"));
    }
  }

  // world.sendMessage("NEW north block: " + northBlock.typeId);
  // world.sendMessage("NEW southBlockRoof: " + southBlock.typeId.includes("roof"));
  // world.sendMessage("NEW hb_hr_custom_stairs_up: " + southBlock.hasTag("hb_hr_custom_stairs_up"));
  // world.sendMessage("NEW shape: " + shape);
  // world.sendMessage("NEW cardinal_direction: " + cardinal_direction);
}
function checkStairNorth(block, northBlock, vertical_half, shape, cardinal_direction) {
  if (vertical_half == "bottom") {
    block.setPermutation(block.permutation.withState("hb_hr:placed_bit", true));
    if (northBlock.hasTag("hb_hr_custom_stairs_up") && northBlock.hasTag("hb_hr_custom_stairs_west") && shape == "straight" && cardinal_direction == "south") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "inner_left"));
    }
    if (!northBlock.hasTag("hb_hr_custom_stairs_up") && !northBlock.hasTag("hb_hr_custom_stairs_west") && shape == "inner_left" && cardinal_direction == "south") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "straight"));
    }
    if (northBlock.hasTag("hb_hr_custom_stairs_up") && northBlock.hasTag("hb_hr_custom_stairs_east") && shape == "straight" && cardinal_direction == "south") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "inner_right"));
    }
    if (!northBlock.hasTag("hb_hr_custom_stairs_up") && !northBlock.hasTag("hb_hr_custom_stairs_east") && shape == "inner_right" && cardinal_direction == "south") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "straight"));
    }
    if (northBlock.hasTag("hb_hr_custom_stairs_up") && northBlock.hasTag("hb_hr_custom_stairs_west") && shape == "straight" && cardinal_direction == "north") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "outer_right"));
    }
    if (!northBlock.hasTag("hb_hr_custom_stairs_up") && !northBlock.hasTag("hb_hr_custom_stairs_west") && shape == "outer_right" && cardinal_direction == "north") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "straight"));
    }
    if (northBlock.hasTag("hb_hr_custom_stairs_up") && northBlock.hasTag("hb_hr_custom_stairs_east") && shape == "straight" && cardinal_direction == "north") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "outer_left"));
    }
    if (!northBlock.hasTag("hb_hr_custom_stairs_up") && !northBlock.hasTag("hb_hr_custom_stairs_east") && shape == "outer_left" && cardinal_direction == "north") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "straight"));
    }
  }
  if (vertical_half == "top") {
    block.setPermutation(block.permutation.withState("hb_hr:placed_bit", true));
    if (northBlock.hasTag("hb_hr_custom_stairs_down") && northBlock.hasTag("hb_hr_custom_stairs_west") && shape == "straight" && cardinal_direction == "south") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "inner_left"));
    }
    if (!northBlock.hasTag("hb_hr_custom_stairs_down") && !northBlock.hasTag("hb_hr_custom_stairs_west") && shape == "inner_left" && cardinal_direction == "south") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "straight"));
    }
    if (northBlock.hasTag("hb_hr_custom_stairs_down") && northBlock.hasTag("hb_hr_custom_stairs_east") && shape == "straight" && cardinal_direction == "south") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "inner_right"));
    }
    if (!northBlock.hasTag("hb_hr_custom_stairs_down") && !northBlock.hasTag("hb_hr_custom_stairs_east") && shape == "inner_right" && cardinal_direction == "south") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "straight"));
    }
    if (northBlock.hasTag("hb_hr_custom_stairs_down") && northBlock.hasTag("hb_hr_custom_stairs_west") && shape == "straight" && cardinal_direction == "north") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "outer_right"));
    }
    if (!northBlock.hasTag("hb_hr_custom_stairs_down") && !northBlock.hasTag("hb_hr_custom_stairs_west") && shape == "outer_right" && cardinal_direction == "north") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "straight"));
    }
    if (northBlock.hasTag("hb_hr_custom_stairs_down") && northBlock.hasTag("hb_hr_custom_stairs_east") && shape == "straight" && cardinal_direction == "north") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "outer_left"));
    }
    if (!northBlock.hasTag("hb_hr_custom_stairs_down") && !northBlock.hasTag("hb_hr_custom_stairs_east") && shape == "outer_left" && cardinal_direction == "north") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "straight"));
    }
  }
}
function checkStairSouth(block, southBlock, vertical_half, shape, cardinal_direction) {
  if (vertical_half === "bottom") {
    block.setPermutation(block.permutation.withState("hb_hr:placed_bit", true));
    // inner-corners (von Süden her betrachtet)
    if (southBlock.hasTag("hb_hr_custom_stairs_up") && southBlock.hasTag("hb_hr_custom_stairs_east") && shape === "straight" && cardinal_direction === "north") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "inner_right"));
    }
    if (!southBlock.hasTag("hb_hr_custom_stairs_up") && !southBlock.hasTag("hb_hr_custom_stairs_east") && shape === "inner_right" && cardinal_direction === "north") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "straight"));
    }
    if (southBlock.hasTag("hb_hr_custom_stairs_up") && southBlock.hasTag("hb_hr_custom_stairs_west") && shape === "straight" && cardinal_direction === "north") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "inner_left"));
    }
    if (!southBlock.hasTag("hb_hr_custom_stairs_up") && !southBlock.hasTag("hb_hr_custom_stairs_west") && shape === "inner_left" && cardinal_direction === "north") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "straight"));
    }
    // outer-corners (von Süden her betrachtet)
    if (southBlock.hasTag("hb_hr_custom_stairs_up") && southBlock.hasTag("hb_hr_custom_stairs_west") && shape === "straight" && cardinal_direction === "south") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "outer_right"));
    }
    if (!southBlock.hasTag("hb_hr_custom_stairs_up") && !southBlock.hasTag("hb_hr_custom_stairs_west") && shape === "outer_right" && cardinal_direction === "south") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "straight"));
    }
    if (southBlock.hasTag("hb_hr_custom_stairs_up") && southBlock.hasTag("hb_hr_custom_stairs_east") && shape === "straight" && cardinal_direction === "south") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "outer_left"));
    }
    if (!southBlock.hasTag("hb_hr_custom_stairs_up") && !southBlock.hasTag("hb_hr_custom_stairs_east") && shape === "outer_left" && cardinal_direction === "south") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "straight"));
    }
  }

  if (vertical_half === "top") {
    block.setPermutation(block.permutation.withState("hb_hr:placed_bit", true));
    // inner-corners (oben, von Süden her)
    if (southBlock.hasTag("hb_hr_custom_stairs_down") && southBlock.hasTag("hb_hr_custom_stairs_east") && shape === "straight" && cardinal_direction === "north") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "inner_right"));
    }
    if (!southBlock.hasTag("hb_hr_custom_stairs_down") && !southBlock.hasTag("hb_hr_custom_stairs_east") && shape === "inner_right" && cardinal_direction === "north") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "straight"));
    }
    if (southBlock.hasTag("hb_hr_custom_stairs_down") && southBlock.hasTag("hb_hr_custom_stairs_west") && shape === "straight" && cardinal_direction === "north") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "inner_left"));
    }
    if (!southBlock.hasTag("hb_hr_custom_stairs_down") && !southBlock.hasTag("hb_hr_custom_stairs_west") && shape === "inner_left" && cardinal_direction === "north") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "straight"));
    }
    // outer-corners (oben, von Süden her)
    if (southBlock.hasTag("hb_hr_custom_stairs_down") && southBlock.hasTag("hb_hr_custom_stairs_west") && shape === "straight" && cardinal_direction === "south") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "outer_right"));
    }
    if (!southBlock.hasTag("hb_hr_custom_stairs_down") && !southBlock.hasTag("hb_hr_custom_stairs_west") && shape === "outer_right" && cardinal_direction === "south") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "straight"));
    }
    if (southBlock.hasTag("hb_hr_custom_stairs_down") && southBlock.hasTag("hb_hr_custom_stairs_east") && shape === "straight" && cardinal_direction === "south") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "outer_left"));
    }
    if (!southBlock.hasTag("hb_hr_custom_stairs_down") && !southBlock.hasTag("hb_hr_custom_stairs_east") && shape === "outer_left" && cardinal_direction === "south") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "straight"));
    }
  }
}

function checkStairEast(block, eastBlock, vertical_half, shape, cardinal_direction) {
  if (vertical_half === "bottom") {
    block.setPermutation(block.permutation.withState("hb_hr:placed_bit", true));
    // inner-corners (von Osten her betrachtet)
    if (eastBlock.hasTag("hb_hr_custom_stairs_up") && eastBlock.hasTag("hb_hr_custom_stairs_south") && shape === "straight" && cardinal_direction === "west") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "inner_left"));
    }
    if (!eastBlock.hasTag("hb_hr_custom_stairs_up") && !eastBlock.hasTag("hb_hr_custom_stairs_south") && shape === "inner_left" && cardinal_direction === "west") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "straight"));
    }
    if (eastBlock.hasTag("hb_hr_custom_stairs_up") && eastBlock.hasTag("hb_hr_custom_stairs_north") && shape === "straight" && cardinal_direction === "west") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "inner_right"));
    }
    if (!eastBlock.hasTag("hb_hr_custom_stairs_up") && !eastBlock.hasTag("hb_hr_custom_stairs_north") && shape === "inner_right" && cardinal_direction === "west") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "straight"));
    }
    // outer-corners (von Osten her betrachtet)
    if (eastBlock.hasTag("hb_hr_custom_stairs_up") && eastBlock.hasTag("hb_hr_custom_stairs_south") && shape === "straight" && cardinal_direction === "east") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "outer_right"));
    }
    if (!eastBlock.hasTag("hb_hr_custom_stairs_up") && !eastBlock.hasTag("hb_hr_custom_stairs_south") && shape === "outer_right" && cardinal_direction === "east") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "straight"));
    }
    if (eastBlock.hasTag("hb_hr_custom_stairs_up") && eastBlock.hasTag("hb_hr_custom_stairs_north") && shape === "straight" && cardinal_direction === "east") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "outer_left"));
    }
    if (!eastBlock.hasTag("hb_hr_custom_stairs_up") && !eastBlock.hasTag("hb_hr_custom_stairs_north") && shape === "outer_left" && cardinal_direction === "east") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "straight"));
    }
  }

  if (vertical_half === "top") {
    block.setPermutation(block.permutation.withState("hb_hr:placed_bit", true));
    // inner-corners (oben, von Osten her)
    if (eastBlock.hasTag("hb_hr_custom_stairs_down") && eastBlock.hasTag("hb_hr_custom_stairs_south") && shape === "straight" && cardinal_direction === "west") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "inner_left"));
    }
    if (!eastBlock.hasTag("hb_hr_custom_stairs_down") && !eastBlock.hasTag("hb_hr_custom_stairs_south") && shape === "inner_left" && cardinal_direction === "west") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "straight"));
    }
    if (eastBlock.hasTag("hb_hr_custom_stairs_down") && eastBlock.hasTag("hb_hr_custom_stairs_north") && shape === "straight" && cardinal_direction === "west") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "inner_right"));
    }
    if (!eastBlock.hasTag("hb_hr_custom_stairs_down") && !eastBlock.hasTag("hb_hr_custom_stairs_north") && shape === "inner_right" && cardinal_direction === "west") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "straight"));
    }
    // outer-corners (oben, von Osten her)
    if (eastBlock.hasTag("hb_hr_custom_stairs_down") && eastBlock.hasTag("hb_hr_custom_stairs_south") && shape === "straight" && cardinal_direction === "east") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "outer_right"));
    }
    if (!eastBlock.hasTag("hb_hr_custom_stairs_down") && !eastBlock.hasTag("hb_hr_custom_stairs_south") && shape === "outer_right" && cardinal_direction === "east") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "straight"));
    }
    if (eastBlock.hasTag("hb_hr_custom_stairs_down") && eastBlock.hasTag("hb_hr_custom_stairs_north") && shape === "straight" && cardinal_direction === "east") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "outer_left"));
    }
    if (!eastBlock.hasTag("hb_hr_custom_stairs_down") && !eastBlock.hasTag("hb_hr_custom_stairs_north") && shape === "outer_left" && cardinal_direction === "east") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "straight"));
    }
  }
}

function checkStairWest(block, westBlock, vertical_half, shape, cardinal_direction) {
  if (vertical_half === "bottom") {
    block.setPermutation(block.permutation.withState("hb_hr:placed_bit", true));
    // inner-corners (von Westen her betrachtet)
    if (westBlock.hasTag("hb_hr_custom_stairs_up") && westBlock.hasTag("hb_hr_custom_stairs_north") && shape === "straight" && cardinal_direction === "east") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "inner_left"));
    }
    if (!westBlock.hasTag("hb_hr_custom_stairs_up") && !westBlock.hasTag("hb_hr_custom_stairs_north") && shape === "inner_left" && cardinal_direction === "east") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "straight"));
    }
    if (westBlock.hasTag("hb_hr_custom_stairs_up") && westBlock.hasTag("hb_hr_custom_stairs_south") && shape === "straight" && cardinal_direction === "east") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "inner_right"));
    }
    if (!westBlock.hasTag("hb_hr_custom_stairs_up") && !westBlock.hasTag("hb_hr_custom_stairs_south") && shape === "inner_right" && cardinal_direction === "east") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "straight"));
    }
    // outer-corners (von Westen her betrachtet)
    if (westBlock.hasTag("hb_hr_custom_stairs_up") && westBlock.hasTag("hb_hr_custom_stairs_north") && shape === "straight" && cardinal_direction === "west") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "outer_right"));
    }
    if (!westBlock.hasTag("hb_hr_custom_stairs_up") && !westBlock.hasTag("hb_hr_custom_stairs_north") && shape === "outer_right" && cardinal_direction === "west") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "straight"));
    }
    if (westBlock.hasTag("hb_hr_custom_stairs_up") && westBlock.hasTag("hb_hr_custom_stairs_south") && shape === "straight" && cardinal_direction === "west") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "outer_left"));
    }
    if (!westBlock.hasTag("hb_hr_custom_stairs_up") && !westBlock.hasTag("hb_hr_custom_stairs_south") && shape === "outer_left" && cardinal_direction === "west") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "straight"));
    }
  }

  if (vertical_half === "top") {
    block.setPermutation(block.permutation.withState("hb_hr:placed_bit", true));
    // inner-corners (oben, von Westen her)
    if (westBlock.hasTag("hb_hr_custom_stairs_down") && westBlock.hasTag("hb_hr_custom_stairs_north") && shape === "straight" && cardinal_direction === "east") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "inner_left"));
    }
    if (!westBlock.hasTag("hb_hr_custom_stairs_down") && !westBlock.hasTag("hb_hr_custom_stairs_north") && shape === "inner_left" && cardinal_direction === "east") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "straight"));
    }
    if (westBlock.hasTag("hb_hr_custom_stairs_down") && westBlock.hasTag("hb_hr_custom_stairs_south") && shape === "straight" && cardinal_direction === "east") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "inner_right"));
    }
    if (!westBlock.hasTag("hb_hr_custom_stairs_down") && !westBlock.hasTag("hb_hr_custom_stairs_south") && shape === "inner_right" && cardinal_direction === "east") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "straight"));
    }
    // outer-corners (oben, von Westen her)
    if (westBlock.hasTag("hb_hr_custom_stairs_down") && westBlock.hasTag("hb_hr_custom_stairs_north") && shape === "straight" && cardinal_direction === "west") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "outer_right"));
    }
    if (!westBlock.hasTag("hb_hr_custom_stairs_down") && !westBlock.hasTag("hb_hr_custom_stairs_north") && shape === "outer_right" && cardinal_direction === "west") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "straight"));
    }
    if (westBlock.hasTag("hb_hr_custom_stairs_down") && westBlock.hasTag("hb_hr_custom_stairs_south") && shape === "straight" && cardinal_direction === "west") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "outer_left"));
    }
    if (!westBlock.hasTag("hb_hr_custom_stairs_down") && !westBlock.hasTag("hb_hr_custom_stairs_south") && shape === "outer_left" && cardinal_direction === "west") {
      block.setPermutation(block.permutation.withState("hb_hr:shape", "straight"));
    }
  }
}

export function roof() {
  world.beforeEvents.worldInitialize.subscribe((initEvent) => {
    initEvent.blockComponentRegistry.registerCustomComponent("hb_hr:stair_placement", {
      onPlace: (e) => {
        const { block } = e;
        // world.sendMessage("on player aplce: " + block.typeId);
        const northBlock = block.north();
        const southBlock = block.south();
        const eastBlock = block.east();
        const westBlock = block.west();
        const vertical_half = block.permutation.getState("minecraft:vertical_half");
        const shape = block.permutation.getState("hb_hr:shape");
        const cardinal_direction = block.permutation.getState("minecraft:cardinal_direction");

        checkStair(block, northBlock, southBlock, eastBlock, westBlock, vertical_half, shape, cardinal_direction);
      },
      onTick: (e) => {
        const { block } = e;
        // 1) Nur eigene hb_hr-roof-Blöcke behandeln
        const myType = block.typeId;
        if (!myType.includes("hb_hr") || !myType.includes("roof")) return;
        if (!block) return;
        // 2) Nachbarn holen
        const northBlock = block.north();
        const southBlock = block.south();
        const eastBlock = block.east();
        const westBlock = block.west();

        const vertical_half = block.permutation.getState("minecraft:vertical_half");
        const shape = block.permutation.getState("hb_hr:shape");
        const cardinal_direction = block.permutation.getState("minecraft:cardinal_direction");

        // Hilfsfunktion, um zu prüfen, ob ein Block existiert und vom richtigen Typ ist
        function isValid(b) {
          return b !== undefined && b.typeId.includes("hb_hr") && b.typeId.includes("roof");
        }

        // 3) Nur gültige Nachbarn übergeben
        if (isValid(northBlock)) {
          checkStairNorth(block, northBlock, vertical_half, shape, cardinal_direction);
        }
        if (isValid(southBlock)) {
          checkStairSouth(block, southBlock, vertical_half, shape, cardinal_direction);
        }
        if (isValid(eastBlock)) {
          checkStairEast(block, eastBlock, vertical_half, shape, cardinal_direction);
        }
        if (isValid(westBlock)) {
          checkStairWest(block, westBlock, vertical_half, shape, cardinal_direction);
        }
      },
    });
  });
  // world.beforeEvents.worldInitialize.subscribe((initEvent) => {
  //   initEvent.blockComponentRegistry.registerCustomComponent("blushrooms:stair_placement", {
  //     onPlace: (e) => {
  //       const { block } = e;
  //       if (!block || !block.permutation) return;
  //       buildStairs(block);
  //       if (block.north(1).hasTag("hb_hr_custom_stairs")) {
  //         buildStairs(block.north(1));
  //       }
  //       if (block.south(1).hasTag("hb_hr_custom_stairs")) {
  //         buildStairs(block.south(1));
  //       }
  //       if (block.east(1).hasTag("hb_hr_custom_stairs")) {
  //         buildStairs(block.east(1));
  //       }
  //       if (block.west(1).hasTag("hb_hr_custom_stairs")) {
  //         buildStairs(block.west(1));
  //       }
  //     },
  //   });
  // });
}
