// priority: 0
// Visit the wiki for more info - https://kubejs.com/
console.info('Hello, World! (Loaded POWERED ENCHANTER.js)')


ServerEvents.recipes(event => {

    const ID_PREFIX = 'kubejs:'
    const BASE_DURATION = 200

    // EU scaling: 2* Enchantment Level
    function eutForLevel(level) {
        return 30 * Math.pow(2, level - 1)
    }

    // Fluids planned (still disabled by default)
    const ENABLE_FLUIDS = false
    const FLUID_ID = 'mob_grinding_utils:fluid_xp'
    function fluidForLevel(level) {
        return 1000 * level
    }

    // ***** ENCHANTMENT LIST ***** //
    const ENCHANTS = {
        'minecraft:sharpness': { maxLevel: 9, uniqueItem: '#forge:gems/nether_quartz' },
        'minecraft:protection': { maxLevel: 7, uniqueItem: '#forge:plates/steel' },
    }

    function folderFromEnchantId(enchantId) {
        enchantId = String(enchantId || '')
        const parts = enchantId.split(':')
        if (parts.length >= 2 && parts[1]) return parts[1]
        return enchantId.replace(/[^a-z0-9_\-]/gi, '_')
    }

    function bookWithEnchant(enchantId, level, strong) {
        strong = !!strong
        let it = Item.of('minecraft:enchanted_book').enchant(enchantId, level)
        if (strong) it = it.strongNBT()
        return it
    }

    function makeBaseRecipe(folderKey, enchantId, level, uniqueItem) {
        const mult = 3 * level
        const recipeId = `${ID_PREFIX}${folderKey}/${level}`

        let recipe = event.recipes.gtceu.powered_enchanter(recipeId)
            .itemInputs('minecraft:book')
            .itemInputs(`${mult}x #forge:gems/lapis`)
            .itemInputs(`${mult}x ${uniqueItem}`)
            // .inputFluids(Fluid.of(FLUID_ID, fluidForLevel(level)))
            .itemOutputs(bookWithEnchant(enchantId, level, false))
            .duration(BASE_DURATION)
            .EUt(eutForLevel(level))
            .circuit(level)

        if (ENABLE_FLUIDS) {
            recipe.inputFluids(Fluid.of(FLUID_ID, fluidForLevel(level)))
        }
    }

    function makeCombineRecipe(folderKey, enchantId, outLevel) {
        const inLevel = outLevel - 1
        const recipeId = `${ID_PREFIX}${folderKey}/${outLevel}/combine`

        let recipe = event.recipes.gtceu.powered_enchanter(recipeId)
            .itemInputs(bookWithEnchant(enchantId, inLevel, true))
            .itemInputs(bookWithEnchant(enchantId, inLevel, true))
            // .inputFluids(Fluid.of(FLUID_ID, fluidForLevel(outLevel)))
            .itemOutputs(bookWithEnchant(enchantId, outLevel, false))
            .duration(BASE_DURATION)
            .EUt(eutForLevel(outLevel))
            .circuit(outLevel)

        if (ENABLE_FLUIDS) {
            recipe.inputFluids(Fluid.of(FLUID_ID, fluidForLevel(outLevel)))
        }
    }

    function generateEnchantSet(enchantId, def) {
        enchantId = String(enchantId)
        const maxLevel = Number(def.maxLevel || 1)
        const uniqueItem = String(def.uniqueItem || '')
        const folderKey = folderFromEnchantId(enchantId)

        if (!enchantId || !uniqueItem || !Number.isFinite(maxLevel) || maxLevel < 1) {
            console.error(`[Powered Enchanter] Bad enchant definition for ${enchantId}`)
            return
        }

        // Base recipes 1 to max
        for (let lvl = 1; lvl <= maxLevel; lvl++) {
            makeBaseRecipe(folderKey, enchantId, lvl, uniqueItem)
        }

        // Combine recipes 2 to max
        for (let lvl = 2; lvl <= maxLevel; lvl++) {
            makeCombineRecipe(folderKey, enchantId, lvl)
        }
    }

    Object.keys(ENCHANTS).forEach(enchantId => {
        generateEnchantSet(enchantId, ENCHANTS[enchantId])
    })


    // fin //

})
