import * as mc from "@minecraft/server"
import * as racoAPI from "../raco-API"

export function leakyHopperTick(block){
    const container = block.above()?.getComponent("inventory")?.container
    if (container){
        for (let i = 0; i < container?.size; i++){
            const thisItem = container.getItem(i)
            if (thisItem){
                const newItem = thisItem
                if (thisItem?.amount > 1){
                    thisItem.amount--
                    container.setItem(i, thisItem)
                } else if (thisItem?.amount == 1){
                    container.setItem(i, )
                }
                newItem.amount = 1
                const dropItem = block.dimension.spawnItem(newItem, {x:block.center().x, y:block.center().y-0.2, z:block.center().z})
                dropItem.teleport({x:block.center().x, y:block.center().y-0.2, z:block.center().z})
                break
            }
        }
    } else {
        const entities = block.dimension.getEntities({location:block.above().center(), maxDistance:0.65})
        for (const entity of entities){
            if (entity?.typeId.startsWith('minecraft:')){
                if (entity?.typeId == 'minecraft:item'){
                    entity.teleport({x:block.center().x, y:block.center().y-0.2, z:block.center().z})
                } else {
                    entity.teleport({x:block.center().x, y:block.y-1, z:block.center().z})
                }
            }
        }
    }
}