import CustomVector3 from "../../modules/calc/vector3";
import { namespace } from "../_config";
export const wateringCan = (player, fertilize, range, accuracyPct) => {
    const targetBlock = player.getBlockFromViewDirection({
        maxDistance: 9,
        includePassableBlocks: false,
    })?.block;
    if (!targetBlock)
        return;
    const targetLocation = CustomVector3.fromMC(targetBlock.location);
    const dimension = targetBlock.dimension;
    // Get all blocks within the range
    const blocksInRange = [];
    for (let x = -range; x <= range; x++) {
        for (let y = -range; y <= range; y++) {
            for (let z = -range; z <= range; z++) {
                const offset = new CustomVector3(x, y, z);
                const blockLocation = targetLocation.add(offset);
                const block = dimension.getBlock(blockLocation);
                // Check if the block is within a circular area
                if (block && CustomVector3.distance(targetLocation, blockLocation) <= range) {
                    blocksInRange.push(block);
                }
            }
        }
    }
    // Filter blocks based on accuracy percentage
    const filteredBlocks = blocksInRange.filter(() => Math.random() * 100 < accuracyPct);
    filteredBlocks.forEach((block) => {
        block.above()?.dimension.spawnParticle("cc:water_effect", block.above()?.bottomCenter());
        if (block.typeId === "minecraft:farmland") {
            const moisture = block.permutation.getState("moisturized_amount");
            if (moisture < 7) {
                block.setPermutation(block.permutation.withState("moisturized_amount", moisture + 1));
            }
        }
        if (fertilize === true) {
            if (block.permutation.getState("growth") !== undefined) {
                const state = block.permutation.getState("growth");
                if (state < 7) {
                    block.setPermutation(block.permutation.withState("growth", state + 1));
                }
            }
            if (block.permutation.getState("cc:grow_state") !== undefined) {
                const state = block.permutation.getState(`${namespace}:grow_state`);
                if (state < 4) {
                    block.setPermutation(block.permutation.withState(`${namespace}:grow_state`, state + 1));
                }
            }
        }
    });
};
