import { ItemStack, system, world } from "@minecraft/server";
import { SuppItems } from "../reg/suppItems";
import { McItems } from "../reg/mcItems";
import { giveItemToPlayer, isCreative, shrinkIfInSurvival } from "../util/miscUtils";
import { McBlocks } from "../reg/mcBlocks";
import { SuppStates } from "../reg/suppStates";
const Topping = "razz_sup:pancake_topping";
function addTopping(block, item, player, newTopping, newItemType) {
    const toppings = block.permutation.getState(Topping);
    if (toppings != "none")
        return;
    system.run(() => {
        const level = block.dimension;
        level.setBlockPermutation(block.location, block.permutation.withState(Topping, newTopping));
        shrinkIfInSurvival(player, item);
        if (newItemType && !isCreative(player)) {
            giveItemToPlayer(player, new ItemStack(newItemType));
        }
        level.playSound("use.honey_block", block.center(), { volume: 1, pitch: 1 });
    });
    return true;
}
world.beforeEvents.playerInteractWithBlock.subscribe((event) => {
    if (!event.isFirstEvent)
        return;
    const block = event.block;
    if (block.typeId != "razz_sup:pancake" /* SuppBlocks.Pancakes */)
        return;
    const level = block.dimension;
    const player = event.player;
    const item = event.itemStack;
    if (item) {
        if (item.typeId == SuppItems.Chocolate) {
            addTopping(block, item, player, "chocolate");
            return;
        }
        else if (item.typeId == McItems.HoneyBottle) {
            addTopping(block, item, player, "honey", McItems.Bottle);
            return;
        }
        else if (item.typeId == McItems.SweetBerries) {
            addTopping(block, item, player, "berries");
            return;
        }
        if (item.typeId == "razz_sup:pancake" /* SuppBlocks.Pancakes */)
            return;
    }
    const currentLayerCount = block.permutation.getState('razz_sup:layer_count');
    if (currentLayerCount === undefined || currentLayerCount <= -1)
        return false;
    const newLayerCount = currentLayerCount - 1;
    system.run(() => {
        player.addEffect("saturation", 20, {
            showParticles: false,
            amplifier: 1,
        });
        level.playSound("random.burp", player.location, { volume: 1, pitch: 0.95 });
        if (newLayerCount <= 0) {
            block.dimension.setBlockType(block.location, McBlocks.Air);
        }
        else {
            block.setPermutation(block.permutation.withState(SuppStates.LayerCount, newLayerCount));
        }
    });
    event.cancel = true;
});
