import { Direction } from "@minecraft/server";
export const stairsTag = "gs_cb:is_stairs";
export const AllStairs = [];
export function uppercase(string) {
    const start = string[0].toLocaleUpperCase();
    return start + string.slice(1, string.length);
}
export class StairsManager {
    static place(data) {
        const found = AllStairs.find((f) => f.blockId == data.permutationToPlace.type.id);
        if (!found)
            return;
        data.permutationToPlace = data.permutationToPlace.withState(found.displayStateId, false);
    }
    static update(block) {
        const found = AllStairs.find((f) => f.blockId == block.typeId);
        if (!found)
            return;
        let facingDirection = block.permutation.getState("minecraft:cardinal_direction");
        if (!facingDirection)
            return;
        facingDirection = uppercase(facingDirection);
        const { back, front } = this.getConnectedStairs(block, facingDirection, block.permutation.getState("minecraft:vertical_half"));
        let connectedFront = undefined;
        if (front) {
            let frontFacing = front.permutation.getState("minecraft:cardinal_direction");
            if (!frontFacing) {
                block.setPermutation(block.permutation.withState(found.connectedFrontStateId, "none"));
                return;
            }
            frontFacing = uppercase(frontFacing);
            switch (facingDirection) {
                case Direction.North:
                    switch (frontFacing) {
                        case Direction.West:
                            connectedFront = "left";
                            break;
                        case Direction.East:
                            connectedFront = "right";
                            break;
                    }
                    break;
                case Direction.South:
                    switch (frontFacing) {
                        case Direction.West:
                            connectedFront = "right";
                            break;
                        case Direction.East:
                            connectedFront = "left";
                            break;
                    }
                    break;
                case Direction.East:
                    switch (frontFacing) {
                        case Direction.North:
                            connectedFront = "left";
                            break;
                        case Direction.South:
                            connectedFront = "right";
                            break;
                    }
                    break;
                case Direction.West:
                    switch (frontFacing) {
                        case Direction.South:
                            connectedFront = "left";
                            break;
                        case Direction.North:
                            connectedFront = "right";
                            break;
                    }
                    break;
            }
            connectedFront != undefined ? block.setPermutation(block.permutation.withState(found.connectedFrontStateId, connectedFront)) : block.setPermutation(block.permutation.withState(found.connectedFrontStateId, "none"));
        }
        else
            block.setPermutation(block.permutation.withState(found.connectedFrontStateId, "none"));
        if (connectedFront == undefined && back) {
            let backFacing = back.permutation.getState("minecraft:cardinal_direction");
            if (!backFacing) {
                block.setPermutation(block.permutation.withState(found.connectedBackStateId, "none"));
                return;
            }
            backFacing = uppercase(backFacing);
            let connectedBack = undefined;
            switch (facingDirection) {
                case Direction.North:
                    switch (backFacing) {
                        case Direction.West:
                            connectedBack = "left";
                            break;
                        case Direction.East:
                            connectedBack = "right";
                            break;
                    }
                    break;
                case Direction.South:
                    switch (backFacing) {
                        case Direction.West:
                            connectedBack = "right";
                            break;
                        case Direction.East:
                            connectedBack = "left";
                            break;
                    }
                    break;
                case Direction.East:
                    switch (backFacing) {
                        case Direction.North:
                            connectedBack = "left";
                            break;
                        case Direction.South:
                            connectedBack = "right";
                            break;
                    }
                    break;
                case Direction.West:
                    switch (backFacing) {
                        case Direction.South:
                            connectedBack = "left";
                            break;
                        case Direction.North:
                            connectedBack = "right";
                            break;
                    }
                    break;
            }
            connectedBack != undefined ? block.setPermutation(block.permutation.withState(found.connectedBackStateId, connectedBack)) : block.setPermutation(block.permutation.withState(found.connectedBackStateId, "none"));
        }
        else
            block.setPermutation(block.permutation.withState(found.connectedBackStateId, "none"));
    }
    static getConnectedStairs(block, facing, verticalHalf) {
        let frontBlock = undefined;
        let backBlock = undefined;
        facing = uppercase(facing);
        switch (facing) {
            case Direction.North:
                try {
                    frontBlock = block.north(1);
                    backBlock = block.south(1);
                }
                catch { }
                break;
            case Direction.South:
                try {
                    frontBlock = block.south(1);
                    backBlock = block.north(1);
                }
                catch { }
                break;
            case Direction.East:
                try {
                    frontBlock = block.east(1);
                    backBlock = block.west(1);
                }
                catch { }
                break;
            case Direction.West:
                try {
                    frontBlock = block.west(1);
                    backBlock = block.east(1);
                }
                catch { }
                break;
        }
        if (frontBlock && !(frontBlock.hasTag(stairsTag) && frontBlock.permutation.getState("minecraft:vertical_half") == verticalHalf))
            frontBlock = undefined;
        if (backBlock && !(backBlock.hasTag(stairsTag) && backBlock.permutation.getState("minecraft:vertical_half") == verticalHalf))
            backBlock = undefined;
        return { front: frontBlock, back: backBlock };
    }
    static updateConnectedStairs(stairs) {
        const { front, back } = stairs;
        if (front)
            this.update(front);
        if (back)
            this.update(back);
    }
    static updateStairsAround(block) {
        let north = undefined;
        try {
            north = block.north(1);
        }
        catch { }
        let south = undefined;
        try {
            south = block.south(1);
        }
        catch { }
        let east = undefined;
        try {
            east = block.east(1);
        }
        catch { }
        let west = undefined;
        try {
            west = block.west(1);
        }
        catch { }
        const blocks = [north, south, east, west];
        for (const block of blocks) {
            if (block && block.hasTag(stairsTag))
                this.update(block);
        }
    }
}
