import { ItemStack } from "@minecraft/server"

/**
 * List of Minecraft Items and their custom tags
 * ```
 * keyword - Literal Item ids
 * #keyword - Items with tag `keyword`
 * .keyword - Item ids ending with `keyword`
 * /keyword - Item ids starting with `keyword` 
 * ~keyword - Item ids containing `keyword`
 * ```
 */
export class CustomItemTags {
    static #is_custom = [
        "/piki_beyondsurvival:"
    ]
    static isCustom(itemStack) {
        const list = CustomItemTags.#is_custom
        return CustomItemTags.matches(list, itemStack)
    } 

    /**
     * Check if item is listed in the array using its information
     * @param {string[]} array 
     * @param {ItemStack} itemStack 
     */
    static matches(array, itemStack) {
        if (!itemStack) return false
        for (const element of array) {
            if (element == itemStack.typeId) return true
            if (element.includes("#") && itemStack.hasTag(element.replace("#", ""))) return true
            if (element.includes(".") && itemStack.typeId.endsWith(element.replace(".", ""))) return true
            if (element.includes("/") && itemStack.typeId.startsWith(element.replace("/", ""))) return true
            if (element.includes("~") && itemStack.typeId.includes(element.replace("~", ""))) return true
        }
        return false
    }
}