//priority: 3005

// Mod shortcuts
let MOD = (domain, id, x) => (x ? `${x}x ` : "") + (id ? (id.startsWith('#') ? '#' : "") : "") + domain + (id ? ":" : "") + (id ? id.replace('#', '') : "")
let AE2 = (id, x) => MOD("appliedenergistics2", id, x)
let AE2E = (id, x) => MOD("ae2extras", id, x)
let CR = (id, x) => MOD("create", id, x)
let CF = (id, x) => MOD("comforts", id, x)
let TC = (id, x) => MOD("tconstruct", id, x)
let MC = (id, x) => MOD("minecraft", id, x)
let KJ = (id, x) => MOD("kubejs", id, x)
let SD = (id, x) => MOD("storagedrawers", id, x)
let F = (id, x) => MOD("forge", id, x)
let Q = (id, x) => MOD("quark", id, x)
let PHCFC = (id, x) => MOD("pamhc2foodcore", id, x)
let ENS = (id, x) => MOD("exnihilosequentia", id, x)
let ENS_AE2 = (id, x) => MOD("exnihiloae", id, x)
let EE = (id, x) => MOD("emendatusenigmatica", id, x)
let MU = (id, x) => MOD("miniutilities", id, x)
let DU = (id, x) => MOD("darkutils", id, x)
let BT = (id, x) => MOD("boss_tools", id, x)
let IF = (id, x) => MOD("industrialforegoing", id, x)
let RTB = (id, x) => MOD("rftoolsbase", id, x)
let PR_C = (id, x) => MOD("projectred-core", id, x)
let PR_T = (id, x) => MOD("projectred-transmission", id, x)
let PR_I = (id, x) => MOD("projectred-illumination", id, x)
let PR_INT = (id, x) => MOD("projectred-integration", id, x)
let TM = (id, x) => MOD("torchmaster", id, x)
let M = (id, x) => MOD("mekanism", id, x)
let MG = (id, x) => MOD("mekanismgenerators", id, x)
let WH = (id, x) => MOD("woodenhopper", id, x)
let SH = (id, x) => MOD("speedyhoppers", id, x)
let CP = (id, x) => MOD("compressium", id, x)
let CG = (id, x) => MOD("cobblefordays", id, x)
let EC = (id, x) => MOD("extendedcrafting", id, x)
let ES = (id, x) => MOD("enderstorage", id, x)
let X = (id, x) => MOD("xnet", id, x)
let PRE = (id, x) => MOD("projecte", id, x)
let PREXP = (id, x) => MOD("projectexpansion", id, x)
let PREXT = (id, x) => MOD("projectextended", id, x)
let CM = (id, x) => MOD("cagedmobs", id, x)
let FFB = (id, x) => MOD("farmingforblockheads", id, x)
let AV = (id, x) => MOD("avaritia", id, x)
let TR = (id, x) => MOD("trashcans", id, x)
let BP = (id, x) => MOD("botanypots", id, x)
let PP = (id, x) => MOD("ftb-power-pots", id, x)
let IFC = (id, x) => MOD("ironfurnaces", id, x)
let IC = (id, x) => MOD("ironchest", id, x)


let colours = ['white', 'orange', 'magenta', 'light_blue', 'lime', 'pink', 'purple', 'light_gray', 'gray', 'cyan', 'brown', 'green', 'blue', 'red', 'black', 'yellow']
let wood_types = [MC('oak'), MC('spruce'), MC('birch'), MC('jungle'), MC('acacia'), MC('dark_oak'), MC('crimson'), MC('warped')]

const processingTimesDefault = {
	milling: 300,
	crushing: 100,
}

let donutCraft = (event, output, center, ring) => {
	event.shaped(output, [
		'SSS',
		'SCS',
		'SSS'
	], {
		C: center,
		S: ring
	})
}

function ifiniDeploying(output, input, tool) {
	return {
		"type": "create:deploying",
		"ingredients": [
			Ingredient.of(input).toJson(),
			Ingredient.of(tool).toJson()
		],
		"results": [
			Item.of(output).toResultJson()
		],
		"keepHeldItem": true
	}
}

const air = 'minecraft:air';

const modPriorities = [
    'appliedenergistics2',
    'emendatusenigmatica',
    'minecraft',
    'mekanism',
    'kubejs',
    'create',
    'industrialforegoing',
    'quark',
    'titanium',
    'exnihilosequentia',
    'tconstruct',
    'miniutilities',
    'boss_tools'
];

function shapedRecipe(result, pattern, key, id) {
    return { result: result, pattern: pattern, key: key, id: id };
}

let modifyShaped = (e, result, count, pattern, ingredients) => {
    e.remove({ output: result, type: 'minecraft:crafting_shaped' })
    e.shaped(item.of(result, count), pattern, ingredients).id(`kubejs:shaped/${result.replace(':', '/')}`)
}

function shapelessRecipe(result, ingredients, id) {
    return { result: result, ingredients: ingredients, id: id };
}

let modifyShapeless = (e, result, count, ingredients) => {
    e.remove({ output: result, type: 'minecraft:crafting_shapeless' })
    e.shapeless(item.of(result, count), ingredients).id(`kubejs:shapeless/${result.replace(':', '/')}`)
}

let modifySmelt = (e, result, ingredients) => {
    e.remove({ output: result, type: 'minecraft:smelting' })
    e.smelting(result, ingredients).id(`kubejs:smelting/${result.replace(':', '/')}`)
}

let removeRecipeByID = (e, recipes) => {
    recipes.forEach(recipe => {
        e.remove({ id: recipe })
    })
}

