import { BlockPermutation, world } from "@minecraft/server";
import { SettingsHandler } from "./settings";
const growthSpongeMap = new Map();
const cropTypes = new Set([
    "minecraft:wheat",
    "minecraft:carrots",
    "minecraft:potatoes",
    "minecraft:beetroot",
    "minecraft:nether_wart"
]);
const blockInfoMap = new Map([
    ["minecraft:wheat", { id: "minecraft:wheat", perm: "growth", max: 7 }],
    ["minecraft:carrots", { id: "minecraft:carrots", perm: "growth", max: 7 }],
    ["minecraft:potatoes", { id: "minecraft:potatoes", perm: "growth", max: 7 }],
    ["minecraft:beetroot", { id: "minecraft:beetroot", perm: "growth", max: 7 }],
    ["minecraft:nether_wart", { id: "minecraft:nether_wart", perm: "age", max: 3 }],
]);
function growthSpongeTimer(block) {
    const id = `${block.location.x}_${block.location.y}_${block.location.z}`;
    const now = Date.now();
    if (growthSpongeMap.has(id)) {
        const expiry = growthSpongeMap.get(id);
        if (now > expiry) {
            block.dimension.runCommand(`setblock ${block.location.x} ${block.location.y} ${block.location.z} air destroy`);
            growthSpongeMap.delete(id);
        }
    }
    else {
        growthSpongeMap.set(id, now + 30_000);
    }
}
function spawnTrailParticle(from, to, particleId = "minecraft:happy_villager") {
    const dx = to.x - from.x;
    const dy = to.y - from.y;
    const dz = to.z - from.z;
    const distance = Math.sqrt(dx * dx + dy * dy + dz * dz);
    const steps = Math.ceil(distance / 0.25);
    const dim = world.getDimension("overworld");
    const arcHeight = 1;
    const dropAmount = 0.5;
    for (let i = 0; i <= steps; i++) {
        const t = i / steps;
        const px = from.x + dx * t + 0.5;
        const py = from.y + dy * t + arcHeight * Math.sin(Math.PI * t) * (1 - t) - dropAmount * t + 0.5;
        const pz = from.z + dz * t + 0.5;
        dim.spawnParticle(particleId, { x: px, y: py, z: pz });
    }
    dim.spawnParticle("minecraft:crop_growth_emitter", { x: to.x + 0.5, y: to.y, z: to.z + 0.5 });
}
export function growthSpongePulse(block) {
    // Check if growth sponge is enabled
    if (!SettingsHandler.isSpongeEnabled("growthSponge")) {
        block.dimension.runCommand(`setblock ${block.location.x} ${block.location.y} ${block.location.z} air destroy`);
        // Notify nearby players
        const nearbyPlayers = block.dimension.getPlayers({ location: block.location, maxDistance: 10 });
        for (const player of nearbyPlayers) {
            player.sendMessage("§cGrowth Sponge is disabled in settings!");
        }
        return;
    }
    growthSpongeTimer(block);
    const pos = block.location;
    const dim = block.dimension;
    const overworld = world.getDimension("overworld");
    const radius = 7;
    const maxTargets = 7;
    let trailsSpawned = 0;
    // Trigger particle from sponge
    overworld.spawnParticle("minecraft:crop_growth_emitter", {
        x: pos.x + 0.5,
        y: pos.y + 1,
        z: pos.z + 0.5
    });
    // Random sample scan until 7 successes or 343 attempts (entire cube)
    for (let i = 0; i < 343 && trailsSpawned < maxTargets; i++) {
        const x = pos.x + Math.floor(Math.random() * 15) - 7;
        const y = pos.y + Math.floor(Math.random() * 15) - 7;
        const z = pos.z + Math.floor(Math.random() * 15) - 7;
        const b = dim.getBlock({ x, y, z });
        if (!b || !cropTypes.has(b.typeId))
            continue;
        const info = blockInfoMap.get(b.typeId);
        if (!info)
            continue;
        //@ts-ignore
        const growth = b.permutation.getState(info.perm);
        if (typeof growth !== "number" || growth >= info.max)
            continue;
        if (Math.random() < 0.5) {
            const next = Math.min(info.max, growth + 1);
            b.setPermutation(BlockPermutation.resolve(b.typeId, { [info.perm]: next }));
            if (Math.random() < 0.3) {
                spawnTrailParticle(pos, b.location, "wildphire_spg:trail_particle");
                trailsSpawned++;
            }
        }
    }
}
