import { world, StructureAnimationMode } from '@minecraft/server';
import { c as clearItem } from './utils-d4218765.js';

const offSet = [
    { name: "minecraft:allium", position: [2, 2] },
    { name: "minecraft:azure_bluet", position: [2, 4] },
    { name: "minecraft:bamboo", position: [0, 0] },
    { name: "minecraft:blue_orchid", position: [2, 6] },
    { name: "minecraft:cornflower", position: [2, 2] },
    { name: "minecraft:lily_of_the_valley", position: [2, 4] },
    { name: "minecraft:melon_block", position: [1, 1] },
    { name: "minecraft:orange_tulip", position: [2, 2] },
    { name: "minecraft:oxeye_daisy", position: [2, 2] },
    { name: "minecraft:pink_tulip", position: [2, 2] },
    { name: "minecraft:poppy", position: [2, 2] },
    { name: "minecraft:pumpkin", position: [1, 1] },
    { name: "minecraft:red_tulip", position: [2, 2] },
    { name: "minecraft:reeds", position: [0, 0] },
    { name: "minecraft:sunflower", position: [1, 4] },
    { name: "minecraft:white_tulip", position: [2, 2] },
    { name: "minecraft:wither_rose", position: [2, 2] },
    { name: "minecraft:yellow_flower", position: [2, 2] },
];
function getPosition(blockID) {
    const result = offSet.find(({ name }) => name === blockID);
    if (result)
        return result["position"];
    else
        return undefined;
}
function checkVector(vector, location) {
    return location.x === vector.x && location.y === vector.y && location.z === vector.z;
}
function spawnParticleBorder(location, dimension, size, particleName) {
    dimension.playSound("note.snare", location, { pitch: 2 });
    let cords = [
        { direction: [location.y, location.y + size.y, "y"], position: [location.x, location.z] },
        { direction: [location.y, location.y + size.y, "y"], position: [location.x + size.x, location.z] },
        { direction: [location.y, location.y + size.y, "y"], position: [location.x, location.z + size.z] },
        { direction: [location.y, location.y + size.y, "y"], position: [location.x + size.x, location.z + size.z] },
        { direction: [location.z, location.z + size.z, "z"], position: [location.x, location.y] },
        { direction: [location.z, location.z + size.z, "z"], position: [location.x + size.x, location.y] },
        { direction: [location.z, location.z + size.z, "z"], position: [location.x, location.y + size.y] },
        { direction: [location.z, location.z + size.z, "z"], position: [location.x + size.x, location.y + size.y] },
        { direction: [location.x, location.x + size.x, "x"], position: [location.y, location.z] },
        { direction: [location.x, location.x + size.x, "x"], position: [location.y + size.y, location.z] },
        { direction: [location.x, location.x + size.x, "x"], position: [location.y, location.z + size.z] },
        { direction: [location.x, location.x + size.x, "x"], position: [location.y + size.y, location.z + size.z] },
    ];
    cords.forEach((element) => {
        let pos1 = element.position[0];
        let pos2 = element.position[1];
        for (let pos3 = element.direction[0]; pos3 < element.direction[1]; pos3++) {
            switch (element.direction[2]) {
                case "y":
                    dimension.spawnParticle(particleName, { x: pos1, y: pos3 + 0.5, z: pos2 });
                    break;
                case "x":
                    dimension.spawnParticle(particleName, { x: pos3, y: pos1 + 0.5, z: pos2 });
                    break;
                case "z":
                    dimension.spawnParticle(particleName, { x: pos1, y: pos2 + 0.5, z: pos3 });
                    break;
            }
        }
    });
}
function checkAir(location, dimension, size, blockLocation, blockID) {
    for (let x = location.x; x < location.x + size.x; x++) {
        for (let y = location.y; y < location.y + size.y; y++) {
            for (let z = location.z; z < location.z + size.z; z++) {
                const blockPos = { x: x, y: y, z: z };
                if (!dimension.getBlock(blockPos).isAir && !checkVector(blockLocation, blockPos)) {
                    if (dimension.getBlock(blockPos).typeId === blockID) {
                        let newLocation = { x: blockLocation.x, y: blockLocation.y + 1, z: blockLocation.z };
                        if (!checkVector(newLocation, blockPos)) {
                            spawnParticleBorder(location, dimension, size, "minecraft:falling_border_dust_particle");
                            return false;
                        }
                    }
                    else {
                        spawnParticleBorder(location, dimension, size, "minecraft:falling_border_dust_particle");
                        return false;
                    }
                }
            }
        }
    }
    return true;
}
function randomBoneMeal() {
    const num = Math.random();
    if (num < 0.5)
        return true;
    else
        return false;
}
function spawnParticle(dimension, location, type) {
    dimension.spawnParticle(type, {
        x: location.x + 0.5,
        y: location.y + 0.5,
        z: location.z + 0.5,
    });
}
world.afterEvents.itemUse.subscribe((event) => {
    const block = event.source.getBlockFromViewDirection({
        maxDistance: 7,
    });
    if (!block) {
        return;
    }
    const blockID = block.block.typeId;
    if (event.itemStack.typeId === "ftb_bees:flower_fertilizer") {
        const position = getPosition(blockID);
        const flowerName = blockID.split(":")[1];
        let particleName = "minecraft:crop_growth_emitter";
        if (position) {
            const location = block.block.location;
            const dimension = block.block.dimension;
            const structureLocation = { x: location.x - position[0], y: location.y, z: location.z - position[1] };
            const structureSize = world.structureManager.get(`mystructure:ftb_bees/big_${flowerName}`).size;
            const canPlace = checkAir(structureLocation, dimension, structureSize, location, blockID);
            if (randomBoneMeal()) {
                if (canPlace) {
                    clearItem(event.source, event.itemStack.typeId);
                    dimension.runCommand(`setblock ${location.x} ${location.y} ${location.z} air replace`);
                    world.structureManager.place(`mystructure:ftb_bees/big_${flowerName}`, dimension, structureLocation, {
                        animationMode: StructureAnimationMode.Blocks,
                        animationSeconds: Math.floor(Math.random() * 2) + 1,
                    });
                }
            }
            spawnParticle(dimension, location, particleName);
        }
    }
});
