import { world, ItemStack, system, } from "@minecraft/server";
import { eggTypeMapping, mythicMaterialDropMap, nestMountDropMap, spawnEggStatTypes, stickDropMap, } from "../DATA/Config";
import { itemLoreConfig } from "../DATA/LoreConfig";
import { processingNests } from "../ESS/ScriptEvent";
export function markNestHadEgg(entity) {
    entity.setDynamicProperty("kicu_mmad:hadEggBeforeBreak", true);
}
export function nestClick(log, entity) {
    const entityId = entity.typeId;
    if (!entityId.startsWith("kicu_mmad:") || !entityId.endsWith("_nest"))
        return;
    const objective = world.scoreboard.getObjective("kicu_mmad_NestHasEgg");
    const identity = entity.scoreboardIdentity;
    if (identity && objective.getParticipants().includes(identity)) {
        const hasEgg = objective.getScore(identity);
        if (hasEgg > 0) {
            spawnEggItemAtFromIndex(hasEgg, entity);
            markNestHadEgg(entity);
            entity.triggerEvent("kicu_mmad:set_egg_false");
            objective.setScore(entity, 0);
        }
        else {
            entity.triggerEvent("kicu_mmad:set_death");
        }
    }
}
export function spawnEggItemAtFromIndex(index, entity) {
    const eggId = eggTypeMapping[index];
    const eggStatInfo = spawnEggStatTypes[index];
    const itemStack = new ItemStack(eggId, 1);
    itemStack.setLore(itemLoreConfig[eggId]);
    const loc = entity.location;
    const spawnLocation = { x: loc.x, y: loc.y + 1.25, z: loc.z };
    entity.dimension.spawnItem(itemStack, spawnLocation);
    const scoreboardCommand = `scoreboard players add "${eggStatInfo.eggStat}" "kicu_mmad:jig_computer.addon_stats" 1`;
    system.run(() => entity.dimension.runCommand(scoreboardCommand));
    const soundCommand = `playsound sound.kicu_mmad_pick_up_egg @a[x=${loc.x},y=${loc.y},z=${loc.z},r=16] ~ ~ ~ 1.0 1.0 1.0`;
    system.run(() => entity.dimension.runCommand(soundCommand));
}
export function nestHit(log, damagingEntity, entity) {
    if (!entity.isValid() || processingNests.has(entity.id))
        return;
    processingNests.add(entity.id);
    const objective = world.scoreboard.getObjective("kicu_mmad_NestHasEgg");
    const identity = entity.scoreboardIdentity;
    let hasEgg = 0;
    if (identity && objective.getParticipants().includes(identity)) {
        hasEgg = objective.getScore(identity);
    }
    if (hasEgg > 0) {
        spawnEggItemAtFromIndex(hasEgg, entity);
        entity.triggerEvent("kicu_mmad:set_egg_false");
        objective.setScore(entity, 0);
        processingNests.delete(entity.id);
        return;
    }
    const { x, y, z } = entity.location;
    const dim = world.getDimension(entity.dimension.id);
    const matCfg = nestMountDropMap[entity.typeId];
    const stickCfg = stickDropMap[entity.typeId];
    const mythicCfg = mythicMaterialDropMap[entity.typeId];
    const hadEggBeforeBreak = entity.getDynamicProperty("kicu_mmad:hadEggBeforeBreak") === true;
    const mythicChance = hadEggBeforeBreak ? 100.0 : mythicCfg.chance;
    const spawnItems = [];
    if (Math.random() * 100 <= matCfg.chance) {
        const cnt = Math.floor(Math.random() * (matCfg.max - matCfg.min + 1)) + matCfg.min;
        if (cnt > 0) {
            const it = new ItemStack(matCfg.id, cnt);
            it.setLore(itemLoreConfig[matCfg.id]);
            spawnItems.push(it);
        }
    }
    if (stickCfg && Math.random() * 100 <= stickCfg.chance) {
        const cnt = Math.floor(Math.random() * (stickCfg.max - stickCfg.min + 1)) + stickCfg.min;
        if (cnt > 0) {
            spawnItems.push(new ItemStack(stickCfg.id, cnt));
        }
    }
    if (mythicChance === 100.0 || Math.random() * 100 <= mythicChance) {
        const cnt = Math.floor(Math.random() * (mythicCfg.max - mythicCfg.min + 1)) + mythicCfg.min;
        if (cnt > 0) {
            const it = new ItemStack(mythicCfg.id, cnt);
            it.setLore(itemLoreConfig[mythicCfg.id]);
            spawnItems.push(it);
        }
    }
    system.runTimeout(() => {
        if (!entity.isValid())
            return;
        const f2 = Math.floor(Math.random() * 4) + 1;
        try {
            entity.runCommand(`playanimation @s animation.kicu_mmad_nest.break${f2}`);
        }
        catch { }
        entity.triggerEvent("kicu_mmad:set_death");
    }, 5);
    system.runTimeout(() => {
        if (!entity.isValid())
            return;
        for (let i = 0; i < 3; i++) {
            dim.spawnParticle("kicu_mmad:nest_snatch", { x, y, z });
        }
        for (const it of spawnItems) {
            dim.spawnItem(it, { x, y, z });
        }
        dim.playSound("sound.kicu_mmad_destroy_nest_break", { x, y, z }, { pitch: Math.random() * 3 + 1, volume: 0.5 });
        entity.triggerEvent("kicu_mmad:despawns");
    }, 12);
}

//# sourceMappingURL=../../../../../_KiCuMountsBPDebug/kicu/mmad/C/Nest.js.map
