import { ItemStack, system, world } from "@minecraft/server";
import { SuppStates } from "../reg/suppStates";
import { McItems } from "../reg/mcItems";
import { McBlocks } from "../reg/mcBlocks";
import { getItemInHand, swapItem } from "../util/miscUtils";
import { SuppItems } from "../reg/suppItems";
import { SuppParticles } from "../reg/suppParticles";
world.beforeEvents.playerInteractWithBlock.subscribe((event) => {
    if (!event.isFirstEvent)
        return;
    const item = event.itemStack;
    if (!item)
        return;
    const block = event.block;
    if (block.typeId == McBlocks.Cauldron) {
        if (item.typeId == McItems.MilkBucket) {
            system.run(() => {
                block.setType("razz_sup:milk_cauldron" /* SuppBlocks.MilkCauldron */);
                block.dimension.playSound("bucket.empty_water", block.center());
                swapItem(event.player, item, McItems.EmptyBucket);
            });
        }
        else if (item.typeId == SuppItems.ChocolateBucket) {
            system.run(() => {
                block.setType("razz_sup:chocolate_milk_cauldron" /* SuppBlocks.ChocolateCauldron */);
                block.dimension.playSound("bucket.empty_water", block.center());
                swapItem(event.player, item, McItems.EmptyBucket);
            });
        }
    }
});
export const MilkCauldronComponent = {
    onRandomTick: (event) => {
        const level = event.block.permutation.getState(SuppStates.CoagulationLevel);
        if (level < 4) {
            event.dimension.spawnParticle(SuppParticles.CheeseFerment, event.block.center());
            event.block.setPermutation(event.block.permutation.withState(SuppStates.CoagulationLevel, level + 1));
        }
    },
    onPlayerInteract: (event) => {
        const block = event.block;
        const player = event.player;
        if (!player)
            return;
        const item = getItemInHand(player);
        const level = block.permutation.getState(SuppStates.CoagulationLevel);
        if (!item) {
            if (level == 4) {
                block.setType(McBlocks.Cauldron);
                //TODO: sound
                const itemStack = new ItemStack(SuppItems.Cheese, 3);
                block.dimension.spawnItem(itemStack, block.center());
                block.dimension.playSound("random.pop", block.center());
            }
        }
        else if (item.typeId == McItems.EmptyBucket && level < 4) {
            block.setType(McBlocks.Cauldron);
            block.dimension.playSound("bucket.empty_water", block.center());
            swapItem(player, item, McItems.MilkBucket);
        }
    }
};
export const ChocolateMilkCauldronComponent = {
    onPlayerInteract: (event) => {
        const block = event.block;
        const player = event.player;
        if (!player)
            return;
        const item = getItemInHand(player);
        if (item.typeId == McItems.EmptyBucket) {
            block.setType(McBlocks.Cauldron);
            block.dimension.playSound("bucket.empty_water", block.center());
            swapItem(player, item, SuppItems.ChocolateBucket);
        }
    }
};
export const CheeseWheelComponent = {
    onPlayerInteract: (event) => {
        const block = event.block;
        const bites = block.permutation.getState(SuppStates.CheeseBites);
        block.dimension.playSound("random.burp", event.player.location, { volume: 1, pitch: 0.95 });
        if (bites < 3) {
            //TODO: saturation
            block.setPermutation(block.permutation.withState(SuppStates.CheeseBites, bites + 1));
            //
        }
        else {
            block.setType(McBlocks.Air);
        }
    }
};
