import { Block, EntityInventoryComponent, Player, system, world } from "@minecraft/server";

/**
 * 
 * @param {Block} block 
 */
export function getCropType(block) {
    for (const [mined, [crop, seeds]] of cropTypes.entries()) {
        if (block.permutation.matches(mined)) {
            return [crop, seeds]
        }
    }
}

/**
 * 
 * @param {Player} player 
 * @param {string} item 
 */
export function hasSeeds(player, item) {
    /**
     * @type {EntityInventoryComponent}
     */
    const inv = player.getComponent("inventory")

    for (let i = 0; i < inv.container.size; i++) {
        const slot = inv.container.getItem(i)
        if (slot?.typeId == `minecraft:${item}`) {
            return true
        }
    }
    return false
}

const cropTypes = new Map([
    ["wheat", ["wheat", "wheat_seeds"]],
    ["beetroot", ["beetroot", "beetroot_seeds"]],
    ["carrots", ["carrots", "carrot"]],
    ["potatoes", ["potatoes", "potato"]],
    ["melon_stem", ["melon_stem", "melon_seeds"]],
    ["pumpkin_stem", ["pumpkin_stem", "pumpkin_seeds"]],
    ["nether_wart", ["nether_wart", "nether_wart"]]
])