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


system.beforeEvents.startup.subscribe((initEvent) => {
    initEvent.blockComponentRegistry.registerCustomComponent("rustic:stair", {
        beforeOnPlayerPlace: e => {
            const { player, block, face, permutationToPlace, dimension } = e;
            const cardinalDirection = permutationToPlace.getState("minecraft:cardinal_direction");
            const verticalHalf = permutationToPlace.getState("minecraft:vertical_half");
            switch (cardinalDirection) {
                case "east":
                    //console.warn(block.east().typeId)
                    if (block.west().hasTag("rustic:customStair") && block.west().permutation.getState("minecraft:cardinal_direction") == "north" && block.west().permutation.getState("minecraft:vertical_half") == verticalHalf) e.permutationToPlace = permutationToPlace.withState("rustic:connection_bit","bottom_left_1");
                    if (block.west().hasTag("rustic:customStair") && block.west().permutation.getState("minecraft:cardinal_direction") == "south" && block.west().permutation.getState("minecraft:vertical_half") == verticalHalf) e.permutationToPlace = permutationToPlace.withState("rustic:connection_bit","bottom_left_2");
                    system.run(() => {
                        if (block.north().hasTag("rustic:customStair") && block.north().permutation.getState("minecraft:cardinal_direction") == "north" && block.north().permutation.getState("minecraft:vertical_half") == verticalHalf) block.north().setPermutation(block.north().permutation.withState("rustic:connection_bit","bottom_right_2"));
                        if (block.south().hasTag("rustic:customStair") && block.south().permutation.getState("minecraft:cardinal_direction") == "south" && block.south().permutation.getState("minecraft:vertical_half") == verticalHalf) block.south().setPermutation(block.south().permutation.withState("rustic:connection_bit","bottom_right_1"));
                    })
                    //Outter
                    if (block.east().hasTag("rustic:customStair") && block.east().permutation.getState("minecraft:cardinal_direction") == "north" && block.east().permutation.getState("minecraft:vertical_half") == verticalHalf) e.permutationToPlace = BlockPermutation.resolve(permutationToPlace.type.id,{"minecraft:cardinal_direction":permutationToPlace.getState("minecraft:cardinal_direction"),"minecraft:vertical_half": permutationToPlace.getState("minecraft:vertical_half"), "rustic:connection_bit": "bottom_right_1", "rustic:connection_type": "outter" });
                    if (block.east().hasTag("rustic:customStair") && block.east().permutation.getState("minecraft:cardinal_direction") == "south" && block.east().permutation.getState("minecraft:vertical_half") == verticalHalf) e.permutationToPlace = BlockPermutation.resolve(permutationToPlace.type.id,{"minecraft:cardinal_direction":permutationToPlace.getState("minecraft:cardinal_direction"),"minecraft:vertical_half": permutationToPlace.getState("minecraft:vertical_half"), "rustic:connection_bit": "bottom_right_2", "rustic:connection_type": "outter" });
                    system.run(() => {
                        if (block.north().hasTag("rustic:customStair") && block.north().permutation.getState("minecraft:cardinal_direction") == "south" && block.north().permutation.getState("minecraft:vertical_half") == verticalHalf) block.north().setPermutation(block.north().permutation.withState("rustic:connection_bit","bottom_right_2")), block.north().setPermutation(block.north().permutation.withState("rustic:connection_type","outter"));
                        if (block.south().hasTag("rustic:customStair") && block.south().permutation.getState("minecraft:cardinal_direction") == "north" && block.south().permutation.getState("minecraft:vertical_half") == verticalHalf) block.south().setPermutation(block.south().permutation.withState("rustic:connection_bit","bottom_right_1")), block.south().setPermutation(block.south().permutation.withState("rustic:connection_type","outter"));

                    })
                    break;
                case "west":
                    //console.warn(block.west().typeId)
                    if (block.east().hasTag("rustic:customStair") && block.east().permutation.getState("minecraft:cardinal_direction") == "north" && block.east().permutation.getState("minecraft:vertical_half") == verticalHalf) e.permutationToPlace = permutationToPlace.withState("rustic:connection_bit","bottom_right_1");
                    if (block.east().hasTag("rustic:customStair") && block.east().permutation.getState("minecraft:cardinal_direction") == "south" && block.east().permutation.getState("minecraft:vertical_half") == verticalHalf) e.permutationToPlace = permutationToPlace.withState("rustic:connection_bit","bottom_right_2");
                    system.run(() => {
                        if (block.north().hasTag("rustic:customStair") && block.north().permutation.getState("minecraft:cardinal_direction") == "north" && block.north().permutation.getState("minecraft:vertical_half") == verticalHalf) block.north().setPermutation(block.north().permutation.withState("rustic:connection_bit","bottom_left_2"));
                        if (block.south().hasTag("rustic:customStair") && block.south().permutation.getState("minecraft:cardinal_direction") == "south" && block.south().permutation.getState("minecraft:vertical_half") == verticalHalf) block.south().setPermutation(block.south().permutation.withState("rustic:connection_bit","bottom_left_1"));
                    })
                    //Outter
                    if (block.west().hasTag("rustic:customStair") && block.west().permutation.getState("minecraft:cardinal_direction") == "north" && block.west().permutation.getState("minecraft:vertical_half") == verticalHalf) e.permutationToPlace = BlockPermutation.resolve(permutationToPlace.type.id,{"minecraft:cardinal_direction":permutationToPlace.getState("minecraft:cardinal_direction"),"minecraft:vertical_half": permutationToPlace.getState("minecraft:vertical_half"), "rustic:connection_bit": "bottom_left_1", "rustic:connection_type": "outter" });
                    if (block.west().hasTag("rustic:customStair") && block.west().permutation.getState("minecraft:cardinal_direction") == "south" && block.west().permutation.getState("minecraft:vertical_half") == verticalHalf) e.permutationToPlace = BlockPermutation.resolve(permutationToPlace.type.id,{"minecraft:cardinal_direction":permutationToPlace.getState("minecraft:cardinal_direction"),"minecraft:vertical_half": permutationToPlace.getState("minecraft:vertical_half"), "rustic:connection_bit": "bottom_left_2", "rustic:connection_type": "outter" });
                    system.run(() => {
                        if (block.north().hasTag("rustic:customStair") && block.north().permutation.getState("minecraft:cardinal_direction") == "south" && block.north().permutation.getState("minecraft:vertical_half") == verticalHalf) block.north().setPermutation(block.north().permutation.withState("rustic:connection_bit","bottom_left_2")), block.north().setPermutation(block.north().permutation.withState("rustic:connection_type","outter"));
                        if (block.south().hasTag("rustic:customStair") && block.south().permutation.getState("minecraft:cardinal_direction") == "north" && block.south().permutation.getState("minecraft:vertical_half") == verticalHalf) block.south().setPermutation(block.south().permutation.withState("rustic:connection_bit","bottom_left_1")), block.south().setPermutation(block.south().permutation.withState("rustic:connection_type","outter"));
                    })
                    break;
                case "north":
                    //console.warn(block.north().typeId)
                    if (block.south().hasTag("rustic:customStair") && block.south().permutation.getState("minecraft:cardinal_direction") == "east" && block.south().permutation.getState("minecraft:vertical_half") == verticalHalf) e.permutationToPlace = permutationToPlace.withState("rustic:connection_bit","bottom_right_2");
                    if (block.south().hasTag("rustic:customStair") && block.south().permutation.getState("minecraft:cardinal_direction") == "west" && block.south().permutation.getState("minecraft:vertical_half") == verticalHalf) e.permutationToPlace = permutationToPlace.withState("rustic:connection_bit","bottom_left_2");
                    system.run(() => {
                        if (block.east().hasTag("rustic:customStair") && block.east().permutation.getState("minecraft:cardinal_direction") == "east" && block.east().permutation.getState("minecraft:vertical_half") == verticalHalf) block.east().setPermutation(block.east().permutation.withState("rustic:connection_bit","bottom_left_1"));
                        if (block.west().hasTag("rustic:customStair") && block.west().permutation.getState("minecraft:cardinal_direction") == "west" && block.west().permutation.getState("minecraft:vertical_half") == verticalHalf) block.west().setPermutation(block.west().permutation.withState("rustic:connection_bit","bottom_right_1"));
                    })
                    //Outter
                    if (block.north().hasTag("rustic:customStair") && block.north().permutation.getState("minecraft:cardinal_direction") == "east" && block.north().permutation.getState("minecraft:vertical_half") == verticalHalf) e.permutationToPlace = BlockPermutation.resolve(permutationToPlace.type.id,{"minecraft:cardinal_direction":permutationToPlace.getState("minecraft:cardinal_direction"),"minecraft:vertical_half": permutationToPlace.getState("minecraft:vertical_half"), "rustic:connection_bit": "bottom_right_1", "rustic:connection_type": "outter" });
                    if (block.north().hasTag("rustic:customStair") && block.north().permutation.getState("minecraft:cardinal_direction") == "west" && block.north().permutation.getState("minecraft:vertical_half") == verticalHalf) e.permutationToPlace = BlockPermutation.resolve(permutationToPlace.type.id,{"minecraft:cardinal_direction":permutationToPlace.getState("minecraft:cardinal_direction"),"minecraft:vertical_half": permutationToPlace.getState("minecraft:vertical_half"), "rustic:connection_bit": "bottom_left_1", "rustic:connection_type": "outter" });
                    system.run(() => {
                        if (block.east().hasTag("rustic:customStair") && block.east().permutation.getState("minecraft:cardinal_direction") == "west" && block.east().permutation.getState("minecraft:vertical_half") == verticalHalf) block.east().setPermutation(block.east().permutation.withState("rustic:connection_bit","bottom_left_1")), block.east().setPermutation(block.east().permutation.withState("rustic:connection_type","outter"));
                        if (block.west().hasTag("rustic:customStair") && block.west().permutation.getState("minecraft:cardinal_direction") == "east" && block.west().permutation.getState("minecraft:vertical_half") == verticalHalf) block.west().setPermutation(block.west().permutation.withState("rustic:connection_bit","bottom_right_1")), block.west().setPermutation(block.west().permutation.withState("rustic:connection_type","outter"));
                    })
                    break;
                case "south":
                    //console.warn(block.east().typeId)
                    if (block.north().hasTag("rustic:customStair") && block.north().permutation.getState("minecraft:cardinal_direction") == "east" && block.north().permutation.getState("minecraft:vertical_half") == verticalHalf) e.permutationToPlace = permutationToPlace.withState("rustic:connection_bit","bottom_right_1");
                    if (block.north().hasTag("rustic:customStair") && block.north().permutation.getState("minecraft:cardinal_direction") == "west" && block.north().permutation.getState("minecraft:vertical_half") == verticalHalf) e.permutationToPlace = permutationToPlace.withState("rustic:connection_bit","bottom_left_1");
                    system.run(() => {
                        if (block.east().hasTag("rustic:customStair") && block.east().permutation.getState("minecraft:cardinal_direction") == "east" && block.east().permutation.getState("minecraft:vertical_half") == verticalHalf) block.east().setPermutation(block.east().permutation.withState("rustic:connection_bit","bottom_left_2"));
                        if (block.west().hasTag("rustic:customStair") && block.west().permutation.getState("minecraft:cardinal_direction") == "west" && block.west().permutation.getState("minecraft:vertical_half") == verticalHalf) block.west().setPermutation(block.west().permutation.withState("rustic:connection_bit","bottom_right_2"));
                    })
                    //Outter
                    if (block.south().hasTag("rustic:customStair") && block.south().permutation.getState("minecraft:cardinal_direction") == "east" && block.south().permutation.getState("minecraft:vertical_half") == verticalHalf) e.permutationToPlace = BlockPermutation.resolve(permutationToPlace.type.id,{"minecraft:cardinal_direction":permutationToPlace.getState("minecraft:cardinal_direction"),"minecraft:vertical_half": permutationToPlace.getState("minecraft:vertical_half"), "rustic:connection_bit": "bottom_right_2", "rustic:connection_type": "outter" });
                    if (block.south().hasTag("rustic:customStair") && block.south().permutation.getState("minecraft:cardinal_direction") == "west" && block.south().permutation.getState("minecraft:vertical_half") == verticalHalf) e.permutationToPlace = BlockPermutation.resolve(permutationToPlace.type.id,{"minecraft:cardinal_direction":permutationToPlace.getState("minecraft:cardinal_direction"),"minecraft:vertical_half": permutationToPlace.getState("minecraft:vertical_half"), "rustic:connection_bit": "bottom_left_2", "rustic:connection_type": "outter" });
                    system.run(() => {
                        if (block.east().hasTag("rustic:customStair") && block.east().permutation.getState("minecraft:cardinal_direction") == "west" && block.east().permutation.getState("minecraft:vertical_half") == verticalHalf) block.east().setPermutation(block.east().permutation.withState("rustic:connection_bit","bottom_left_2")), block.east().setPermutation(block.east().permutation.withState("rustic:connection_type","outter"));
                        if (block.west().hasTag("rustic:customStair") && block.west().permutation.getState("minecraft:cardinal_direction") == "east" && block.west().permutation.getState("minecraft:vertical_half") == verticalHalf) block.west().setPermutation(block.west().permutation.withState("rustic:connection_bit","bottom_right_2")), block.west().setPermutation(block.west().permutation.withState("rustic:connection_type","outter"));
                    })
                    break;
            }
        },
        onPlayerBreak: e => {
            const { block, brokenBlockPermutation, dimension, player } = e;
            const cardinalDirection = brokenBlockPermutation.getState("minecraft:cardinal_direction");
            const verticalHalf = brokenBlockPermutation.getState("minecraft:vertical_half");
            switch (cardinalDirection) {
                case "north":
                    //console.warn(block.east().typeId)
                    if (block.east().hasTag("rustic:customStair") && block.east().permutation.getState("rustic:connection_bit") == "bottom_left_1" && block.east().permutation.getState("minecraft:vertical_half") == verticalHalf && block.east().permutation.getState("minecraft:cardinal_direction") != cardinalDirection) block.east().setPermutation(block.east().permutation.withState("rustic:connection_bit","none")), block.east().setPermutation(block.east().permutation.withState("rustic:connection_type","inner"))
                    if (block.west().hasTag("rustic:customStair") && block.west().permutation.getState("rustic:connection_bit") == "bottom_right_1" && block.west().permutation.getState("minecraft:vertical_half") == verticalHalf && block.west().permutation.getState("minecraft:cardinal_direction") != cardinalDirection) block.west().setPermutation(block.west().permutation.withState("rustic:connection_bit","none")), block.west().setPermutation(block.west().permutation.withState("rustic:connection_type","inner"))
                    break;
                case "south":
                    if (block.east().hasTag("rustic:customStair") && block.east().permutation.getState("rustic:connection_bit") == "bottom_left_2" && block.east().permutation.getState("minecraft:vertical_half") == verticalHalf && block.east().permutation.getState("minecraft:cardinal_direction") != cardinalDirection) block.east().setPermutation(block.east().permutation.withState("rustic:connection_bit","none")), block.east().setPermutation(block.east().permutation.withState("rustic:connection_type","inner"))
                    if (block.west().hasTag("rustic:customStair") && block.west().permutation.getState("rustic:connection_bit") == "bottom_right_2" && block.west().permutation.getState("minecraft:vertical_half") == verticalHalf && block.west().permutation.getState("minecraft:cardinal_direction") != cardinalDirection) block.west().setPermutation(block.west().permutation.withState("rustic:connection_bit","none")), block.west().setPermutation(block.west().permutation.withState("rustic:connection_type","inner"))
                    break;
                case "east":
                    //console.warn(block.north().typeId)
                    if (block.north().hasTag("rustic:customStair") && block.north().permutation.getState("rustic:connection_bit") == "bottom_right_2" && block.north().permutation.getState("minecraft:vertical_half") == verticalHalf && block.north().permutation.getState("minecraft:cardinal_direction") != cardinalDirection) block.north().setPermutation(block.north().permutation.withState("rustic:connection_bit","none")), block.north().setPermutation(block.north().permutation.withState("rustic:connection_type","inner"))
                    if (block.south().hasTag("rustic:customStair") && block.south().permutation.getState("rustic:connection_bit") == "bottom_right_1" && block.south().permutation.getState("minecraft:vertical_half") == verticalHalf && block.south().permutation.getState("minecraft:cardinal_direction") != cardinalDirection) block.south().setPermutation(block.south().permutation.withState("rustic:connection_bit","none")), block.south().setPermutation(block.south().permutation.withState("rustic:connection_type","inner"))
                    break;
                case "west":
                    //console.warn(block.north().typeId)
                    if (block.north().hasTag("rustic:customStair") && block.north().permutation.getState("rustic:connection_bit") == "bottom_left_2" && block.north().permutation.getState("minecraft:vertical_half") == verticalHalf && block.north().permutation.getState("minecraft:cardinal_direction") != cardinalDirection) block.north().setPermutation(block.north().permutation.withState("rustic:connection_bit","none")), block.north().setPermutation(block.north().permutation.withState("rustic:connection_type","inner"))
                    if (block.south().hasTag("rustic:customStair") && block.south().permutation.getState("rustic:connection_bit") == "bottom_left_1" && block.south().permutation.getState("minecraft:vertical_half") == verticalHalf && block.south().permutation.getState("minecraft:cardinal_direction") != cardinalDirection) block.south().setPermutation(block.south().permutation.withState("rustic:connection_bit","none")), block.south().setPermutation(block.south().permutation.withState("rustic:connection_type","inner"))
                    break;
            }
        }
    });
});