import { EntityEquippableComponent, EquipmentSlot, ItemDurabilityComponent } from "@minecraft/server";

export default {
    isPotion(entity) {
      return entity.typeId == "minecraft:splash_potion";
    },
    damageWeapon(item, damage) {
      const durability = item?.getComponent(ItemDurabilityComponent.componentId);
  
      if (!durability) return item;
      durability.damage += damage;
      return item;
    },
    getEquipmentComponent(entity) {
      return entity.getComponent(EntityEquippableComponent.componentId);
    },
    getWeapon(player, offhand = false) {
      return this.getEquipmentComponent(player)
        .getEquipment(offhand ? EquipmentSlot.Offhand : EquipmentSlot.Mainhand);
    },
    calculateReduction(damage, damager, isProyectile = false) {
      if (!this.isPotion(damager) && isProyectile) return damage;
      return damage - (damage * 0.4);
    },
    findWeapon(player) {
      return [ false, true ]
        .map(offhand => this.getWeapon(player, offhand))
        .filter(item => item?.typeId.includes("twohanded"))[0];
    }
}