import { world } from "@minecraft/server";

const ITEM_TO_FUNCTION = {
    "pr:old_rod": "fishing/spawn_random_water_pokemon",
    "pr:good_rod": "fishing/use_good_rod",
"pr:good_rod": "fishing/use_good_rod",
    "pr:super_rod": "fishing/use_super_rod"
};

// cooldown in milliseconds
const COOLDOWNS = {
    "pr:old_rod": 2500,
    "pr:good_rod": 2500,
    "pr:super_rod": 2500
};

const lastUse = new Map();

world.afterEvents.itemUse.subscribe((data) => {
    try {
        const player = data.source;
        const item = data.itemStack;
        if (!item) return;

        const fn = ITEM_TO_FUNCTION[item.typeId];
        if (!fn) return;

        const key = `${player.id}:${item.typeId}`;
        const now = Date.now();

        const cd = COOLDOWNS[item.typeId] ?? 0;
        const previous = lastUse.get(key) ?? 0;

        // ⛔ cooldown check
        if (now - previous < cd) return;

        // update cooldown
        lastUse.set(key, now);

        player.runCommand(`execute at @s run function ${fn}`);

    } catch {
        // silent fail
    }
});