let removeRecipeByInput = (e, recipes) => {
    recipes.forEach(recipe => {
        if (Array.isArray(recipe)) {
            e.remove({ type: recipe[1], input: recipe[0] })
        } else {
            e.remove({ input: recipe })
        }
    })
}

let removeRecipeByOutput = (e, recipes) => {
    recipes.forEach(recipe => {
        if (Array.isArray(recipe)) {
            e.remove({ type: recipe[1], output: recipe[0] })
        } else {
            e.remove({ output: recipe })
        }
    })
}

function tagIsEmpty(tag) {
    return getPreferredItemInTag(Ingredient.of(tag)).id == air;
}

function getPreferredItemInTag(tag) {
    let pref =
        utils
            .listOf(tag.stacks)
            .toArray()
            .sort(({ mod: a }, { mod: b }) => compareIndices(a, b, tag))[0] || Item.of(air);
    return pref;
}
function compareIndices(a, b, tag) {
    if (a == b) return 0; // if a == b, they'll be found at the same position in modPriorities

    for (let mod of modPriorities) {
        if (mod == a) return -1; // if a comes before b, then idx(a) < idx(b), so -1
        if (mod == b) return 1; // if a comes after b, then idx(a) > idx(b), so 1
    }

    console.error('[' + a + ', ' + b + '] were both unaccounted for in mod unification' + (tag ? ' for ' + tag : '!'));
    return 0;
}

const playerHas = (item, player) => {
    return player.inventory.find(item) != -1;
};

const armorMats = {
    hazmat: {
        A: F("#plates/lead"),
		B: F("#dyes/orange"),
		C: Item.of('extendedcrafting:singularity', { Id:"extendedcrafting:klein_star_omega" }),
		D: PREXP("magenta_matter"),
		F: Item.of('extendedcrafting:singularity', { Id:"extendedcrafting:lead" }),
		G: Item.of('extendedcrafting:singularity', { Id:"extendedcrafting:lead" }),
        base: {
            helmet: MU('unstable_helmet'),
            chestplate: MU('unstable_chestplate'),
            pants: MU('unstable_leggings'),
            boots: MU('unstable_boots'),
        },
    },
    meka: {
        A: M("hdpe_sheet"),
		B: M("ultimate_control_circuit"),
		C: M("basic_induction_cell"),
		D: PREXP("cyan_matter"),
		F: M("reprocessed_fissile_fragment"),
		G: M("reprocessed_fissile_fragment"),
        base: {
            helmet: MU('infused_helmet'),
            chestplate: MU('infused_chestplate'),
            pants: MU('infused_leggings'),
            boots: MU('infused_boots'),
        },
    },
    space: {
        A: BT('compressed_steel'),
        B: M('hdpe_stick'),
        C: BT('oxygen_tank'),
        D: PREXP("violet_matter"),
        F: PREXP('magnum_star_ein'),
        G: BT('oxygen_gear'),
        base: {
            helmet: M('hazmat_mask'),
            chestplate: M('hazmat_gown'),
            pants: M('hazmat_pants'),
            boots: M('hazmat_boots'),
        },
    },
    spaceNetherite: {
        A: MC('netherite_ingot'),
        B: BT('compressed_desh'),
        C: MU('the_final_opinium_core'),
        D: PREXP("blue_matter"),
        F: PREXP('magnum_star_zwei'),
        G: AV('neutronium_ingot'),
        base: {
            helmet: BT('oxygen_mask'),
            chestplate: BT('space_suit'),
            pants: BT('space_pants'),
            boots: BT('space_boots'),
        },
    },
    infused: {
        A: MU('nether_star_opinium_core'),
        B: MU('the_final_opinium_core'),
        C: PREXP('magnum_star_zwei'),
        D: PREXP("blue_matter"),
        F: Item.of('extendedcrafting:singularity', {Id:"extendedcrafting:blue_matter"}),
        G: MU('nether_star_opinium_core'),
        base: {
            helmet: PRE('gem_helmet'),
            chestplate: PRE('gem_chestplate'),
            pants: PRE('gem_leggings'),
            boots: PRE('gem_boots'),
        },
    },
}

const ultArmorObj = (mat, base) => {
    return {
      A: mat.A,
      B: mat.B,
      C: mat.C,
      D: mat.D,
      E: mat.base[base],
      F: mat.F,
      G: mat.G,
    }
}

const templates = {
    ultimate: {
        helmet: [
            ' AAAAAAA ',
            'AABBBBBAA',
            'AFBDDDBFA',
            'ABBDEDBBA',
            'ABCDDDCBA',
            'AB     BA',
            'AB     BA',
            'AB     BA',
            'ABG   GBA',
        ],
        chestplate: [
            'AAAA AAAA',
            'AABBBBBAA',
            'AFBDDDBFA',
            'ABBDEDBBA',
            'ABCDDDCBA',
            'ABBBGBBBA',
            'AAABBBAAA',
            'AAAAAAAAA',
            ' AAAAAAA ',
        ],
        pants: [
            'AAAAAAAAA',
            'ABBBBBBBA',
            'ABADDDABA',
            'ABADEDABA',
            'ABCDDDCBA',
            'ABAF FABA',
            'ABA   ABA',
            'ABA   ABA',
            'AGA   AGA',
        ],
        boots: [
            '         ',
            '         ',
            '   DDD   ',
            '   DED   ',
            'AACDDDCAA',
            'ABBA ABBA',
            'ABBA ABBA',
            'ABBA ABBA',
            'FBBG GBBF',
        ]
    },
}