// https://github.com/AquaThree/AvaritiaNeo/blob/main/example/kubejs/example.md

ServerEvents.recipes(event => {
  event.remove({ output: 'avaritia:infinity_catalyst' });
  event.remove({ output: 'avaritia:cosmic_meatballs' });
  event.remove({ output: 'avaritia:ultimate_stew' });
  event.remove({ output: 'avaritia:infinity_sword' });
  event.remove({ output: 'avaritia:skullfire_sword' });

  event.forEachRecipe({ type: 'avaritia:extreme_shaped' }, r => {
    let keys = {}
    let keysJson = r.json.get('key')
    keysJson.keySet().map((key) => {
      let value = keysJson.get(key)
      if (value.getAsJsonObject().has('tag')) {
        keys[key] = '#' + value.get('tag').getAsString()
      } else {
        keys[key] = value.get('item').getAsString()
      }
    });
    event.recipes.create.mechanical_crafting(r.originalRecipeResult, r.json.get('pattern'), keys);
  })

  let extremeShaped = (output, pattern, key) => {
    event.recipes.create.mechanical_crafting(output, pattern, key);
    event.recipes.avaritia.extreme_shaped(pattern, key, output);
  }
  let extremeShapeless = (output, pattern, key) => {
    event.recipes.create.mechanical_crafting(output, pattern, key);
    let items = [].concat.apply([], pattern.map((line) => line.trim().split('').map((ch) => key[ch])));
    event.recipes.avaritia.extreme_shapeless(items, output);
  }

  // Infinity Catalyst
  const infinityCatalystPattern = ['DCPNIRUME', 'S        ']
  extremeShapeless('avaritia:infinity_catalyst', infinityCatalystPattern, {
    D: "avaritia:diamond_lattice",
    C: "avaritia:crystal_matrix_ingot",
    P: "avaritia:neutron_pile",
    N: "avaritia:neutron_nugget",
    I: "avaritia:neutronium_ingot",
    R: "avaritia:record_fragment",
    U: "avaritia:ultimate_stew",
    M: "avaritia:cosmic_meatballs",
    E: "avaritia:endest_pearl",
    S: "avaritia:infinity_singularity"
  });

  // Replace singularity recipes and add new ones
  event.remove({ type: 'avaritia:compressor' })
  const singularityIO = {
    "minecraft:copper_ingot"   : "copper_singularity",
    "minecraft:iron_ingot"     : "iron_singularity",
    "minecraft:gold_ingot"     : "gold_singularity",
    "minecraft:lapis_lazuli"   : "lapis_singularity",
    "minecraft:redstone"       : "redstone_singularity",
    "minecraft:quartz"         : "quartz_singularity",
    "minecraft:diamond"        : "diamond_singularity",
    "minecraft:emerald"        : "emerald_singularity",
    "minecraft:amethyst_shard" : "amethyst_singularity",
    "minecraft:netherite_scrap": "netherite_singularity",
    "create:zinc_ingot"        : "zinc_singularity",
    "create:brass_ingot"       : "brass_singularity",
  };
  for (let key in singularityIO) {
    let input = key
    let output = singularityIO[key]
    let item = input.startsWith('minecraft:') ? Item.of('avaritia:' + output) : Item.of("avaritia:json_singularity", {"singularity_id": output})
    event.recipes.avaritia.compressor([input], item, 4000);
  }

  // Cosmic Meatballs
  extremeShapeless('avaritia:cosmic_meatballs', ['FPBMCSKHV', 'UZRTQYEOG', 'JNAI     '], {
    F: '#forge:cooked_fishes',
    P: '#forge:cooked_pork',
    B: '#forge:cooked_beef',
    M: '#forge:cooked_mutton',
    C: '#forge:cooked_chicken',
    S: '#forge:cooked_seafood',
    K: 'croptopia:sausage',
    H: 'farmersdelight:smoked_ham',
    V: 'createfood:pork_meatball',
    U: 'createfood:meatball',
    Z: 'createfood:strider_meatball',
    R: 'createfood:rabbit_meatball',
    T: 'createfood:endermite_meatball',
    Q: 'quarkdelight:crab_bars',
    Y: 'createfood:bacon_bits',
    E: '#forge:eggs',
    O: '#c:crops/onion',
    G: 'croptopia:garlic',
    J: '#forge:chili_pepper',
    N: '#c:salt',
    A: 'croptopia:pepper',
    I: 'avaritia:neutronium_ingot'
  });

  // Ultimate Stew
  extremeShapeless('avaritia:ultimate_stew', ['CPBFMWVVV', 'VTOGJNAI '], {
    C: '#forge:carrot',
    P: '#forge:vegetables/potato',
    B: '#forge:beetroot',
    F: '#forge:fruits',
    M: '#c:mushrooms',
    W: 'minecraft:glow_berries',
    V: '#c:vegetables',
    T: '#c:crops/tomato',
    O: '#c:crops/onion',
    G: 'croptopia:garlic',
    J: '#forge:chili_pepper',
    N: '#c:salt',
    A: 'croptopia:pepper',
    I: 'avaritia:neutronium_ingot'
  });

  // Sword of the Cosmos
  extremeShaped('avaritia:infinity_sword', [
      '       II',
      '      IQI',
      '     IEI ',
      '    IVI  ',
      ' C IEI   ',
      '  MQI    ',
      '  NM     ',
      ' D  C    ',
      'C        '],
    {
    C: 'avaritia:infinity_catalyst',
    N: 'avaritia:neutronium_block',
    M: 'avaritia:crystal_matrix_block',
    I: 'avaritia:infinity_block',
    E: 'cataclysm:essence_of_the_storm',
    Q: 'advanced_ae:quantum_alloy_block',
    V: 'cataclysm:void_core',
    D: 'block_factorys_bosses:dragon_bone'
  });

  // Skullfire Sword
  extremeShaped('avaritia:skullfire_sword', [
    '       MM',
    '      MBM',
    '     MBM ',
    '    MNM  ',
    ' S MNM   ',
    '  SMM    ',
    '  CS     ',
    ' C  S    ',
    'S        '],
    {
      S: 'minecraft:nether_star',
      M: 'avaritia:crystal_matrix_ingot',
      C: 'create_tank_defenses:steel_casing',
      N: 'avaritia:neutronium_ingot',
      B: 'botania:blaze_block'
    });
});