import { world, } from "@minecraft/server";
import { namespace } from "../../../config/_config";
export class TableConnectBlock {
    constructor() {
        TableConnectBlock.initialize();
    }
    static initialize() {
        world.beforeEvents.worldInitialize.subscribe((event) => {
            event.blockComponentRegistry.registerCustomComponent(`${namespace}:table_connect`, {
                // Handle events before a player places a block
                beforeOnPlayerPlace: (event) => this.handleBeforePlacement(event),
                // Handle events when a block is placed
                onPlace: (event) => this.handleOnPlace(event),
                // Handle events when a block is destroyed
                onPlayerDestroy: (event) => this.handleOnDestroy(event),
            });
        });
    }
    static handleBeforePlacement(event) {
        const northBlock = event.block.north();
        const southBlock = event.block.south();
        const eastBlock = event.block.east();
        const westBlock = event.block.west();
        if (northBlock?.hasTag(this.tag)) {
            event.permutationToPlace = event.permutationToPlace.withState(`${namespace}:connect_north`, true);
        }
        if (southBlock?.hasTag(this.tag)) {
            event.permutationToPlace = event.permutationToPlace.withState(`${namespace}:connect_south`, true);
        }
        if (eastBlock?.hasTag(this.tag)) {
            event.permutationToPlace = event.permutationToPlace.withState(`${namespace}:connect_east`, true);
        }
        if (westBlock?.hasTag(this.tag)) {
            event.permutationToPlace = event.permutationToPlace.withState(`${namespace}:connect_west`, true);
        }
        this.determineLegsBeforePlacement(event, northBlock, southBlock, eastBlock, westBlock);
    }
    static handleOnPlace(event) {
        const block = event.block;
        const northBlock = block.north();
        const southBlock = block.south();
        const eastBlock = block.east();
        const westBlock = block.west();
        this.determineLegs(northBlock);
        this.determineLegs(northBlock.east());
        this.determineLegs(northBlock.west());
        this.determineLegs(southBlock);
        this.determineLegs(southBlock.east());
        this.determineLegs(southBlock.west());
        this.determineLegs(eastBlock);
        this.determineLegs(westBlock);
        if (northBlock?.hasTag(this.tag)) {
            northBlock.setPermutation(northBlock.permutation.withState(`${namespace}:connect_south`, true));
        }
        if (southBlock?.hasTag(this.tag)) {
            southBlock.setPermutation(southBlock.permutation.withState(`${namespace}:connect_north`, true));
        }
        if (eastBlock?.hasTag(this.tag)) {
            eastBlock.setPermutation(eastBlock.permutation.withState(`${namespace}:connect_west`, true));
        }
        if (westBlock?.hasTag(this.tag)) {
            westBlock.setPermutation(westBlock.permutation.withState(`${namespace}:connect_east`, true));
        }
    }
    static handleOnDestroy(event) {
        const block = event.block;
        const northBlock = block.north();
        const southBlock = block.south();
        const eastBlock = block.east();
        const westBlock = block.west();
        this.determineLegs(northBlock);
        this.determineLegs(northBlock.east());
        this.determineLegs(northBlock.west());
        this.determineLegs(southBlock);
        this.determineLegs(southBlock.east());
        this.determineLegs(southBlock.west());
        this.determineLegs(eastBlock);
        this.determineLegs(westBlock);
        if (northBlock?.hasTag(this.tag)) {
            northBlock.setPermutation(northBlock.permutation.withState(`${namespace}:connect_south`, false));
        }
        if (southBlock?.hasTag(this.tag)) {
            southBlock.setPermutation(southBlock.permutation.withState(`${namespace}:connect_north`, false));
        }
        if (eastBlock?.hasTag(this.tag)) {
            eastBlock.setPermutation(eastBlock.permutation.withState(`${namespace}:connect_west`, false));
        }
        if (westBlock?.hasTag(this.tag)) {
            westBlock.setPermutation(westBlock.permutation.withState(`${namespace}:connect_east`, false));
        }
    }
    static determineLegsBeforePlacement(event, northBlock, southBlock, eastBlock, westBlock) {
        const northBlockHasTag = northBlock?.hasTag(this.tag);
        const northEastBlockHasTag = northBlock?.east()?.hasTag(this.tag);
        const northWestBlockHasTag = northBlock?.west()?.hasTag(this.tag);
        const eastBlockHasTag = eastBlock?.hasTag(this.tag);
        const westBlockHasTag = westBlock?.hasTag(this.tag);
        const southBlockHasTag = southBlock?.hasTag(this.tag);
        const southEastBlockHasTag = southBlock?.east()?.hasTag(this.tag);
        const southWestBlockHasTag = southBlock?.west()?.hasTag(this.tag);
        if (northBlock?.hasTag(this.tag)) {
            event.permutationToPlace = event.permutationToPlace.withState(`${namespace}:leg_ne`, false);
            event.permutationToPlace = event.permutationToPlace.withState(`${namespace}:leg_nw`, false);
            if (northBlockHasTag && westBlockHasTag && !northWestBlockHasTag) {
                event.permutationToPlace = event.permutationToPlace.withState(`${namespace}:leg_nw`, true);
            }
            if (northBlockHasTag && eastBlockHasTag && !northEastBlockHasTag) {
                event.permutationToPlace = event.permutationToPlace.withState(`${namespace}:leg_ne`, true);
            }
        }
        if (eastBlock?.hasTag(this.tag)) {
            event.permutationToPlace = event.permutationToPlace.withState(`${namespace}:leg_ne`, false);
            event.permutationToPlace = event.permutationToPlace.withState(`${namespace}:leg_se`, false);
            if (eastBlockHasTag && southBlockHasTag && !southEastBlockHasTag) {
                event.permutationToPlace = event.permutationToPlace.withState(`${namespace}:leg_se`, true);
            }
            if (eastBlockHasTag && northBlockHasTag && !northEastBlockHasTag) {
                event.permutationToPlace = event.permutationToPlace.withState(`${namespace}:leg_ne`, true);
            }
        }
        if (southBlock?.hasTag(this.tag)) {
            event.permutationToPlace = event.permutationToPlace.withState(`${namespace}:leg_se`, false);
            event.permutationToPlace = event.permutationToPlace.withState(`${namespace}:leg_sw`, false);
            if (southBlockHasTag && eastBlockHasTag && !southEastBlockHasTag) {
                event.permutationToPlace = event.permutationToPlace.withState(`${namespace}:leg_se`, true);
            }
            if (southBlockHasTag && westBlockHasTag && !southWestBlockHasTag) {
                event.permutationToPlace = event.permutationToPlace.withState(`${namespace}:leg_sw`, true);
            }
        }
        if (westBlock?.hasTag(this.tag)) {
            event.permutationToPlace = event.permutationToPlace.withState(`${namespace}:leg_nw`, false);
            event.permutationToPlace = event.permutationToPlace.withState(`${namespace}:leg_sw`, false);
            if (westBlockHasTag && southBlockHasTag && !southWestBlockHasTag) {
                event.permutationToPlace = event.permutationToPlace.withState(`${namespace}:leg_sw`, true);
            }
            if (westBlockHasTag && northBlockHasTag && !northWestBlockHasTag) {
                event.permutationToPlace = event.permutationToPlace.withState(`${namespace}:leg_nw`, true);
            }
        }
    }
    static determineLegs(block) {
        if (!block.hasTag(this.tag))
            return;
        const northBlock = block.north();
        const northEastBlock = block.north()?.east();
        const northWestBlock = block.north()?.west();
        const southBlock = block.south();
        const southEastBlock = block.south()?.east();
        const southWestBlock = block.south()?.west();
        const eastBlock = block.east();
        const westBlock = block.west();
        const northBlockHasTag = northBlock?.hasTag(this.tag);
        const northEastBlockHasTag = northEastBlock?.hasTag(this.tag);
        const northWestBlockHasTag = northWestBlock?.hasTag(this.tag);
        const eastBlockHasTag = eastBlock?.hasTag(this.tag);
        const westBlockHasTag = westBlock?.hasTag(this.tag);
        const southBlockHasTag = southBlock?.hasTag(this.tag);
        const southEastBlockHasTag = southEastBlock?.hasTag(this.tag);
        const southWestBlockHasTag = southWestBlock?.hasTag(this.tag);
        // if (northEastBlockHasTag) {
        //   block.setPermutation(block.permutation.withState(`${namespace}:leg_ne`, false));
        // }
        if (northBlock) {
            if (!eastBlockHasTag) {
                block.setPermutation(block.permutation.withState(`${namespace}:leg_ne`, !northBlockHasTag));
            }
            if (!westBlockHasTag) {
                block.setPermutation(block.permutation.withState(`${namespace}:leg_nw`, !northBlockHasTag));
            }
            if (northBlockHasTag && westBlockHasTag && !northWestBlockHasTag) {
                block.setPermutation(block.permutation.withState(`${namespace}:leg_nw`, northBlockHasTag));
            }
            if (northBlockHasTag && eastBlockHasTag && !northEastBlockHasTag) {
                block.setPermutation(block.permutation.withState(`${namespace}:leg_ne`, northBlockHasTag));
            }
            if (northBlockHasTag && eastBlockHasTag && northEastBlockHasTag) {
                block.setPermutation(block.permutation.withState(`${namespace}:leg_ne`, !northBlockHasTag));
            }
            if (northBlockHasTag && westBlockHasTag && northWestBlockHasTag) {
                block.setPermutation(block.permutation.withState(`${namespace}:leg_nw`, !northBlockHasTag));
            }
        }
        if (eastBlock) {
            if (!northBlockHasTag) {
                block.setPermutation(block.permutation.withState(`${namespace}:leg_ne`, !eastBlockHasTag));
            }
            if (!southBlockHasTag) {
                block.setPermutation(block.permutation.withState(`${namespace}:leg_se`, !eastBlockHasTag));
            }
            if (eastBlockHasTag && southBlockHasTag && !southEastBlockHasTag) {
                block.setPermutation(block.permutation.withState(`${namespace}:leg_se`, southBlockHasTag));
            }
            if (eastBlockHasTag && northBlockHasTag && !northEastBlockHasTag) {
                block.setPermutation(block.permutation.withState(`${namespace}:leg_ne`, eastBlockHasTag));
            }
            if (eastBlockHasTag && northBlockHasTag && northEastBlockHasTag) {
                block.setPermutation(block.permutation.withState(`${namespace}:leg_ne`, !eastBlockHasTag));
            }
            if (eastBlockHasTag && southBlockHasTag && southEastBlockHasTag) {
                block.setPermutation(block.permutation.withState(`${namespace}:leg_se`, !eastBlockHasTag));
            }
        }
        if (southBlock) {
            if (!eastBlockHasTag) {
                block.setPermutation(block.permutation.withState(`${namespace}:leg_se`, !southBlockHasTag));
            }
            if (!westBlockHasTag) {
                block.setPermutation(block.permutation.withState(`${namespace}:leg_sw`, !southBlockHasTag));
            }
            if (southBlockHasTag && eastBlockHasTag && !southEastBlockHasTag) {
                block.setPermutation(block.permutation.withState(`${namespace}:leg_se`, southBlockHasTag));
            }
            if (southBlockHasTag && westBlockHasTag && !southWestBlockHasTag) {
                block.setPermutation(block.permutation.withState(`${namespace}:leg_sw`, southBlockHasTag));
            }
            if (southBlockHasTag && westBlockHasTag && southWestBlockHasTag) {
                block.setPermutation(block.permutation.withState(`${namespace}:leg_sw`, !southBlockHasTag));
            }
            if (southBlockHasTag && eastBlockHasTag && southEastBlockHasTag) {
                block.setPermutation(block.permutation.withState(`${namespace}:leg_se`, !southBlockHasTag));
            }
        }
        if (westBlock) {
            if (!northBlockHasTag) {
                block.setPermutation(block.permutation.withState(`${namespace}:leg_nw`, !westBlockHasTag));
            }
            if (!southBlockHasTag) {
                block.setPermutation(block.permutation.withState(`${namespace}:leg_sw`, !westBlockHasTag));
            }
            if (westBlockHasTag && southBlockHasTag && !southWestBlockHasTag) {
                block.setPermutation(block.permutation.withState(`${namespace}:leg_sw`, southBlockHasTag));
            }
            if (westBlockHasTag && northBlockHasTag && !northWestBlockHasTag) {
                block.setPermutation(block.permutation.withState(`${namespace}:leg_nw`, westBlockHasTag));
            }
            if (westBlockHasTag && northBlockHasTag && northWestBlockHasTag) {
                block.setPermutation(block.permutation.withState(`${namespace}:leg_nw`, !westBlockHasTag));
            }
            if (westBlockHasTag && southBlockHasTag && southWestBlockHasTag) {
                block.setPermutation(block.permutation.withState(`${namespace}:leg_sw`, !westBlockHasTag));
            }
        }
    }
}
TableConnectBlock.tag = `${namespace}:table_connect`;
