import { Vector, system } from "@minecraft/server";

const cooldown = new Map();
const SWEEP_REGEXP = /[A-z]+_sweep:[0-9]+/;

export default {
    async execute(item, player) {
        const tag = item?.getTags()?.find(tag => SWEEP_REGEXP.test(tag));
        const { x, y, z } = player.location;

        if (!tag) return;
        // Sweep System
        if (system.currentTick < cooldown.get(player.id)) return;
        if (!cooldown.has(player.id)) cooldown.set(player.id, system.currentTick + Number(tag.split(":")[1]) * 20);
        else cooldown.set(player.id, system.currentTick + Number(tag.split(":")[1]) * 20);

        player.playSound("sound.attack.sweep", { location: player.location });
        player.dimension.spawnEntity(`true:${tag.split(":")[0]}`, new Vector(x + 1, y, z));
        await player.runCommandAsync(`particle true:${tag.split(":")[0]} ^ ^1.2 ^2.3`);
    }
}