import { BlockPermutation, Direction, } from '@minecraft/server';
import { decreaseItemAmountInHand } from '../../util/utils';
import WBVector3 from '../../util/wbVector3';
export function directionToVector(direction) {
    switch (direction) {
        case Direction.Down:
            return { x: 0, y: -1, z: 0 };
        case Direction.Up:
            return { x: 0, y: 1, z: 0 };
        case Direction.North:
            return { x: 0, y: 0, z: -1 };
        case Direction.East:
            return { x: 1, y: 0, z: 0 };
        case Direction.South:
            return { x: 0, y: 0, z: 1 };
        case Direction.West:
            return { x: -1, y: 0, z: 0 };
        default:
            throw new Error(`Unknown direction ${direction}`);
    }
}
export default class AshLayerComponent {
    beforeOnPlayerPlace(e) {
        if (!e.player)
            return;
        const offset = WBVector3.fromMC(directionToVector(e.face)).multiply(-1);
        const placedOn = e.dimension.getBlock(offset.add(e.block));
        if (!placedOn)
            return;
        if (placedOn.typeId !== 'twb_wisu:ash_layer')
            return;
        const level = placedOn.permutation.getState('twb_wisu:level');
        if (level === 8)
            return;
        decreaseItemAmountInHand(e.player, 1, true);
        placedOn.setPermutation((level === 7)
            ? BlockPermutation.resolve('twb_wisu:ash')
            : placedOn.permutation.withState('twb_wisu:level', level + 1));
        e.dimension.playSound('block.sand.place', placedOn);
        e.cancel = true;
    }
}
