import { BlockPermutation, system, world } from "@minecraft/server";
import { SettingsHandler } from "./settings";
export function xpSpongePulse(block) {
    // Check if XP sponge is enabled
    if (!SettingsHandler.isSpongeEnabled("xpSponge")) {
        const dim = world.getDimension(block.dimension.id);
        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("§cXP Sponge is disabled in settings!");
        }
        return;
    }
    const states = block.permutation.getAllStates();
    let value0 = states["wildphire_spg:xp_0"];
    let value1 = states["wildphire_spg:xp_1"];
    let value2 = states["wildphire_spg:xp_2"];
    let value3 = states["wildphire_spg:xp_3"];
    if (value0 == undefined || value1 == undefined || value2 == undefined || value3 == undefined)
        return;
    value0 = value0 * 1000;
    value1 = value1 * 100;
    value2 = value2 * 10;
    let totalValue = value0 + value1 + value2 + value3;
    const dim = world.getDimension(block.dimension.id);
    const surroundingXpOrbs = dim.getEntities({
        maxDistance: 15,
        type: "minecraft:xp_orb",
        location: block.location
    });
    if (surroundingXpOrbs.length <= 0)
        return;
    if (totalValue + surroundingXpOrbs.length > 9999)
        return;
    totalValue += surroundingXpOrbs.length;
    dim.spawnParticle("wildphire_spg:xp_sponge_particle", {
        x: block.location.x + 0.5, y: block.location.y, z: block.location.z + 0.5
    });
    surroundingXpOrbs.forEach((xpOrb) => {
        dim.spawnParticle("minecraft:explosion_manual", xpOrb.location);
        xpOrb.remove();
    });
    const newStates = splitXp(totalValue);
    block.setPermutation(BlockPermutation.resolve(block.typeId, {
        "wildphire_spg:xp_0": newStates[0],
        "wildphire_spg:xp_1": newStates[1],
        "wildphire_spg:xp_2": newStates[2],
        "wildphire_spg:xp_3": newStates[3]
    }));
    dim.playSound("note.chime", block.location);
}
export function breakXpSponge(block, player) {
    const states = block.permutation.getAllStates();
    let value0 = states["wildphire_spg:xp_0"];
    let value1 = states["wildphire_spg:xp_1"];
    let value2 = states["wildphire_spg:xp_2"];
    let value3 = states["wildphire_spg:xp_3"];
    if (value0 == undefined || value1 == undefined || value2 == undefined || value3 == undefined)
        return;
    value0 = value0 * 1000;
    value1 = value1 * 100;
    value2 = value2 * 10;
    let totalValue = value0 + value1 + value2 + value3;
    const xpLevels = getXpLevel(totalValue);
    if (xpLevels == 0)
        return;
    system.run(() => {
        player.addLevels(xpLevels);
    });
}
function splitXp(xp) {
    return xp.toString().padStart(4, '0').split('').map(d => parseInt(d));
}
function getXpLevel(xp) {
    if (xp < 0)
        return 0;
    let level = 0;
    if (xp < 352) {
        level = Math.floor((-6 + Math.sqrt(36 + 4 * xp)) / 2);
    }
    else if (xp < 1507) {
        level = Math.floor(40.5 + Math.sqrt(40.5 * 40.5 - 4 * 2.5 * (360 - xp))) / (2 * 2.5);
    }
    else {
        level = Math.floor((162.5 + Math.sqrt(162.5 * 162.5 - 4 * 4.5 * (2220 - xp))) / (2 * 4.5));
    }
    return level;
}
export function getXPSponge(block) {
    const states = block.permutation.getAllStates();
    let value0 = states["wildphire_spg:xp_0"];
    let value1 = states["wildphire_spg:xp_1"];
    let value2 = states["wildphire_spg:xp_2"];
    let value3 = states["wildphire_spg:xp_3"];
    if (value0 == undefined || value1 == undefined || value2 == undefined || value3 == undefined)
        return 0;
    value0 = value0 * 1000;
    value1 = value1 * 100;
    value2 = value2 * 10;
    let totalValue = value0 + value1 + value2 + value3;
    return totalValue;
}
