import { system, world, Direction } from '@minecraft/server';
import { decrementStack, getOppositeDirection, DirectionType, cardinalSides } from './utils/helper';
import { directionToVector3 } from './utils/math';

world.beforeEvents.worldInitialize.subscribe(initEvent => {
  initEvent.blockComponentRegistry.registerCustomComponent('pa:escalera', {
  beforeOnPlayerPlace: e => { let face = e.permutationToPlace.getState('minecraft:cardinal_direction');
let half = e.permutationToPlace.getState('minecraft:vertical_half');
let blockPermutation = e.permutationToPlace;
let blockToCheck;
switch (face) {
    case 'north':
        {
            for (const direction of DirectionType.HORIZONTAL) {
                blockToCheck = e.block.offset(directionToVector3(direction));
                if (blockToCheck.typeId !== blockPermutation.type.id)
                    continue;
                const blockToCheckHalf = blockToCheck.permutation.getState('minecraft:vertical_half');
                if (blockToCheckHalf !== half)
                    continue;
                const blockToCheckDirection = blockToCheck.permutation.getState('minecraft:cardinal_direction');
                switch (direction) {
                    case Direction.North:
                        {
                            if (blockToCheckDirection === 'west') {
                                blockPermutation =
                                    blockPermutation.withState('pa:shape', 'outer_left');
                            }
                            if (blockToCheckDirection === 'east') {
                                blockPermutation =
                                    blockPermutation.withState('pa:shape', 'outer_right');
                            }
                        }
                        break;
                    case Direction.South:
                        {
                            if (blockToCheckDirection === 'east') {
                                blockPermutation =
                                    blockPermutation.withState('pa:shape', 'inner_left');
                            }
                            if (blockToCheckDirection === 'west') {
                                blockPermutation =
                                    blockPermutation.withState('pa:shape', 'inner_right');
                            }
                        }
                        break;
                    case Direction.East:
                        {
                            if (blockToCheckDirection === 'east') {
                                blockToCheck.setPermutation(blockToCheck.permutation.withState('pa:shape', 'inner_right'));
                            }
                            if (blockToCheckDirection === 'west') {
                                blockToCheck.setPermutation(blockToCheck.permutation.withState('pa:shape', 'outer_right'));
                            }
                        }
                        break;
                    case Direction.West:
                        {
                            if (blockToCheckDirection === 'west') {
                                blockToCheck.setPermutation(blockToCheck.permutation.withState('pa:shape', 'inner_left'));
                            }
                            if (blockToCheckDirection === 'east') {
                                blockToCheck.setPermutation(blockToCheck.permutation.withState('pa:shape', 'outer_left'));
                            }
                        }
                        break;
                    default:
                        break;
                }
            }
        }
        break;
    case 'south':
        {
            blockPermutation = blockPermutation.withState('pa:south', true);
            for (const direction of DirectionType.HORIZONTAL) {
                blockToCheck = e.block.offset(directionToVector3(direction));
                if (blockToCheck.typeId !== blockPermutation.type.id)
                    continue;
                const blockToCheckHalf = blockToCheck.permutation.getState('minecraft:vertical_half');
                if (blockToCheckHalf !== half)
                    continue;
                const blockToCheckDirection = blockToCheck.permutation.getState('minecraft:cardinal_direction');
                switch (direction) {
                    case Direction.North:
                        {
                            if (blockToCheckDirection === 'west') {
                                blockPermutation =
                                    blockPermutation.withState('pa:shape', 'inner_left');
                            }
                            if (blockToCheckDirection === 'east') {
                                blockPermutation =
                                    blockPermutation.withState('pa:shape', 'inner_right');
                            }
                        }
                        break;
                    case Direction.South:
                        {
                            if (blockToCheckDirection === 'east') {
                                blockPermutation =
                                    blockPermutation.withState('pa:shape', 'outer_left');
                            }
                            if (blockToCheckDirection === 'west') {
                                blockPermutation =
                                    blockPermutation.withState('pa:shape', 'outer_right');
                            }
                        }
                        break;
                    case Direction.East:
                        {
                            if (blockToCheckDirection === 'east') {
                                blockToCheck.setPermutation(blockToCheck.permutation.withState('pa:shape', 'inner_left'));
                            }
                            if (blockToCheckDirection === 'west') {
                                blockToCheck.setPermutation(blockToCheck.permutation.withState('pa:shape', 'outer_left'));
                            }
                        }
                        break;
                    case Direction.West:
                        {
                            if (blockToCheckDirection === 'west') {
                                blockToCheck.setPermutation(blockToCheck.permutation.withState('pa:shape', 'inner_right'));
                            }
                            if (blockToCheckDirection === 'east') {
                                blockToCheck.setPermutation(blockToCheck.permutation.withState('pa:shape', 'outer_right'));
                            }
                        }
                        break;
                    default:
                        break;
                }
            }
        }
        break;
    case 'east':
        {
            for (const direction of DirectionType.HORIZONTAL) {
                blockToCheck = e.block.offset(directionToVector3(direction));
                if (blockToCheck.typeId !== blockPermutation.type.id)
                    continue;
                const blockToCheckHalf = blockToCheck.permutation.getState('minecraft:vertical_half');
                if (blockToCheckHalf !== half)
                    continue;
                const blockToCheckDirection = blockToCheck.permutation.getState('minecraft:cardinal_direction');
                switch (direction) {
                    case Direction.East:
                        {
                            if (blockToCheckDirection === 'north') {
                                blockPermutation =
                                    blockPermutation.withState('pa:shape', 'outer_left');
                            }
                            if (blockToCheckDirection === 'south') {
                                blockPermutation =
                                    blockPermutation.withState('pa:shape', 'outer_right');
                            }
                        }
                        break;
                    case Direction.West:
                        {
                            if (blockToCheckDirection === 'south') {
                                blockPermutation =
                                    blockPermutation.withState('pa:shape', 'inner_left');
                            }
                            if (blockToCheckDirection === 'north') {
                                blockPermutation =
                                    blockPermutation.withState('pa:shape', 'inner_right');
                            }
                        }
                        break;
                    case Direction.North:
                        {
                            if (blockToCheckDirection === 'north') {
                                blockToCheck.setPermutation(blockToCheck.permutation.withState('pa:shape', 'inner_left'));
                            }
                            if (blockToCheckDirection === 'south') {
                                blockToCheck.setPermutation(blockToCheck.permutation.withState('pa:shape', 'outer_left'));
                            }
                        }
                        break;
                    case Direction.South:
                        {
                            if (blockToCheckDirection === 'north') {
                                blockToCheck.setPermutation(blockToCheck.permutation.withState('pa:shape', 'outer_right'));
                            }
                            if (blockToCheckDirection === 'south') {
                                blockToCheck.setPermutation(blockToCheck.permutation.withState('pa:shape', 'inner_right'));
                            }
                        }
                        break;
                    default:
                        break;
                }
            }
        }
        break;
    case 'west':
        {
            for (const direction of DirectionType.HORIZONTAL) {
                blockToCheck = e.block.offset(directionToVector3(direction));
                if (blockToCheck.typeId !== blockPermutation.type.id)
                    continue;
                const blockToCheckHalf = blockToCheck.permutation.getState('minecraft:vertical_half');
                if (blockToCheckHalf !== half)
                    continue;
                const blockToCheckDirection = blockToCheck.permutation.getState('minecraft:cardinal_direction');
                switch (direction) {
                    case Direction.East:
                        {
                            if (blockToCheckDirection === 'north') {
                                blockPermutation =
                                    blockPermutation.withState('pa:shape', 'inner_left');
                            }
                            if (blockToCheckDirection === 'south') {
                                blockPermutation =
                                    blockPermutation.withState('pa:shape', 'inner_right');
                            }
                        }
                        break;
                    case Direction.West:
                        {
                            if (blockToCheckDirection === 'south') {
                                blockPermutation =
                                    blockPermutation.withState('pa:shape', 'outer_left');
                            }
                            if (blockToCheckDirection === 'north') {
                                blockPermutation =
                                    blockPermutation.withState('pa:shape', 'outer_right');
                            }
                        }
                        break;
                    case Direction.North:
                        {
                            if (blockToCheckDirection === 'north') {
                                blockToCheck.setPermutation(blockToCheck.permutation.withState('pa:shape', 'inner_right'));
                            }
                            if (blockToCheckDirection === 'south') {
                                blockToCheck.setPermutation(blockToCheck.permutation.withState('pa:shape', 'outer_right'));
                            }
                        }
                        break;
                    case Direction.South:
                        {
                            if (blockToCheckDirection === 'north') {
                                blockToCheck.setPermutation(blockToCheck.permutation.withState('pa:shape', 'outer_left'));
                            }
                            if (blockToCheckDirection === 'south') {
                                blockToCheck.setPermutation(blockToCheck.permutation.withState('pa:shape', 'inner_left'));
                            }
                        }
                        break;
                    default:
                        break;
                }
            }
        }
        break;
    default:
        break;
}
e.permutationToPlace = blockPermutation;
e.block.dimension.playSound("use.cherry_wood", e.block.location, { pitch: 0.8, volume: 1.0 });
 },
});

});