/* ░░░▒▒▓▓██▣ RES INTERFACE ▣██▓▓▒▒░░░ ══[ 2026-04-10 ] ══ [mayview_selling_bin.js] ═════════
    ┳┳┓┏┓┓┏┓┏┳┏┓┓ ┏ ╭╭────────●▬▬▬▬๑۩۩๑▬▬▬▬▬●────────●▬▬▬▬๑۩۩๑▬▬▬▬▬●────────●▬▬▬▬๑۩۩๑▬▬▬▬▬●────────╮╮
    ┃┃┃┣┫┗┫┃┃┃┣ ┃┃┃ || server_scripts | sellingbin | Main Selling Bin Recipe/Features 
    ┛ ┗┛┗┗┛┗┛┻┗┛┗┻┛ ╰╰────────●▬▬▬▬๑۩۩๑▬▬▬▬▬●────────●▬▬▬▬๑۩۩๑▬▬▬▬▬●────────●▬▬▬▬๑۩۩๑▬▬▬▬▬●────────╯╯
*/ console.info("[RES:CORE] Initializing mayview_selling_bin.js");
const sellItems = global.Mayview.sellables ?? []

const extractors = [
    ["mayview:copper_selling_bin", 30 * 20, false],
    ["mayview:mythril_selling_bin", 20 * 20, false],
    ["mayview:viridium_selling_bin", 10 * 20, false]
]

ServerEvents.recipes(e => {
    extractors.forEach(extractor => {
        sellItems.forEach(item => {
            addExtraction(e, item, extractor[0], extractor[1], extractor[2])
        })

        const leftoversRecipe = e.recipes.custommachinery.custom_machine(extractor[0], 1.61 * 20)
            .requireItem("minecraft:apple")
            .produceItem("cobblemon:leftovers").chance(0.05)
            .resetOnError()

        if (extractor[2])
            leftoversRecipe.hide()
    })

    e.shaped("mayview:copper_selling_bin",["PMP", "PPP" ], { P: "#minecraft:planks", M: "minecraft:copper_ingot" } )
    e.shaped("mayview:mythril_selling_bin",["PMP", "PPP" ], { P: "#minecraft:planks", M: "ores:mythril_ingot" } )
    e.shaped("mayview:viridium_selling_bin",["PMP", "PPP" ], { P: "#minecraft:planks", M: "ores:viridium_ingot" } )
})

const stripCount = stack => stack.replace(/^\d+x\s*/, "")

ServerEvents.tags("item", e => {
    sellItems.forEach(item => e.add("mayview:sellables", stripCount(item[0])))
})

function addExtraction(e, item, machine, time, hide) {
    let recipe = e.recipes.custommachinery.custom_machine(machine, time)
        .requireItemOnEnd(item[0])

    for (let i = 1; i < Math.min(item.length, 7); i++)
        recipe.produceItem(item[i])

    recipe.resetOnError()
    if (hide)
        recipe.hide()
}

console.info(`[Mayview] server sellables count: ${sellItems.length}`)