//console.warn('apocalysisDifficulty')
import { ItemDurabilityComponent, ItemStack, MinecraftDimensionTypes, system, world } from "@minecraft/server";
import { getRandomInt, isHumanoidMonster } from "nps_mot/utils/my_API";
import config from "nps_mot/config";
import { parameterNames, Settings } from "nps_mot/utils/scoreboard";
system.runInterval(() => {
    applyResistanceEffectToEntities();
}, 20 * 2);
world.afterEvents.entityDie.subscribe((event => {
    apocalypseDrop(event);
}));
function applyResistanceEffectToEntities() {
    const apocalysisDifficulty = Settings.getValue(parameterNames.apocalysisDifficulty);
    if (apocalysisDifficulty > 0) {
        const resistanceEffect = "resistance";
        const duration = 30 * 20;
        const amplifier = apocalysisDifficulty - 1;
        const dimensions = [
            MinecraftDimensionTypes.overworld,
            MinecraftDimensionTypes.nether,
            MinecraftDimensionTypes.theEnd
        ];
        dimensions.forEach(dimensionType => {
            const dimension = world.getDimension(dimensionType);
            dimension.getEntities({
                excludeFamilies: ["player", "armor_stand"]
            }).forEach(entity => {
                entity.addEffect(resistanceEffect, duration, {
                    showParticles: false,
                    amplifier: amplifier
                });
            });
        });
    }
}
function apocalypseDrop(event) {
    let damagingEntity = event.damageSource.damagingEntity;
    if (damagingEntity === undefined) {
        return;
    }
    if (damagingEntity.typeId !== "minecraft:player") {
        return;
    }
    let deadEntity = event.deadEntity;
    if (!isHumanoidMonster(deadEntity)) {
        return;
    }
    ;
    if (Settings.getValue(parameterNames.repairKitDrops) == 1) {
        dropRepairKit(deadEntity.dimension, deadEntity.location);
    }
    if (Settings.getValue(parameterNames.jewelDrops) == 1) {
        dropMerchantJewel(deadEntity.dimension, deadEntity.location);
    }
    if (Settings.getValue(parameterNames.monsterDrops) == 1) {
        dropMonsterEquipment(deadEntity.dimension, deadEntity.location);
    }
}
function dropRepairKit(dimension, location) {
    let repair_kit = new ItemStack(`nps_mot:repair_kit`);
    repair_kit.setLore(["", "§r§7Use it in an anvil to repair an object§r", config.addonName, config.credits]);
    const REPAIR_KIT_DROP_CHANCE = 1 / 50;
    if (Math.random() > REPAIR_KIT_DROP_CHANCE) {
        return;
    }
    system.run(() => {
        dimension.spawnItem(repair_kit, location);
    });
}
function dropMerchantJewel(dimension, location) {
    const MERCHANT_JEWEL_DROP_CHANCE = 1 / 50;
    if (Math.random() > MERCHANT_JEWEL_DROP_CHANCE) {
        return;
    }
    const amount = getRandomInt(1, 4);
    const apocalysisDifficulty = Settings.getValue(parameterNames.apocalysisDifficulty);
    const type = getRandomInt(0, apocalysisDifficulty + 1);
    const jewel = new ItemStack(`nps_mot:merchant_jewel_${type}`, amount);
    system.run(() => {
        dimension.spawnItem(jewel, location);
    });
}
function getPickaxeLevel(apocalypseDifficulty) {
    // Define the mean and standard deviation for each difficulty level
    const difficultyParams = {
        0: { mean: 1.17, stddev: 0.85 },
        1: { mean: 2.0, stddev: 1.0 },
        2: { mean: 3.0, stddev: 1.5 },
        3: { mean: 4.0, stddev: 1.8 },
        4: { mean: 5.0, stddev: 2.0 },
    };
    // Get the parameters for the current difficulty
    const params = difficultyParams[apocalypseDifficulty];
    if (!params) {
        throw new Error(`Invalid apocalypse difficulty: ${apocalypseDifficulty}`);
    }
    // Generate a random number following a normal distribution
    function randn_bm() {
        let u = 0, v = 0;
        while (u === 0)
            u = Math.random(); // Converting [0,1) to (0,1)
        while (v === 0)
            v = Math.random();
        return Math.sqrt(-2.0 * Math.log(u)) * Math.cos(2.0 * Math.PI * v);
    }
    // Calculate the pickaxe level
    let pickaxeLevel = Math.round(params.mean + params.stddev * randn_bm());
    // Ensure the pickaxe level is within bounds (0 to 7)
    pickaxeLevel = Math.max(0, Math.min(7, pickaxeLevel));
    return pickaxeLevel;
}
function dropPickaxe(dimension, location) {
    const pickaxeIDs = {
        0: [
            "minecraft:wooden_pickaxe",
            "nps_mot:aluminium_pickaxe"
        ],
        1: [
            "minecraft:stone_pickaxe",
            "minecraft:golden_pickaxe"
        ],
        2: [
            "minecraft:iron_pickaxe",
            "nps_mot:amethyst_pickaxe",
            "nps_mot:emerald_pickaxe"
        ],
        3: [
            "minecraft:diamond_pickaxe",
            "minecraft:netherite_pickaxe"
        ],
        4: [
            "nps_mot:obsidian_pickaxe",
            "nps_mot:platinum_pickaxe",
            "nps_mot:palladium_pickaxe",
            "nps_mot:ruthenium_pickaxe",
            "nps_mot:iridium_pickaxe"
        ],
        5: [
            "nps_mot:titanium_pickaxe",
            "nps_mot:black_opal_pickaxe",
            "nps_mot:sapphire_pickaxe",
            "nps_mot:ruby_pickaxe"
        ],
        6: [
            "nps_mot:tungsten_pickaxe",
            "nps_mot:orichalc_pickaxe",
            "nps_mot:black_diamond_pickaxe",
            "nps_mot:pink_diamond_pickaxe",
            "nps_mot:purple_diamond_pickaxe",
            "nps_mot:green_diamond_pickaxe",
            "nps_mot:orange_diamond_pickaxe",
            "nps_mot:yellow_diamond_pickaxe"
        ],
        7: [
            "nps_mot:osmium_pickaxe",
            "nps_mot:adamantite_pickaxe",
            "nps_mot:red_diamond_pickaxe"
        ]
    };
    const apocalypseDifficulty = Settings.getValue(parameterNames.apocalysisDifficulty);
    const pickaxeLevel = getPickaxeLevel(apocalypseDifficulty);
    const pickaxeArray = pickaxeIDs[pickaxeLevel];
    if (!pickaxeArray || pickaxeArray.length === 0) {
        throw new Error(`No pickaxes found for level: ${pickaxeLevel}`);
    }
    const pickaxeID = pickaxeArray[Math.floor(Math.random() * pickaxeArray.length)];
    system.run(() => {
        if (pickaxeLevel == 0 || pickaxeLevel == 1) {
            dimension.runCommand(`/particle nps_mot:uncommon_puff ${location.x} ${location.y} ${location.z}`);
            dimension.runCommand(`/execute positioned ${location.x} ${location.y} ${location.z} run playsound nps_mot:sparkle_1 @a[r=16]`);
        }
        else if (pickaxeLevel == 2 || pickaxeLevel == 3) {
            dimension.runCommand(`/particle nps_mot:rare_puff ${location.x} ${location.y} ${location.z}`);
            dimension.runCommand(`/execute positioned ${location.x} ${location.y} ${location.z} run playsound nps_mot:sparkle_1 @a[r=16]`);
        }
        else if (pickaxeLevel == 4 || pickaxeLevel == 5) {
            dimension.runCommand(`/particle nps_mot:epic_puff ${location.x} ${location.y} ${location.z}`);
            dimension.runCommand(`/execute positioned ${location.x} ${location.y} ${location.z} run playsound nps_mot:sparkle_1 @a[r=16]`);
        }
        else if (pickaxeLevel == 6 || pickaxeLevel == 7) {
            dimension.runCommand(`/particle nps_mot:legendary_puff ${location.x} ${location.y} ${location.z}`);
            dimension.runCommand(`/execute positioned ${location.x} ${location.y} ${location.z} run playsound nps_mot:sparkle_2 @a[r=16]`);
        }
        const pickaxe = new ItemStack(pickaxeID);
        let idc = pickaxe.getComponent(ItemDurabilityComponent.componentId);
        idc.damage = idc.maxDurability - getRandomInt(1, idc.maxDurability * 1 / 2);
        dimension.spawnItem(pickaxe, location);
    });
}
function dropSticks(dimension, location) {
    const stickIDs = {
        0: [
            "minecraft:stick",
            "nps_mot:iron_stick"
        ],
        1: [
            "minecraft:stick",
            "nps_mot:iron_stick"
        ],
        2: [
            "minecraft:stick",
            "nps_mot:iron_stick"
        ],
        3: [
            "minecraft:stick",
            "nps_mot:iron_stick"
        ],
        4: [
            "minecraft:stick",
            "nps_mot:iron_stick",
            "nps_mot:bronze_stick",
            "nps_mot:stainless_steel_stick"
        ],
        5: [
            "minecraft:stick",
            "nps_mot:iron_stick",
            "nps_mot:bronze_stick",
            "nps_mot:stainless_steel_stick",
            "nps_mot:titanium_stick"
        ],
        6: [
            "minecraft:stick",
            "nps_mot:iron_stick",
            "nps_mot:bronze_stick",
            "nps_mot:stainless_steel_stick",
            "nps_mot:titanium_stick",
            "nps_mot:tungsten_aluminium_stick",
        ],
        7: [
            "minecraft:stick",
            "nps_mot:iron_stick",
            "nps_mot:bronze_stick",
            "nps_mot:stainless_steel_stick",
            "nps_mot:titanium_stick",
            "nps_mot:tungsten_aluminium_stick",
            "nps_mot:adamantite_stick"
        ]
    };
    const apocalypseDifficulty = Settings.getValue(parameterNames.apocalysisDifficulty);
    const stickLevel = getPickaxeLevel(apocalypseDifficulty);
    const stickArray = stickIDs[stickLevel];
    if (!stickArray || stickArray.length === 0) {
        throw new Error(`No sticks found for level: ${stickLevel}`);
    }
    const stickID = stickArray[Math.floor(Math.random() * stickArray.length)];
    system.run(() => {
        if (stickLevel == 0 || stickLevel == 1) {
            dimension.runCommand(`/particle nps_mot:uncommon_puff ${location.x} ${location.y} ${location.z}`);
            dimension.runCommand(`/execute positioned ${location.x} ${location.y} ${location.z} run playsound nps_mot:sparkle_1 @a[r=16]`);
        }
        else if (stickLevel == 2 || stickLevel == 3) {
            dimension.runCommand(`/particle nps_mot:rare_puff ${location.x} ${location.y} ${location.z}`);
            dimension.runCommand(`/execute positioned ${location.x} ${location.y} ${location.z} run playsound nps_mot:sparkle_1 @a[r=16]`);
        }
        else if (stickLevel == 4 || stickLevel == 5) {
            dimension.runCommand(`/particle nps_mot:epic_puff ${location.x} ${location.y} ${location.z}`);
            dimension.runCommand(`/execute positioned ${location.x} ${location.y} ${location.z} run playsound nps_mot:sparkle_1 @a[r=16]`);
        }
        else if (stickLevel == 6 || stickLevel == 7) {
            dimension.runCommand(`/particle nps_mot:legendary_puff ${location.x} ${location.y} ${location.z}`);
            dimension.runCommand(`/execute positioned ${location.x} ${location.y} ${location.z} run playsound nps_mot:sparkle_2 @a[r=16]`);
        }
        let stick = new ItemStack(stickID, getRandomInt(1, 2));
        dimension.spawnItem(stick, location);
    });
}
function dropGemOrIngot(dimension, location) {
    const itemIDs = {
        0: [
            "nps_mot:lead_ingot",
            "nps_mot:aluminium_ingot",
            "nps_mot:nickel_ingot",
            "nps_mot:chrome_ingot",
            "nps_mot:tin_ingot",
            "nps_mot:bronze_ingot",
            "minecraft:coal"
        ],
        1: [
            "nps_mot:stainless_steel_ingot",
            "minecraft:copper_ingot",
            "minecraft:lapis_lazuli",
            "minecraft:gold_ingot",
            "minecraft:iron_ingot"
        ],
        2: [
            "nps_mot:cobalt_ingot",
            "nps_mot:citrine",
            "nps_mot:amber",
            "nps_mot:topaz",
            "nps_mot:opal",
            "nps_mot:blue_garnet",
            "nps_mot:sulfur",
            "nps_mot:peridot",
            "minecraft:diamond",
            "minecraft:emerald",
            "minecraft:amethyst_shard"
        ],
        3: [
            "minecraft:obsidian",
            "minecraft:netherite_ingot"
        ],
        4: [
            "nps_mot:titanium_ingot",
            "nps_mot:rhodium_ingot",
            "nps_mot:iridium_ingot",
            "nps_mot:ruthenium_ingot",
            "nps_mot:palladium_ingot",
            "nps_mot:platinum_ingot"
        ],
        5: [
            "nps_mot:tungsten_ingot",
            "nps_mot:black_opal",
            "nps_mot:sapphire",
            "nps_mot:ruby"
        ],
        6: [
            "nps_mot:green_diamond",
            "nps_mot:yellow_diamond",
            "nps_mot:green_diamond",
            "nps_mot:pink_diamond",
            "nps_mot:purple_diamond",
            "nps_mot:orange_diamond",
        ],
        7: [
            "nps_mot:osmium_ingot",
            "nps_mot:red_diamond",
            "nps_mot:adamantite_ingot",
        ]
    };
    const apocalypseDifficulty = Settings.getValue(parameterNames.apocalysisDifficulty);
    const itemLevel = getPickaxeLevel(apocalypseDifficulty);
    const itemArray = itemIDs[itemLevel];
    if (!itemArray || itemArray.length === 0) {
        throw new Error(`No items found for level: ${itemLevel}`);
    }
    const itemID = itemArray[Math.floor(Math.random() * itemArray.length)];
    system.run(() => {
        if (itemLevel == 0 || itemLevel == 1) {
            dimension.runCommand(`/particle nps_mot:uncommon_puff ${location.x} ${location.y} ${location.z}`);
            dimension.runCommand(`/execute positioned ${location.x} ${location.y} ${location.z} run playsound nps_mot:sparkle_1 @a[r=16]`);
        }
        else if (itemLevel == 2 || itemLevel == 3) {
            dimension.runCommand(`/particle nps_mot:rare_puff ${location.x} ${location.y} ${location.z}`);
            dimension.runCommand(`/execute positioned ${location.x} ${location.y} ${location.z} run playsound nps_mot:sparkle_1 @a[r=16]`);
        }
        else if (itemLevel == 4 || itemLevel == 5) {
            dimension.runCommand(`/particle nps_mot:epic_puff ${location.x} ${location.y} ${location.z}`);
            dimension.runCommand(`/execute positioned ${location.x} ${location.y} ${location.z} run playsound nps_mot:sparkle_1 @a[r=16]`);
        }
        else if (itemLevel == 6 || itemLevel == 7) {
            dimension.runCommand(`/particle nps_mot:legendary_puff ${location.x} ${location.y} ${location.z}`);
            dimension.runCommand(`/execute positioned ${location.x} ${location.y} ${location.z} run playsound nps_mot:sparkle_2 @a[r=16]`);
        }
        let item = new ItemStack(itemID, getRandomInt(1, 2));
        dimension.spawnItem(item, location);
    });
}
function dropMonsterEquipment(dimension, location) {
    const MONSTER_DROP_CHANCE = 1 / 50;
    if (Math.random() > MONSTER_DROP_CHANCE) {
        return;
    }
    let choice = getRandomInt(0, 2);
    switch (choice) {
        case 0: {
            dropPickaxe(dimension, location);
            break;
        }
        case 1: {
            dropSticks(dimension, location);
            break;
        }
        case 2: {
            dropGemOrIngot(dimension, location);
            break;
        }
    }
}
