import { BlockPermutation, system, world } from "@minecraft/server";
import { flipDir, isAxisVertical, offsetPos, setADirectionState } from "../util/dirUtils";
import { McBlocks } from "../reg/mcBlocks";
import { hasStrongClickAction, isFence, isWall } from "../util/blockProperties";
import { isBlockSolid, playSwingArm, shrinkIfInSurvival } from "../util/miscUtils";
import { getPlacementPosition, replaceBlock } from "../util/placementUtils";
function getConnection(behind, face) {
    if (true)
        return "block"; //walls always connect.... thanks mojang
    if (isWall(behind.typeId)) {
        return "wall";
    }
    if (isFence(behind.typeId)) {
        return "fence";
    }
    if (isBlockSolid(behind, face)) {
        return "block";
    }
    return undefined;
}
world.beforeEvents.playerInteractWithBlock.subscribe((event) => {
    if (!event.isFirstEvent)
        return;
    const item = event.itemStack;
    if (!item)
        return;
    const isLantern = item.typeId == McBlocks.Lantern;
    const isSoulLantern = item.typeId == McBlocks.SoulLantern;
    if (!isLantern && !isSoulLantern)
        return;
    const block = event.block;
    const face = event.blockFace;
    if (isAxisVertical(face))
        return;
    const level = block.dimension;
    const player = event.player;
    let targetBlock = getPlacementPosition(block, face);
    const behind = offsetPos(targetBlock, flipDir(face));
    const connection = getConnection(behind, face);
    if (targetBlock && connection && (player.isSneaking || !hasStrongClickAction(behind))) {
        event.cancel = true;
        system.run(() => {
            let permutation = BlockPermutation.resolve(isLantern ? "razz_sup:wall_lantern" /* SuppBlocks.WallLantern */ : "razz_sup:wall_lantern_soul" /* SuppBlocks.WallLanternSoul */);
            permutation = setADirectionState(permutation, flipDir(face));
            replaceBlock(targetBlock, permutation);
            level.playSound("block.lantern.place", targetBlock.center(), { pitch: 1, volume: 0.8 });
            shrinkIfInSurvival(player, item);
            playSwingArm(player);
        });
    }
});
