import { Direction, ItemStack, world } from "@minecraft/server";
import { SuppStates } from "../reg/suppStates";
import { randomInt } from "../util/mthUtils";
import { breakBlock, getItemInHand, shrinkIfInSurvival } from "../util/miscUtils";
import { McItems } from "../reg/mcItems";
import { suppEvents } from "../suppEventHandler";
import { McParticles } from "../reg/mcParticles";
import { offsetPos } from "../util/dirUtils";
import { McBlocks } from "../reg/mcBlocks";
import { configs } from "../configs";
import { SuppItems } from "../reg/suppItems";
const MAX_GROWTH = 7;
const DOUBLE_AGE = 4;
function growBy(block, growBy) {
    const growth = block.permutation.getState(SuppStates.Growth);
    const nenGrowth = Math.min(growth + growBy, MAX_GROWTH);
    if (nenGrowth == growth) {
        return false;
    }
    if (nenGrowth >= DOUBLE_AGE) {
        const above = block.above();
        if (!above || (!above.isAir && above.typeId != "razz_sup:flax_upper" /* SuppBlocks.FlaxUpper */)) {
            return false;
        }
        above.setType("razz_sup:flax_upper" /* SuppBlocks.FlaxUpper */);
    }
    block.setPermutation(block.permutation.withState(SuppStates.Growth, Math.min(nenGrowth, MAX_GROWTH)));
    return true;
}
function performBoneMeal(player, block) {
    if (!player || !block)
        return;
    const item = getItemInHand(player);
    if (!item || item.typeId != McItems.BoneMeal)
        return;
    if (growBy(block, randomInt(1, 3))) {
        shrinkIfInSurvival(player, item);
        // Play effects
        const effectLocation = block.center();
        const dimension = block.dimension;
        dimension.playSound("item.bone_meal.use", effectLocation);
        dimension.spawnParticle(McParticles.BoneMeal, effectLocation);
    }
}
/** @type {import("@minecraft/server").BlockCustomComponent} */
export const FlaxCropGrowthBlockComponent = {
    onRandomTick(event) {
        const block = event.block;
        const growthChance = 1; //increase growth
        if (Math.random() > growthChance)
            return;
        growBy(block, 1);
    },
    onPlayerInteract(event) {
        const player = event.player;
        const block = event.block;
        performBoneMeal(player, block);
    }
};
export const FlaxUpperComponent = {
    onPlayerInteract(event) {
        const player = event.player;
        const block = event.block;
        performBoneMeal(player, block.below());
    }
};
suppEvents.subscribeOnNeighborChanged((event) => {
    const block = event.block;
    const isUpper = block.typeId == "razz_sup:flax_upper" /* SuppBlocks.FlaxUpper */;
    const isFlax = block.typeId == "razz_sup:flax_crop" /* SuppBlocks.Flax */;
    if (isFlax || isUpper) {
        const dir = isUpper ? Direction.Down : Direction.Up;
        if (event.fromDir == dir) {
            const other = offsetPos(block, dir);
            if (!other || other.typeId != (isUpper ? "razz_sup:flax_crop" /* SuppBlocks.Flax */ : "razz_sup:flax_upper" /* SuppBlocks.FlaxUpper */)) {
                breakBlock(block);
            }
        }
    }
});
world.afterEvents.playerBreakBlock.subscribe((event) => {
    if (event.brokenBlockPermutation.type.id == McBlocks.TallGrass) {
        if (Math.random() < (configs.flaxDropChance.getValue() / 100)) {
            event.block.dimension.spawnItem(new ItemStack(SuppItems.FlaxSeeds, 1), event.block.center());
        }
    }
});
