//priority: 997
onEvent("recipes", (e) => {
  // #region Functions
  function unifyMetal(name, ingotItem, dustItem, blockItem, nuggetItem) {
    e.replaceOutput(`#forge:ingots/${name}`, ingotItem);
    e.replaceOutput(`#forge:dusts/${name}`, dustItem);
    e.replaceOutput(`#forge:nuggets/${name}`, nuggetItem);
    e.replaceOutput(`#forge:storage_blocks/${name}`, blockItem);

    e.remove({
      input: `#forge:ores/${name}`,
      type: "immersiveengineering:crusher",
    });
    e.remove({
      input: `#forge:ingots/${name}`,
      type: "immersiveengineering:crusher",
    });
    e.remove({
      input: [`#forge:ores/${name}`, `#forge:dusts/${name}`],
      output: `#forge:ingots/${name}`,
      type: "minecraft:smelting",
    });
    e.remove({
      input: [`#forge:ores/${name}`, `#forge:dusts/${name}`],
      output: `#forge:ingots/${name}`,
      type: "minecraft:blasting",
    });
    e.remove({ id: `appliedenergistics2:grinder/${name}_dust_ingot` });

    if (
      name !== "iron" &&
      name !== "gold" &&
      name !== "azure_silver" &&
      name !== "crimson_iron"
    ) {
      e.remove({
        input: `#forge:ores/${name}`,
        output: `mekanism:dust_${name}`,
        type: "mekanism:enriching",
      });
      e.remove({
        input: `mekanism:dirty_dust_${name}`,
        output: `mekanism:dust_${name}`,
        type: "mekanism:enriching",
      });
      e.remove({
        input: `#mekanism:clumps/${name}`,
        output: `mekanism:dirty_dust_${name}`,
        type: "mekanism:crushing",
      });
      e.remove({
        input: `#forge:ores/${name}`,
        output: `mekanism:clump_${name}`,
        type: "mekanism:purifying",
      });
      e.remove({
        input: `mekanism:shard_${name}`,
        output: `mekanism:clump_${name}`,
        type: "mekanism:purifying",
      });
      e.remove({
        input: `#forge:ores/${name}`,
        output: `mekanism:shard_${name}`,
        type: "mekanism:injecting",
      });
      e.remove({
        input: `#mekanism:crystals/${name}`,
        output: `mekanism:shard_${name}`,
        type: "mekanism:injecting",
      });
    }

    e.recipes.minecraft.smelting(ingotItem, `#forge:dusts/${name}`).xp(0.5);
    e.recipes.minecraft.blasting(ingotItem, `#forge:dusts/${name}`).xp(0.5);
    e.recipes.appliedenergistics2.grinder({
      input: { tag: `forge:ingots/${name}` },
      result: { primary: { item: dustItem } },
      turns: 8,
    });
    e.recipes.immersiveengineering.crusher({
      secondaries: [],
      result: { base_ingredient: { item: dustItem } },
      input: { tag: `forge:ingots/${name}` },
      energy: 3000,
    });

    if (!ingredient.of(`#forge:ores/${name}`).stacks.empty) {
      e.recipes.minecraft.smelting(ingotItem, `#forge:ores/${name}`).xp(1);
      e.recipes.minecraft.blasting(ingotItem, `#forge:ores/${name}`).xp(1);
      // e.recipes.mekanism.enriching(item.of(dustItem, 2), `#forge:ores/${name}`);
      e.recipes.immersiveengineering.crusher({
        secondaries: [],
        result: { count: 2, base_ingredient: { item: dustItem } },
        input: { tag: `forge:ores/${name}` },
        energy: 3000,
      });

      e.remove({ id: `appliedenergistics2:grinder/${name}_dust_ore` });
      e.recipes.appliedenergistics2.grinder({
        input: { tag: `forge:ores/${name}` },
        result: { primary: { item: dustItem, count: 2 } },
        turns: 8,
      });
    }

    e.replaceInput(nuggetItem, `#forge:nuggets/${name}`);
    e.replaceInput(dustItem, `#forge:dusts/${name}`);
    e.replaceInput(ingotItem, `#forge:ingots/${name}`);
    e.replaceInput(blockItem, `#forge:storage_blocks/${name}`);
  }

  function unifyCraftMetal(name, ingotItem, dustItem, blockItem, nuggetItem) {
    e.replaceInput(nuggetItem, `#forge:nuggets/${name}`);
    e.replaceInput(dustItem, `#forge:dusts/${name}`);
    e.replaceInput(ingotItem, `#forge:ingots/${name}`);
    e.replaceInput(blockItem, `#forge:storage_blocks/${name}`);
    e.replaceOutput(`#forge:ingots/${name}`, ingotItem);
    e.replaceOutput(`#forge:dusts/${name}`, dustItem);
    e.replaceOutput(`#forge:nuggets/${name}`, nuggetItem);
    e.replaceOutput(`#forge:storage_blocks/${name}`, blockItem);
  }

  // #endregion Functions
  // exemple denvoi de fichier pour unification
  // #region Metal Unification
  //Vanilla MC
  unifyMetal(
    "gold",
    "minecraft:gold_ingot",
    "alltheores:gold_dust",
    "minecraft:gold_block",
    "minecraft:gold_nugget"
  );
  unifyMetal(
    "iron",
    "minecraft:iron_ingot",
    "alltheores:iron_dust",
    "minecraft:iron_block",
    "minecraft:iron_nugget"
  );

  //Multiple Mods
  unifyMetal(
    "aluminum",
    "alltheores:aluminum_ingot",
    "alltheores:aluminum_dust",
    "alltheores:aluminum_block",
    "alltheores:aluminum_nugget"
  );
  unifyMetal(
    "lead",
    "alltheores:lead_ingot",
    "alltheores:lead_dust",
    "alltheores:lead_block",
    "alltheores:lead_nugget"
  );
  unifyMetal(
    "nickel",
    "alltheores:nickel_ingot",
    "alltheores:nickel_dust",
    "alltheores:nickel_block",
    "alltheores:nickel_nugget"
  );
  unifyMetal(
    "platinum",
    "alltheores:platinum_ingot",
    "alltheores:platinum_dust",
    "alltheores:platinum_block",
    "alltheores:platinum_nugget"
  );
  unifyMetal(
    "silver",
    "alltheores:silver_ingot",
    "alltheores:silver_dust",
    "alltheores:silver_block",
    "alltheores:silver_nugget"
  );
  unifyMetal(
    "tin",
    "alltheores:tin_ingot",
    "alltheores:tin_dust",
    "alltheores:tin_block",
    "alltheores:tin_nugget"
  );
  unifyMetal(
    "uranium",
    "alltheores:uranium_ingot",
    "alltheores:uranium_dust",
    "alltheores:uranium_block",
    "alltheores:uranium_nugget"
  );
  unifyMetal(
    "zinc",
    "alltheores:zinc_ingot",
    "alltheores:zinc_dust",
    "alltheores:zinc_block",
    "alltheores:zinc_nugget"
  );

  unifyMetal(
    "copper",
    "alltheores:copper_ingot",
    "alltheores:copper_dust",
    "alltheores:copper_block",
    "alltheores:copper_nugget"
  );

  //Mekanis
  unifyMetal(
    "osmium",
    "alltheores:osmium_ingot",
    "alltheores:osmium_dust",
    "alltheores:osmium_block",
    "alltheores:osmium_nugget"
  );

  //Craft only metals
  unifyCraftMetal(
    "steel",
    "mekanism:ingot_steel",
    "mekanism:dust_steel",
    "mekanism:block_steel",
    "mekanism:nugget_steel"
  );

  //Thermal
  unifyCraftMetal(
    "bronze",
    "thermal:bronze_ingot",
    "thermal:bronze_dust",
    "thermal:bronze_block",
    "thermal:bronze_nugget"
  );
  unifyCraftMetal(
    "constantan",
    "thermal:constantan_ingot",
    "thermal:constantan_dust",
    "thermal:constantan_block",
    "thermal:constantan_nugget"
  );
  unifyCraftMetal(
    "electrum",
    "thermal:electrum_ingot",
    "thermal:electrum_dust",
    "thermal:electrum_block",
    "thermal:electrum_nugget"
  );
  // #endregion Metal Unification

  // #region Tinkers Unification
  function tinkerMelting(
    material,
    type,
    typeValues,
    temperature,
    time,
    byproduct
  ) {
    const recipe = {
      type: "tconstruct:melting",
      ingredient: { tag: `forge:${type}/${material}` },
      result: {
        fluid: `alltheores:molten_${material}`,
        amount: typeValues.amount,
      },
      temperature: temperature,
      time: time * typeValues.multiplier,
    };
    e.custom(
      type != "ores" ? recipe : Object.assign({ byproducts: byproduct }, recipe)
    ).id(`kubejs:melting/${type}/${material}`);
  }
  function tinkerBlockMelting(entries) {
    entries.forEach(([input, fluidAmount, time, byproductAmount], index) => {
      e.custom({
        type: "tconstruct:melting",
        ingredient: input,
        result: { fluid: "alltheores:molten_copper", amount: fluidAmount },
        temperature: 500,
        time: time,
        byproducts: [
          { fluid: "tconstruct:seared_stone", amount: byproductAmount },
        ],
      }).id(`kubejs:melting/copper/fromblock/${index + 1}`);
    });
  }
  function tinkerAlloys(entries) {
    entries.forEach(([output, outputAmount, temperature, inputs]) => {
      e.remove({ id: `tconstruct:smeltery/alloys/molten_${output}` });
      e.custom({
        type: "tconstruct:alloy",
        inputs: inputs,
        result: { fluid: `tconstruct:molten_${output}`, amount: outputAmount },
        temperature: temperature,
      }).id(`kubejs:melting/alloys/${output}`);
    });
  }
  function unifyTinkers(entries) {
    const meltingTypes = [
      "block",
      "can",
      "coin",
      "dust",
      "gear",
      "ingot",
      "nugget",
      "ore",
      "plates",
      "rod",
      "sheetmetal",
    ];
    const meltingTypeValues = {
      storage_blocks: { amount: 1296, multiplier: 88.125 },
      dusts: { amount: 144, multiplier: 21.75 },
      gears: { amount: 576, multiplier: 58.8 },
      ingots: { amount: 144, multiplier: 29.4 },
      nuggets: { amount: 16, multiplier: 10.0 },
      ores: { amount: 288, multiplier: 44.1 },
      plates: { amount: 144, multiplier: 29.4 },
      rods: { amount: 72, multiplier: 5.88 },
      sheetmetals: { amount: 144, multiplier: 29.4 },
    };
    const meltingParts = [
      "broad_axe_head",
      "broad_blade",
      "hammer_head",
      "large_plate",
      "pickaxe_head",
      "repair_kit",
      "small_axe_head",
      "small_blade",
      "tool_binding",
      "tool_handle",
      "tough_handle",
    ];

    entries.forEach(([material, temperature, time, types, byproduct]) => {
      e.replaceInput(
        `tconstruct:${material}_ingot`,
        `#forge:ingots/${material}`
      );
      e.replaceInput(
        `tmechworks:${material}_ingot`,
        `#forge:ingots/${material}`
      );
      e.replaceInput(
        `tconstruct:${material}_block`,
        `#forge:storage_blocks/${material}`
      );
      e.replaceInput(
        `tmechworks:${material}_block`,
        `#forge:storage_blocks/${material}`
      );
      e.replaceOutput(
        `tconstruct:${material}_ingot`,
        `alltheores:${material}_ingot`
      );
      e.replaceOutput(
        `tmechworks:${material}_ingot`,
        `alltheores:${material}_ingot`
      );
      e.replaceOutput(
        `tconstruct:${material}_block`,
        `alltheores:${material}_block`
      );
      e.replaceOutput(
        `tmechworks:${material}_block`,
        `alltheores:${material}_block`
      );

      meltingTypes.forEach((meltingType) => {
        e.remove({
          id: `tconstruct:smeltery/melting/metal/${material}/${meltingType}`,
        });
        for (let type in meltingTypeValues)
          if (types.includes(type))
            tinkerMelting(
              material,
              type,
              meltingTypeValues[type],
              temperature,
              time,
              byproduct
            );
      });
      meltingParts.forEach((meltingPart) =>
        e.remove({
          id: `tconstruct:tools/parts/melting/${meltingPart}/tconstruct/${material}`,
        })
      );
    });
  }
  function tinkerAlloyMelting(entries) {
    entries.forEach(([input, outputItem, outputBlock, outputNugget]) => {
      e.remove({ id: `tconstruct:smeltery/casting/metal/${input}/block` });

      e.remove({
        id: `tconstruct:smeltery/casting/metal/${input}/ingot_gold_cast`,
      });
      e.remove({
        id: `tconstruct:smeltery/casting/metal/${input}/ingot_sand_cast`,
      });

      e.remove({
        id: `tconstruct:smeltery/casting/metal/${input}/nugget_gold_cast`,
      });
      e.remove({
        id: `tconstruct:smeltery/casting/metal/${input}/nugget_sand_cast`,
      });

      e.custom({
        type: "tconstruct:casting_basin",
        fluid: { tag: `tconstruct:molten_${input}`, amount: 1296 },
        result: { item: `${outputBlock}` },
        cooling_time: 171,
      }).id(`kubejs:smeltery/casting/metal/${input}/block`);

      e.custom({
        type: "tconstruct:casting_table",
        cast: { tag: "tconstruct:casts/multi_use/ingot" },
        fluid: { tag: `tconstruct:molten_${input}`, amount: 144 },
        result: { item: `${outputItem}` },
        cooling_time: 57,
      }).id(`kubejs:smeltery/casting/metal/${input}/ingot_gold_cast`);
      e.custom({
        type: "tconstruct:casting_table",
        cast: { tag: "tconstruct:casts/single_use/ingot" },
        cast_consumed: true,
        fluid: { tag: `tconstruct:molten_${input}`, amount: 144 },
        result: { item: `${outputItem}` },
        cooling_time: 57,
      }).id(`kubejs:smeltery/casting/metal/${input}/ingot_sand_cast`);

      e.custom({
        type: "tconstruct:casting_table",
        cast: { tag: "tconstruct:casts/multi_use/nugget" },
        fluid: { tag: `tconstruct:molten_${input}`, amount: 16 },
        result: { item: `${outputNugget}` },
        cooling_time: 19,
      }).id(`kubejs:smeltery/casting/metal/${input}/nugget_gold_cast`);
      e.custom({
        type: "tconstruct:casting_table",
        cast: { tag: "tconstruct:casts/single_use/nugget" },
        cast_consumed: true,
        fluid: { tag: `tconstruct:molten_${input}`, amount: 16 },
        result: { item: `${outputNugget}` },
        cooling_time: 19,
      }).id(`kubejs:smeltery/casting/metal/${input}/nugget_sand_cast`);
    });
  }

  tinkerAlloyMelting([
    [
      "bronze",
      "thermal:bronze_ingot",
      "thermal:bronze_block",
      "thermal:bronze_nugget",
    ],
    [
      "constantan",
      "thermal:constantan_ingot",
      "thermal:constantan_block",
      "thermal:constantan_nugget",
    ],
    [
      "electrum",
      "thermal:electrum_ingot",
      "thermal:electrum_block",
      "thermal:electrum_nugget",
    ],
  ]);

  unifyTinkers([
    [
      "aluminum",
      425,
      1.6,
      [
        "storage_blocks",
        "dusts",
        "ingots",
        "nuggets",
        "ores",
        "plates",
        "rods",
        "sheetmetals",
      ],
      [{ fluid: "tconstruct:molten_iron", amount: 48 }],
    ],
    [
      "copper",
      500,
      1.7,
      [
        "storage_blocks",
        "dusts",
        "gears",
        "ingots",
        "nuggets",
        "ores",
        "plates",
        "sheetmetals",
      ],
      [{ fluid: "tconstruct:molten_gold", amount: 16 }],
    ],
    [
      "lead",
      330,
      1.4,
      [
        "storage_blocks",
        "dusts",
        "gears",
        "ingots",
        "nuggets",
        "ores",
        "plates",
        "sheetmetals",
      ],
      [{ fluid: "alltheores:molten_silver", amount: 48 }],
    ],
    [
      "nickel",
      950,
      2.2,
      [
        "storage_blocks",
        "dusts",
        "gears",
        "ingots",
        "nuggets",
        "ores",
        "plates",
        "sheetmetals",
      ],
      [{ fluid: "alltheores:molten_platinum", amount: 16 }],
    ],
    [
      "osmium",
      975,
      2.2,
      ["storage_blocks", "dusts", "ingots", "nuggets", "ores"],
      [{ fluid: "tconstruct:molten_iron", amount: 48 }],
    ],
    [
      "platinum",
      970,
      2.2,
      ["storage_blocks", "dusts", "ingots", "nuggets", "ores"],
      [{ fluid: "tconstruct:molten_gold", amount: 48 }],
    ],
    [
      "silver",
      790,
      2.0,
      [
        "storage_blocks",
        "dusts",
        "gears",
        "ingots",
        "nuggets",
        "ores",
        "plates",
        "sheetmetals",
      ],
      [{ fluid: "alltheores:molten_lead", amount: 48 }],
    ],
    [
      "tin",
      225,
      1.3,
      [
        "storage_blocks",
        "dusts",
        "gears",
        "ingots",
        "nuggets",
        "ores",
        "plates",
      ],
      [{ fluid: "alltheores:molten_copper", amount: 48 }],
    ],
    [
      "uranium",
      830,
      2.0,
      [
        "storage_blocks",
        "dusts",
        "ingots",
        "nuggets",
        "ores",
        "plates",
        "sheetmetals",
      ],
      [{ fluid: "alltheores:molten_lead", amount: 48 }],
    ],
    [
      "zinc",
      420,
      1.6,
      ["storage_blocks", "dusts", "ingots", "nuggets", "ores"],
      [{ fluid: "alltheores:molten_tin", amount: 48 }],
    ],
  ]);

  tinkerAlloys([
    [
      "brass",
      288,
      605,
      [
        { tag: "forge:molten_copper", amount: 144 },
        { tag: "forge:molten_zinc", amount: 144 },
      ],
    ],
    [
      "bronze",
      576,
      700,
      [
        { tag: "forge:molten_copper", amount: 432 },
        { tag: "forge:molten_tin", amount: 144 },
      ],
    ],
    [
      "constantan",
      288,
      920,
      [
        { tag: "forge:molten_copper", amount: 144 },
        { tag: "forge:molten_nickel", amount: 144 },
      ],
    ],
    [
      "electrum",
      288,
      760,
      [
        { tag: "tconstruct:molten_gold", amount: 144 },
        { tag: "forge:molten_silver", amount: 144 },
      ],
    ],
    [
      "hepatizon",
      288,
      1400,
      [
        { tag: "forge:molten_copper", amount: 288 },
        { tag: "tconstruct:molten_cobalt", amount: 144 },
        { tag: "tconstruct:molten_obsidian", amount: 1000 },
      ],
    ],
    [
      "invar",
      432,
      900,
      [
        { tag: "tconstruct:molten_iron", amount: 288 },
        { tag: "forge:molten_nickel", amount: 144 },
      ],
    ],
    [
      "pewter",
      288,
      400,
      [
        { tag: "tconstruct:molten_iron", amount: 144 },
        { tag: "forge:molten_lead", amount: 144 },
      ],
    ],
    [
      "rose_gold",
      576,
      550,
      [
        { tag: "forge:molten_copper", amount: 432 },
        { tag: "tconstruct:molten_gold", amount: 144 },
      ],
    ],
    [
      "tinkers_bronze",
      432,
      700,
      [
        { tag: "forge:molten_copper", amount: 432 },
        { tag: "tconstruct:molten_glass", amount: 1000 },
      ],
    ],
  ]);

  e.remove({ id: "tconstruct:smeltery/melting/copper/smeltery_controller" });
  e.remove({ id: "tconstruct:smeltery/melting/copper/smeltery_io" });
  tinkerBlockMelting([
    [{ item: "tconstruct:smeltery_controller" }, 576, 175, 1152],
    [
      [
        { item: "tconstruct:seared_drain" },
        { item: "tconstruct:seared_chute" },
      ],
      288,
      125,
      576,
    ],
  ]);

  e.custom({
    type: "tconstruct:casting_basin",
    cast: { item: "tconstruct:seared_heater" },
    cast_consumed: true,
    fluid: { name: "alltheores:molten_copper", amount: 576 },
    result: "tconstruct:smeltery_controller",
    cooling_time: 100,
  });

  // #endregion Tinkers Unification

  // #region Plate Unification
  function unifyPlateSheets(types) {
    types.forEach((type) => {
      let create =
        type == "gold" ? `create:${type}en_sheet` : `create:${type}_sheet`;
      let output =
        type == "aluminum"
          ? `immersiveengineering:plate_${type}`
          : `thermal:${type}_plate`;
      e.replaceInput(`thermal:${type}_plate`, `#forge:plates/${type}`);
      e.replaceInput(
        `immersiveengineering:plate_${type}`,
        `#forge:plates/${type}`
      );
      e.replaceInput(create, `#forge:plates/${type}`);
      e.replaceOutput(`immersiveengineering:plate_${type}`, output);
      e.replaceOutput(create, output);
    });
  }

  unifyPlateSheets([
    "iron",
    "gold",
    "copper",
    "tin",
    "lead",
    "silver",
    "nickel",
    "bronze",
    "electrum",
    "invar",
    "constantan",
    "signalum",
    "lumium",
    "enderium",
    "aluminum",
  ]);
  // #endregion Plate Unification
});
