import { SettingsHandler } from "./settings";
const DECAY_RADIUS = 15;
const DECAY_LIFETIME_TICKS = 600;
const MAX_BLOCKS_PER_TICK = 5;
const DECAY_BLOCKS = new Set([
    "minecraft:vine",
    "minecraft:twisting_vines",
    "minecraft:weeping_vines",
    "minecraft:weeping_vines_plant",
    "minecraft:twisting_vines_plant",
    "minecraft:glow_lichen",
    "minecraft:cave_vines",
    "minecraft:cave_vines_body_with_berries",
    "minecraft:cave_vines_head_with_berries",
    "minecraft:azalea_leaves",
    "minecraft:azalea_leaves_flowered",
    "minecraft:oak_leaves",
    "minecraft:spruce_leaves",
    "minecraft:birch_leaves",
    "minecraft:jungle_leaves",
    "minecraft:acacia_leaves",
    "minecraft:dark_oak_leaves",
    "minecraft:mangrove_leaves",
    "minecraft:cherry_leaves",
    "minecraft:leaf_litter",
    "minecraft:pink_petals",
    "minecraft:wildflowers",
]);
const spongeMap = new Map();
function getKey(pos) {
    return `${pos.x}_${pos.y}_${pos.z}`;
}
// Fisher-Yates shuffle
function shuffleArray(array) {
    for (let i = array.length - 1; i > 0; i--) {
        const j = Math.floor(Math.random() * (i + 1));
        [array[i], array[j]] = [array[j], array[i]];
    }
    return array;
}
function generateShuffledPositions(center) {
    const positions = [];
    for (let dx = -DECAY_RADIUS; dx <= DECAY_RADIUS; dx++) {
        for (let dy = -DECAY_RADIUS; dy <= DECAY_RADIUS; dy++) {
            for (let dz = -DECAY_RADIUS; dz <= DECAY_RADIUS; dz++) {
                positions.push({
                    x: center.x + dx,
                    y: center.y + dy,
                    z: center.z + dz
                });
            }
        }
    }
    return shuffleArray(positions);
}
export function decaySpongePulse(block) {
    // Check if decay sponge is enabled
    if (!SettingsHandler.isSpongeEnabled("decaySponge")) {
        const dim = block.dimension;
        dim.runCommand(`setblock ${block.location.x} ${block.location.y} ${block.location.z} air destroy`);
        // Notify nearby players
        const nearbyPlayers = dim.getPlayers({ location: block.location, maxDistance: 10 });
        for (const player of nearbyPlayers) {
            player.sendMessage("§cDecay Sponge is disabled in settings!");
        }
        return;
    }
    const key = getKey(block.location);
    let state = spongeMap.get(key);
    if (!state) {
        state = {
            tick: 0,
            positions: generateShuffledPositions(block.location),
            index: 0
        };
        spongeMap.set(key, state);
    }
    const dim = block.dimension;
    let removed = 0;
    for (; state.index < state.positions.length && removed < MAX_BLOCKS_PER_TICK; state.index++) {
        const pos = state.positions[state.index];
        // Skip positions below y -63 (world boundary)
        if (pos.y < -63) {
            continue;
        }
        const b = dim.getBlock(pos);
        if (b && DECAY_BLOCKS.has(b.typeId)) {
            dim.runCommand(`setblock ${pos.x} ${pos.y} ${pos.z} air destroy`);
            removed++;
        }
    }
    state.tick += 20;
    if (state.tick >= DECAY_LIFETIME_TICKS || state.index >= state.positions.length) {
        dim.runCommand(`setblock ${block.location.x} ${block.location.y} ${block.location.z} air destroy`);
        spongeMap.delete(key);
    }
    else {
        spongeMap.set(key, state); // Save updated state
    }
}
