import { EntityEquippableComponent, EquipmentSlot } from "@minecraft/server";
import { hasTag } from "../../functions";

const WEIGHT_REGEXP = /weight:[0-9]+/;
const firstTimeHandle = new Map();

export default {
    execute(item, player) {
        const playerEquipment = player?.getComponent(EntityEquippableComponent.componentId);
        const offHand = playerEquipment?.getEquipment(EquipmentSlot.Offhand);
        const tag = item?.getTags()?.find(tag => WEIGHT_REGEXP.test(tag));
        const weight = Number(tag?.match(/([0-9]+)/gm)[0]);

        if (!tag) return firstTimeHandle.has(player.id) ? firstTimeHandle.delete(player.id) : null;
        if (!offHand && weight > 1) player.playAnimation("animation.player.sword_heavy");
        if (!offHand) return;
        if (!firstTimeHandle.has(player.id)) player.onScreenDisplay.setActionBar({ translate: "trueweapons.weight.line" });

        firstTimeHandle.set(player.id, true);
        player.addEffect("slowness", 1, { amplifier: weight, showParticles: false });
    }
}