import { breakBlock, damageItemIfInSurvival, getItemInHand, isShovel, shrinkIfInSurvival } from "../util/miscUtils";
import { McItems } from "../reg/mcItems";
import { McParticles } from "../reg/mcParticles";
import { suppEvents } from "../suppEventHandler";
import { SuppTags } from "../reg/suppTags";
import { directionFromString, flipDir } from "../util/dirUtils";
import { SuppStates } from "../reg/suppStates";
function getParticleOffsets(block) {
    const face = block.permutation.getState("minecraft:block_face");
    switch (face) {
        case "north":
            return [0.5, 13 / 16, 1 - 4 / 16];
        case "south":
            return [0.5, 13 / 16, 4 / 16];
        case "west":
            return [1 - 4 / 16, 13 / 16, 0.5];
        case "east":
            return [4 / 16, 13 / 16, 0.5];
        default:
            return [0.5, 11 / 16, 0.5];
    }
}
function spawnParticles(block) {
    const level = block.dimension;
    const offset = getParticleOffsets(block);
    const pos = { x: block.x + offset[0], y: block.y + offset[1], z: block.z + offset[2] };
    level.spawnParticle(McParticles.Smoke, pos);
    // level.playSound("ambient.candle", block.center());
}
export const SconceParticleComponent = {
    onTick(event) {
        const block = event.block;
        if (!block.isValid())
            return;
        if (block.permutation.getState(SuppStates.Lit)) { //aso this should be a minecraft prop boolean
            if (Math.random() < 0.4) {
                spawnParticles(block);
            }
        }
    },
    onPlayerInteract(event) {
        const block = event.block;
        const player = event.player;
        if (!player)
            return;
        const item = getItemInHand(player);
        if (!item)
            return;
        const isOn = block.permutation.getState(SuppStates.Lit);
        if ((isShovel(item)) && isOn) {
            block.setPermutation(block.permutation.withState(SuppStates.Lit, false));
            block.dimension.playSound("extinguish.candle", block.center());
            spawnParticles(block);
            //   event.cancel = true;
            return;
        }
        else if ((item.typeId == McItems.FlintAndSteel || item.typeId == McItems.FireCharge) && !isOn) {
            block.setPermutation(block.permutation.withState(SuppStates.Lit, true));
            block.dimension.playSound("fire.ignite", block.center());
            if (item.typeId == McItems.FlintAndSteel) {
                damageItemIfInSurvival(player, item);
            }
            else {
                shrinkIfInSurvival(player, item);
            }
            //spawn particles immediately
            spawnParticles(block);
            //   event.cancel = true;
            return;
        }
    }
};
export const CandleHolderItemComponent = {
    onUseOn(event) {
        //just needed for hand swing
        //also not ideal because adding this will make the hand will swing regardless
    }
};
suppEvents.subscribeOnNeighborChanged((event) => {
    const block = event.block;
    if (block.hasTag(SuppTags.Sconces)) {
        const dir = directionFromString(block.permutation.getState("minecraft:block_face"));
        if (dir == flipDir(event.fromDir) && event.neighborBlock.isAir) {
            breakBlock(block);
        }
    }
});
