//priority: 30

const furnaceTypes = [ "minecraft:smelting", "minecraft:blasting" ];
const castType = [ "multi_use", "single_use" ];
const metals = [
  {
    "material": "copper",
    "types": {
      "nugget": {
        "fluid_amount": 16,
        "cooling_time": 17
      },
      "ingot": {
        "fluid_amount": 144,
        "cooling_time": 50
      },
      "block": {
        "fluid_amount": 1296,
        "cooling_time": 150
      }
    },
    "xp": 0.7
  },
  {
    "material": "silver",
    "types": {
      "nugget": {
        "fluid_amount": 16,
        "cooling_time": 20
      },
      "ingot": {
        "fluid_amount": 144,
        "cooling_time": 60
      },
      "block": {
        "fluid_amount": 1296,
        "cooling_time": 179
      }
    },
    "xp": 0.7
  },
  {
    "material": "lead",
    "types": {
      "nugget": {
        "fluid_amount": 16,
        "cooling_time": 14
      },
      "ingot": {
        "fluid_amount": 144,
        "cooling_time": 43
      },
      "block": {
        "fluid_amount": 1296,
        "cooling_time": 130
      }
    },
    "xp": 0.7
  }
];

onEvent("recipes", recipeEvent => {
  const unifyMetal = entries => {
    const { material, types, xp } = entries;
    for (let type in types) {
      recipeEvent.replaceOutput(`#forge:${type === "block" ? "storage_blocks" : `${type}s`}/${material}`, `thermal:${material}_${type}`);
      castType.forEach(castType => {
        recipeEvent.remove({
          id: `tconstruct:smeltery/casting/metal/${material}/${type === "block" ? type : `${type}_${castType === "multi_use" ? "gold" : "sand" }_cast`}`
        });
        if (type !== "block") {
          recipeEvent.custom({
            "type": "tconstruct:casting_table",
            "conditions": [
              {
                "value": {
                  "tag": `forge:${type}/${material}`,
                  "type": "forge:tag_empty"
                },
                "type": "forge:not"
              }
            ],
            "cast": {
              "tag": `tconstruct:casts/${castType}/${type}`
            },
            "fluid": {
              "name": `tconstruct:molten_${material}`,
              "amount": types[type].fluid_amount
            },
            "result": {
              "item": `thermal:${material}_${type}`
            },
            "cooling_time": types[type].cooling_time
          });
        } else {
          recipeEvent.custom({
            "type": "tconstruct:casting_basin",
            "conditions": [
              {
                "value": {
                  "tag": `forge:storage_blocks/${material}`,
                  "type": "forge:tag_empty"
                },
                "type": "forge:not"
              }
            ],
            "fluid": {
              "name": `tconstruct:molten_${material}`,
              "amount": types[type].fluid_amount
            },
            "result": {
              "item": `thermal:${material}_${type}`
            },
            "cooling_time": types[type].cooling_time
          });
        }
      });
    };

    furnaceTypes.forEach(type => {
      recipeEvent.remove({
        input: `#forge:ores/${material}`,
        output: `#forge:ingots/${material}`,
        type: type
      });
    });

    recipeEvent.recipes.minecraft.smelting(`thermal:${material}_ingot`, `#forge:ores/${material}`).xp(xp);
    recipeEvent.recipes.minecraft.blasting(`thermal:${material}_ingot`, `#forge:ores/${material}`).xp(xp);
  };

  metals.forEach(metal => unifyMetal(metal));
});