import * as mc from "@minecraft/server";
import { SafetyCheckedPlayer, mclog } from "./utils/index";
const REPLACEITEMS = {
    "ldz:magnet": "ldz_240409:mob_magnet",
    "ldz:allay_genes": "ldz_240409:allay_genes",
    "ldz:axolotl_genes": "ldz_240409:axolotl_genes",
    "ldz:bat_genes": "ldz_240409:bat_genes",
    "ldz:bee_genes": "ldz_240409:bee_genes",
    "ldz:blaze_genes": "ldz_240409:blaze_genes",
    "ldz:camel_genes": "ldz_240409:camel_genes",
    "ldz:cat_genes": "ldz_240409:cat_genes",
    "ldz:cave_spider_genes": "ldz_240409:cave_spider_genes",
    "ldz:chicken_genes": "ldz_240409:chicken_genes",
    "ldz:cod_genes": "ldz_240409:cod_genes",
    "ldz:cow_genes": "ldz_240409:cow_genes",
    "ldz:creeper_genes": "ldz_240409:creeper_genes",
    "ldz:dolphin_genes": "ldz_240409:dolphin_genes",
    "ldz:donkey_genes": "ldz_240409:donkey_genes",
    "ldz:drowned_genes": "ldz_240409:drowned_genes",
    "ldz:elder_guardian_genes": "ldz_240409:elder_guardian_genes",
    "ldz:ender_dragon_genes": "ldz_240409:ender_dragon_genes",
    "ldz:enderman_genes": "ldz_240409:enderman_genes",
    "ldz:endermite_genes": "ldz_240409:endermite_genes",
    "ldz:evoker_genes": "ldz_240409:evoker_genes",
    "ldz:fox_genes": "ldz_240409:fox_genes",
    "ldz:frog_genes": "ldz_240409:frog_genes",
    "ldz:ghast_genes": "ldz_240409:ghast_genes",
    "ldz:glow_squid_genes": "ldz_240409:glow_squid_genes",
    "ldz:goat_genes": "ldz_240409:goat_genes",
    "ldz:guardian_genes": "ldz_240409:guardian_genes",
    "ldz:hoglin_genes": "ldz_240409:hoglin_genes",
    "ldz:horse_genes": "ldz_240409:horse_genes",
    "ldz:husk_genes": "ldz_240409:husk_genes",
    "ldz:iron_golem_genes": "ldz_240409:iron_golem_genes",
    "ldz:llama_genes": "ldz_240409:llama_genes",
    "ldz:magma_cube_genes": "ldz_240409:magma_cube_genes",
    "ldz:mooshroom_genes": "ldz_240409:mooshroom_genes",
    "ldz:mule_genes": "ldz_240409:mule_genes",
    "ldz:ocelot_genes": "ldz_240409:ocelot_genes",
    "ldz:panda_genes": "ldz_240409:panda_genes",
    "ldz:parrot_genes": "ldz_240409:parrot_genes",
    "ldz:phantom_genes": "ldz_240409:phantom_genes",
    "ldz:pig_genes": "ldz_240409:pig_genes",
    "ldz:piglin_brute_genes": "ldz_240409:piglin_brute_genes",
    "ldz:piglin_genes": "ldz_240409:piglin_genes",
    "ldz:pillager_genes": "ldz_240409:pillager_genes",
    "ldz:polar_bear_genes": "ldz_240409:polar_bear_genes",
    "ldz:pufferfish_genes": "ldz_240409:pufferfish_genes",
    "ldz:rabbit_genes": "ldz_240409:rabbit_genes",
    "ldz:ravager_genes": "ldz_240409:ravager_genes",
    "ldz:salmon_genes": "ldz_240409:salmon_genes",
    "ldz:sheep_genes": "ldz_240409:sheep_genes",
    "ldz:shulker_genes": "ldz_240409:shulker_genes",
    "ldz:silverfish_genes": "ldz_240409:silverfish_genes",
    "ldz:skeleton_genes": "ldz_240409:skeleton_genes",
    "ldz:skeleton_horse_genes": "ldz_240409:skeleton_horse_genes",
    "ldz:slime_genes": "ldz_240409:slime_genes",
    "ldz:sniffer_genes": "ldz_240409:sniffer_genes",
    "ldz:snow_golem_genes": "ldz_240409:snow_golem_genes",
    "ldz:spider_genes": "ldz_240409:spider_genes",
    "ldz:squid_genes": "ldz_240409:squid_genes",
    "ldz:stray_genes": "ldz_240409:stray_genes",
    "ldz:strider_genes": "ldz_240409:strider_genes",
    "ldz:tadpole_genes": "ldz_240409:tadpole_genes",
    "ldz:tropical_fish_genes": "ldz_240409:tropical_fish_genes",
    "ldz:turtle_genes": "ldz_240409:turtle_genes",
    "ldz:vex_genes": "ldz_240409:vex_genes",
    "ldz:villager_genes": "ldz_240409:villager_genes",
    "ldz:vindicator_genes": "ldz_240409:vindicator_genes",
    "ldz:wandering_trader_genes": "ldz_240409:wandering_trader_genes",
    "ldz:warden_genes": "ldz_240409:warden_genes",
    "ldz:witch_genes": "ldz_240409:witch_genes",
    "ldz:wither_genes": "ldz_240409:wither_genes",
    "ldz:wither_skeleton_genes": "ldz_240409:wither_skeleton_genes",
    "ldz:wolf_genes": "ldz_240409:wolf_genes",
    "ldz:zoglin_genes": "ldz_240409:zoglin_genes",
    "ldz:zombie_genes": "ldz_240409:zombie_genes",
    "ldz:zombie_piglin_genes": "ldz_240409:zombie_piglin_genes",
    "ldz:zombie_villager_genes": "ldz_240409:zombie_villager_genes",
};
export function initItemUpdater() {
    mc.system.runInterval(updateAllPlayersItems, 1 * mc.TicksPerSecond);
}
function updateAllPlayersItems() {
    const playersItems = getPlayersItems();
    updatePlayersItems(playersItems);
    function getPlayersItems() {
        const playersItems = [];
        mc.world.getAllPlayers().map(player => new SafetyCheckedPlayer(player)).forEach(player => {
            const inventory = player.Inventory;
            const items = new Map();
            for (let slot = 0; slot < inventory.size; slot++) {
                const item = inventory.getItem(slot);
                if (item) {
                    items.set(item.typeId, (items.get(item.typeId) || 0) + item.amount);
                }
            }
            playersItems.push({ player, inventory, items });
        });
        return playersItems;
    }
    function updatePlayersItems(playersItems) {
        for (const playerItems of playersItems) {
            const player = playerItems.player;
            for (const [fromItem, toItem] of Object.entries(REPLACEITEMS)) {
                const fromItemAmount = playerItems.items.get(fromItem);
                if (fromItemAmount) {
                    mclog(`Replaced ${fromItemAmount}x ${fromItem}`);
                    player.runCommand(`clear @s ${fromItem}`);
                    player.runCommand(`give @s ${toItem} ${fromItemAmount}`);
                }
            }
        }
    }
}
