import { Entity, ItemStack } from "@minecraft/server";
import { randomChance } from "../../utils/systemHelper";
import { EntityHelper } from "../../entities/utils/entityHelper";
import { CustomItemTags } from "../utils/itemTags";

export const GLACIAL_NECKLACE = {
    _itemIdSuffix: "glacial_necklace",
    _glacialNecklaceAttackEffect: EntityHelper.createEntityEffect("slowness", 6, 0, true),
    _effectChance: 0.25,
    

    /**
     * Checks if an item is custom, glacial and either a necklace or a chestplate
     * @param {ItemStack} itemStack 
     * @returns True if item id pattern matches of a glacial necklace
     */
    isGlacialNecklace: function(itemStack) {
        if (!itemStack) return
        
        const isCustom = CustomItemTags.isCustom(itemStack)
        const isGlacialNecklace = itemStack.typeId.endsWith(this._itemIdSuffix)

        return isCustom && isGlacialNecklace
    },

    /**
     * 
     * @param {Entity} entity 
     */
    applyAttackEffect: function(entity) {
        if (!randomChance(this._effectChance)) return;

        const {effectType, duration, amplifier, showParticles} = this._glacialNecklaceAttackEffect
        entity.addEffect(effectType, duration, {amplifier: amplifier, showParticles: showParticles})
    }
}