import { SuppStates } from "../reg/suppStates";
import { random } from "../util/mthUtils";
import { damageItemIfInSurvival, getItemInHand, isShovel, shrinkIfInSurvival } from "../util/miscUtils";
import { McItems } from "../reg/mcItems";
import { McParticles } from "../reg/mcParticles";
function spawnParticles(block, amount = 1) {
    const center = block.center();
    for (let i = 0; i < amount; i++) {
        const pos = {
            x: center.x + random(-0.35, 0.35),
            y: center.y + 0.3 + random(-0.35, 0.35),
            z: center.z + random(-0.35, 0.35)
        };
        block.dimension.spawnParticle(McParticles.Smoke, pos);
    }
}
export const BrazierComponent = {
    onTick(event) {
        const block = event.block;
        if (!block.isValid())
            return;
        if (block.permutation.getState(SuppStates.Lit)) { //aso this should be a minecraft prop boolean
            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, 5);
            //   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;
        }
    }
};
