// priority: 5
onEvent(`recipes`, e => {
  const sieve = (mesh, chance, input, result, wlog) => {
    e.custom({
      type: `exnihilosequentia:sieve`,
      rolls: [{
        chance: chance,
        mesh: mesh
      }],
      input: Ingredient.of(input).toJson(),
      result: Item.of(result).toResultJson(),
      waterlogged: wlog
    });
  };

  const hammer = (input, result) => {
    e.custom({
      type: `exnihilosequentia:hammer`,
      input: Ingredient.of(input).toJson(),
      results: [Item.of(result).toResultJson()]
    });
  };

  const heat = (block, heat) => {
    e.custom({
      type: `exnihilosequentia:heat`,
      block: block,
      amount: heat
    });
  };

  const hsieve = (item, source, rolls) => {
    e.custom({
      type: `excompressum:heavy_sieve_generated`,
      input: Ingredient.of(item).toJson(),
      source: source,
      rolls: rolls
    });
  };

  const hhammer = (input, output, count) => {
    e.custom({
      type: `excompressum:compressed_hammer`,
      input: Ingredient.of(input).toJson(),
      lootTable: {
        type: `minecraft:block`,
        pools: [{
          rolls: 1,
          entries: [{
            type: `minecraft:item`,
            name: output,
            functions: [{
              function: `minecraft:set_count`,
              count: count
            }]
          }],
          conditions: []
        }]
      }
    });
  };

  var exDust = `exnihilosequentia:dust`;
  var exRack = `exnihilosequentia:crushed_netherrack`;
  var exEnd = `exnihilosequentia:crushed_end_stone`;

  //Crucible heating `blocks`
  heat(`minecraft:lava`, 4);
  heat(`botania:blaze_block`, 8);
  heat(`mekanism:block_uranium`, 20);
  heat(`mekanism:superheating_element`, 60);

  //Params go like this: Mesh, Drop chance, Input item, Output item, Waterlogged.
  //Overworld sieve
  sieve(`diamond`, 0.3, `minecraft:gravel`, `mysticalagriculture:inferium_essence`, null);
  sieve(`diamond`, 0.2, `minecraft:gravel`, `mysticalagriculture:prosperity_shard`, null);
  sieve(`iron`, 0.25, `#minecraft:sand`, `minecraft:ink_sac`, true);
  //sieve(`iron`, 0.025, `minecraft:gravel`, `mekanism:fluorite_gem`, null);
  sieve(`diamond`, 0.05, `minecraft:gravel`, `mekanism:fluorite_gem`, null);
  //sieve(`flint`, 0.01, `#minecraft:sand`, `mekanism:salt`, null);
  sieve(`iron`, 0.02, `minecraft:sand`, `mekanism:salt`, null);
  sieve(`iron`, 0.05, `minecraft:gravel`, `powah:uraninite_raw_poor`, null);
  //sieve(`diamond`, 0.10, `minecraft:gravel`, `powah:uraninite_raw_poor`, null);
  //sieve(`netherite`, 0.25, `minecraft:gravel`, `powah:uraninite_raw_poor`, null);
  //sieve(`emerald`, 0.25, `minecraft:gravel`, `powah:uraninite_raw_poor`, null);
  //sieve(`diamond`, 0.4, `#minecraft:sand`, `mekanism:salt`, null);
  //sieve(`flint`, 0.05, `#forge:gravel`, `thermal:apatite`, null);
  sieve(`iron`, 0.1, `#forge:gravel`, `thermal:apatite`, null);
  //sieve(`diamond`, 0.15, `#forge:gravel`, `thermal:apatite`, null);
  //sieve(`flint`, 0.05, `#forge:gravel`, `thermal:cinnabar`, null);
  sieve(`iron`, 0.1, `#forge:gravel`, `thermal:cinnabar`, null);
  //sieve(`diamond`, 0.15, `#forge:gravel`, `thermal:cinnabar`, null);
  //sieve(`flint`, 0.05, `#forge:gravel`, `thermal:niter`, null);
  sieve(`iron`, 0.1, `#forge:gravel`, `thermal:niter`, null);
  //sieve(`diamond`, 0.15, `#forge:gravel`, `thermal:niter`, null);
  //sieve(`flint`, 0.05, `#forge:gravel`, `thermal:sulfur`, null);
  sieve(`iron`, 0.1, `#forge:gravel`, `thermal:sulfur`, null);
  //sieve(`diamond`, 0.15, `#forge:gravel`, `thermal:sulfur`, null);

  //Nether sieve
  sieve(`diamond`, 0.2, exRack, `appliedenergistics2:fluix_crystal_seed`, null);
  sieve(`diamond`, 0.3, exRack, `mysticalagriculture:inferium_essence`, null);
  sieve(`diamond`, 0.2, exRack, `minecraft:netherite_scrap`, null);
  sieve(`iron`, 0.2, exRack, `minecraft:quartz`, null);
  sieve(`iron`, 0.15, `#forge:soul_sand`, `mysticalagriculture:soulium_dust`, null);

  //End sieve
  sieve(`diamond`, 0.1, exEnd, `minecraft:chorus_flower`, null);

  //modium integration
  sieve(`netherite`, 0.150, `minecraft:gravel`, `kubejs:piece_hega`, null);
  sieve(`netherite`, 0.100, exRack, `kubejs:piece_gera`, null);
  sieve(`netherite`, 0.350, exRack, `kubejs:piece_cobalt`, null);

  //Heavy sieving | Params: Item to sieve, normal sieving item, how many rolls(multiplier for drops)
  hsieve(`compressium:cobblestone_1`, `minecraft:cobblestone`, 9);
  hsieve(`compressium:gravel_1`, `minecraft:gravel`, 9);
  hsieve(`compressium:sand_1`, `minecraft:sand`, 9);
  hsieve(`compressium:dirt_1`, `minecraft:dirt`, 9);
  hsieve(`compressium:soulsand_1`, `minecraft:soul_sand`, 9);

  //Hammer recipes
  hammer(`#minecraft:logs`, `thermal:sawdust`);

  //Heavy hammer
  hhammer(`compressium:cobblestone_1`, `minecraft:gravel`, 9);
  hhammer(`compressium:gravel_1`, `minecraft:sand`, 9);
  hhammer(`compressium:sand_1`, exDust, 9);
  hhammer(`compressium:netherrack_1`, exRack, 9);
  hhammer(`compressium:endstone_1`, exEnd, 9);
  hhammer(`compressium:diorite_1`, `exnihilosequentia:crushed_diorite`, 9);
  hhammer(`compressium:granite_1`, `exnihilosequentia:crushed_granite`, 9);
  hhammer(`compressium:andesite_1`, `exnihilosequentia:crushed_andesite`, 9);

  var chunks = [
    `hega`,
    `gera`,
    `cobalt`,
  ];
  chunks.forEach(name => {
    e.shaped(`kubejs:chunk_${name}`, [
      `aa`,
      `aa`
    ], {
      a: `kubejs:piece_${name}`
    });
  });

  const crushEm = (result, crush) => {
    e.recipes.mekanism.crushing(Item.of(result), crush);
    e.custom({
      type: `pedestals:pedestal_crushing`,
      ingredient: Ingredient.of(crush).toJson(),
      result: Item.of(result).toResultJson(),
    });
    e.custom({
      type: `appliedenergistics2:grinder`,
      input: Ingredient.of(crush).toJson(),
      result: {
        primary: Item.of(result).toResultJson()
      },
      turns: 8
    });
  };

  crushEm(exDust, `#minecraft:sand`);
  crushEm(exRack, `#forge:netherrack`);
  crushEm(exEnd, `#forge:end_stones`);

  var pieces = [
    `osmium`,
    `copper`,
    `lead`,
    `nickel`,
    `silver`,
    `tin`,
    `aluminum`,
    `platinum`,
    `uranium`,
    `zinc`,
    `iron`,
    `gold`,
    `hega`,
    `gera`,
  ];

});
