import { Direction, system } from "@minecraft/server";
import { AllStairs, StairsManager, uppercase } from "./manager";
export class BlockComponentStairs {
    beforeOnPlayerPlace = (data) => {
        StairsManager.place(data);
        system.runTimeout(() => {
            StairsManager.updateStairsAround(data.block);
        });
        const { block } = data;
        const found = AllStairs.find((f) => f.blockId == data.permutationToPlace.type.id);
        if (!found)
            return;
        let facingDirection = data.permutationToPlace.getState("minecraft:cardinal_direction");
        if (!facingDirection)
            return;
        facingDirection = uppercase(facingDirection);
        const { back, front } = StairsManager.getConnectedStairs(block, facingDirection, data.permutationToPlace.getState("minecraft:vertical_half"));
        let connectedFront = undefined;
        if (front) {
            let frontFacing = front.permutation.getState("minecraft:cardinal_direction");
            if (!frontFacing) {
                data.permutationToPlace = data.permutationToPlace.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 ? data.permutationToPlace = data.permutationToPlace.withState(found.connectedFrontStateId, connectedFront) : data.permutationToPlace = data.permutationToPlace.withState(found.connectedFrontStateId, "none");
        }
        else
            data.permutationToPlace = data.permutationToPlace.withState(found.connectedFrontStateId, "none");
        if (connectedFront == undefined && back) {
            let backFacing = back.permutation.getState("minecraft:cardinal_direction");
            if (!backFacing) {
                data.permutationToPlace = data.permutationToPlace.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 ? data.permutationToPlace = data.permutationToPlace.withState(found.connectedBackStateId, connectedBack) : data.permutationToPlace = data.permutationToPlace.withState(found.connectedBackStateId, "none");
        }
    };
    onPlayerDestroy = (data) => {
        StairsManager.updateStairsAround(data.block);
    };
    connectedFrontStateId;
    connectedBackStateId;
    displayStateId;
    constructor(blockId, connectedFrontStateId, connectedBackStateId, displayStateId) {
        this.connectedFrontStateId = connectedFrontStateId;
        this.connectedBackStateId = connectedBackStateId;
        this.displayStateId = displayStateId;
        AllStairs.push({ blockId: blockId, connectedBackStateId: connectedBackStateId, connectedFrontStateId: connectedFrontStateId, displayStateId: displayStateId });
    }
}
