// CUNICULUM smithing bridge.
// Keeps vanilla and RPG-Series equipment upgrade paths consistent after recipe cleanup.

function cuniculumSmithingId(path) {
  return String(path).replace(/[^a-z0-9_\/.-]/gi, '_').toLowerCase();
}

function cuniculumAddNetheriteUpgrade(event, base, result) {
  event.custom({
    type: 'minecraft:smithing_transform',
    template: { item: 'minecraft:netherite_upgrade_smithing_template' },
    base: { item: base },
    addition: { item: 'minecraft:netherite_ingot' },
    result: { id: result, count: 1 }
  }).id('cuniculum:smithing/netherite/' + cuniculumSmithingId(result.replace(':', '/')));
}

function cuniculumAddSizedUpgrade(event, template, additionTag, additionCount, base, result) {
  event.custom({
    type: 'apotheosis:sized_upgrade_recipe',
    template: { item: template },
    base: { item: base },
    addition: { tag: additionTag, count: additionCount },
    result: { id: result, count: 1 }
  }).id('cuniculum:smithing/sized/' + cuniculumSmithingId(result.replace(':', '/')));
}

const CUNICULUM_NETHERITE_UPGRADES = [
  ['minecraft:diamond_sword', 'minecraft:netherite_sword'],
  ['minecraft:diamond_axe', 'minecraft:netherite_axe'],
  ['minecraft:diamond_pickaxe', 'minecraft:netherite_pickaxe'],
  ['minecraft:diamond_shovel', 'minecraft:netherite_shovel'],
  ['minecraft:diamond_hoe', 'minecraft:netherite_hoe'],
  ['minecraft:diamond_helmet', 'minecraft:netherite_helmet'],
  ['minecraft:diamond_chestplate', 'minecraft:netherite_chestplate'],
  ['minecraft:diamond_leggings', 'minecraft:netherite_leggings'],
  ['minecraft:diamond_boots', 'minecraft:netherite_boots'],
  ['farmersdelight:diamond_knife', 'farmersdelight:netherite_knife'],

  ['archers:heavy_crossbow', 'archers:netherite_heavy_crossbow'],
  ['archers:royal_longbow', 'archers:netherite_longbow'],
  ['archers:rapid_crossbow', 'archers:netherite_rapid_crossbow'],
  ['archers:mechanic_shortbow', 'archers:netherite_shortbow'],
  ['archers:diamond_spear', 'archers:netherite_spear'],
  ['archers:ranger_armor_head', 'archers:netherite_ranger_armor_head'],
  ['archers:ranger_armor_chest', 'archers:netherite_ranger_armor_chest'],
  ['archers:ranger_armor_legs', 'archers:netherite_ranger_armor_legs'],
  ['archers:ranger_armor_feet', 'archers:netherite_ranger_armor_feet'],
  ['archers_expansion:deadeye_head', 'archers_expansion:netherite_deadeye_head'],
  ['archers_expansion:deadeye_chest', 'archers_expansion:netherite_deadeye_chest'],
  ['archers_expansion:deadeye_legs', 'archers_expansion:netherite_deadeye_legs'],
  ['archers_expansion:deadeye_feet', 'archers_expansion:netherite_deadeye_feet'],
  ['archers_expansion:tundra_hunter_head', 'archers_expansion:netherite_tundra_hunter_head'],
  ['archers_expansion:tundra_hunter_chest', 'archers_expansion:netherite_tundra_hunter_chest'],
  ['archers_expansion:tundra_hunter_legs', 'archers_expansion:netherite_tundra_hunter_legs'],
  ['archers_expansion:tundra_hunter_feet', 'archers_expansion:netherite_tundra_hunter_feet'],
  ['archers_expansion:war_archer_head', 'archers_expansion:netherite_war_archer_head'],
  ['archers_expansion:war_archer_chest', 'archers_expansion:netherite_war_archer_chest'],
  ['archers_expansion:war_archer_legs', 'archers_expansion:netherite_war_archer_legs'],
  ['archers_expansion:war_archer_feet', 'archers_expansion:netherite_war_archer_feet'],

  ['bards_rpg:harp_crossbow', 'bards_rpg:netherite_harp_crossbow'],
  ['bards_rpg:diamond_lute', 'bards_rpg:netherite_lute'],
  ['bards_rpg:diamond_lyre', 'bards_rpg:netherite_lyre'],
  ['bards_rpg:diamond_rapier', 'bards_rpg:netherite_rapier'],
  ['bards_rpg:troubadour_garb_head', 'bards_rpg:netherite_troubadour_garb_head'],
  ['bards_rpg:troubadour_garb_chest', 'bards_rpg:netherite_troubadour_garb_chest'],
  ['bards_rpg:troubadour_garb_legs', 'bards_rpg:netherite_troubadour_garb_legs'],
  ['bards_rpg:troubadour_garb_feet', 'bards_rpg:netherite_troubadour_garb_feet'],

  ['berserker_rpg:diamond_berserker_axe', 'berserker_rpg:netherite_berserker_axe'],
  ['berserker_rpg:northling_head', 'berserker_rpg:netherite_northling_head'],
  ['berserker_rpg:northling_chest', 'berserker_rpg:netherite_northling_chest'],
  ['berserker_rpg:northling_legs', 'berserker_rpg:netherite_northling_legs'],
  ['berserker_rpg:northling_feet', 'berserker_rpg:netherite_northling_feet'],

  ['elemental_wizards_rpg:dripstone_head', 'elemental_wizards_rpg:netherite_dripstone_head'],
  ['elemental_wizards_rpg:dripstone_chest', 'elemental_wizards_rpg:netherite_dripstone_chest'],
  ['elemental_wizards_rpg:dripstone_legs', 'elemental_wizards_rpg:netherite_dripstone_legs'],
  ['elemental_wizards_rpg:dripstone_feet', 'elemental_wizards_rpg:netherite_dripstone_feet'],
  ['elemental_wizards_rpg:kelp_head', 'elemental_wizards_rpg:netherite_kelp_head'],
  ['elemental_wizards_rpg:kelp_chest', 'elemental_wizards_rpg:netherite_kelp_chest'],
  ['elemental_wizards_rpg:kelp_legs', 'elemental_wizards_rpg:netherite_kelp_legs'],
  ['elemental_wizards_rpg:kelp_feet', 'elemental_wizards_rpg:netherite_kelp_feet'],
  ['elemental_wizards_rpg:wind_head', 'elemental_wizards_rpg:netherite_wind_head'],
  ['elemental_wizards_rpg:wind_chest', 'elemental_wizards_rpg:netherite_wind_chest'],
  ['elemental_wizards_rpg:wind_legs', 'elemental_wizards_rpg:netherite_wind_legs'],
  ['elemental_wizards_rpg:wind_feet', 'elemental_wizards_rpg:netherite_wind_feet'],
  ['elemental_wizards_rpg:staff_aqua', 'elemental_wizards_rpg:staff_netherite_aqua'],
  ['elemental_wizards_rpg:wand_aqua', 'elemental_wizards_rpg:wand_netherite_aqua'],
  ['elemental_wizards_rpg:staff_terra', 'elemental_wizards_rpg:staff_netherite_terra'],
  ['elemental_wizards_rpg:wand_terra', 'elemental_wizards_rpg:wand_netherite_terra'],
  ['elemental_wizards_rpg:staff_wind', 'elemental_wizards_rpg:staff_netherite_wind'],
  ['elemental_wizards_rpg:wand_wind', 'elemental_wizards_rpg:wand_netherite_wind'],

  ['forcemaster_rpg:diamond_knuckle', 'forcemaster_rpg:netherite_knuckle'],
  ['forcemaster_rpg:phasleb_head', 'forcemaster_rpg:aken_head'],
  ['forcemaster_rpg:phasleb_chest', 'forcemaster_rpg:aken_chest'],
  ['forcemaster_rpg:phasleb_legs', 'forcemaster_rpg:aken_legs'],
  ['forcemaster_rpg:phasleb_feet', 'forcemaster_rpg:aken_feet'],

  ['paladins:diamond_claymore', 'paladins:netherite_claymore'],
  ['paladins:diamond_great_hammer', 'paladins:netherite_great_hammer'],
  ['paladins:diamond_holy_staff', 'paladins:netherite_holy_staff'],
  ['paladins:diamond_holy_wand', 'paladins:netherite_holy_wand'],
  ['paladins:diamond_kite_shield', 'paladins:netherite_kite_shield'],
  ['paladins:diamond_mace', 'paladins:netherite_mace'],
  ['paladins:crusader_armor_head', 'paladins:netherite_crusader_armor_head'],
  ['paladins:crusader_armor_chest', 'paladins:netherite_crusader_armor_chest'],
  ['paladins:crusader_armor_legs', 'paladins:netherite_crusader_armor_legs'],
  ['paladins:crusader_armor_feet', 'paladins:netherite_crusader_armor_feet'],
  ['paladins:prior_robe_head', 'paladins:netherite_prior_robe_head'],
  ['paladins:prior_robe_chest', 'paladins:netherite_prior_robe_chest'],
  ['paladins:prior_robe_legs', 'paladins:netherite_prior_robe_legs'],
  ['paladins:prior_robe_feet', 'paladins:netherite_prior_robe_feet'],

  ['rogues:diamond_dagger', 'rogues:netherite_dagger'],
  ['rogues:diamond_double_axe', 'rogues:netherite_double_axe'],
  ['rogues:diamond_glaive', 'rogues:netherite_glaive'],
  ['rogues:diamond_sickle', 'rogues:netherite_sickle'],
  ['rogues:assassin_armor_head', 'rogues:netherite_assassin_armor_head'],
  ['rogues:assassin_armor_chest', 'rogues:netherite_assassin_armor_chest'],
  ['rogues:assassin_armor_legs', 'rogues:netherite_assassin_armor_legs'],
  ['rogues:assassin_armor_feet', 'rogues:netherite_assassin_armor_feet'],
  ['rogues:berserker_armor_head', 'rogues:netherite_berserker_armor_head'],
  ['rogues:berserker_armor_chest', 'rogues:netherite_berserker_armor_chest'],
  ['rogues:berserker_armor_legs', 'rogues:netherite_berserker_armor_legs'],
  ['rogues:berserker_armor_feet', 'rogues:netherite_berserker_armor_feet'],

  ['witcher_rpg:diamond_witcher_sword', 'witcher_rpg:netherite_witcher_sword'],

  ['wizards:arcane_robe_head', 'wizards:netherite_arcane_robe_head'],
  ['wizards:arcane_robe_chest', 'wizards:netherite_arcane_robe_chest'],
  ['wizards:arcane_robe_legs', 'wizards:netherite_arcane_robe_legs'],
  ['wizards:arcane_robe_feet', 'wizards:netherite_arcane_robe_feet'],
  ['wizards:fire_robe_head', 'wizards:netherite_fire_robe_head'],
  ['wizards:fire_robe_chest', 'wizards:netherite_fire_robe_chest'],
  ['wizards:fire_robe_legs', 'wizards:netherite_fire_robe_legs'],
  ['wizards:fire_robe_feet', 'wizards:netherite_fire_robe_feet'],
  ['wizards:frost_robe_head', 'wizards:netherite_frost_robe_head'],
  ['wizards:frost_robe_chest', 'wizards:netherite_frost_robe_chest'],
  ['wizards:frost_robe_legs', 'wizards:netherite_frost_robe_legs'],
  ['wizards:frost_robe_feet', 'wizards:netherite_frost_robe_feet'],
  ['wizards:staff_arcane', 'wizards:staff_netherite_arcane'],
  ['wizards:staff_fire', 'wizards:staff_netherite_fire'],
  ['wizards:staff_frost', 'wizards:staff_netherite_frost'],
  ['wizards:wand_arcane', 'wizards:wand_netherite_arcane'],
  ['wizards:wand_fire', 'wizards:wand_netherite_fire'],
  ['wizards:wand_frost', 'wizards:wand_netherite_frost']
];

const CUNICULUM_IRON_TO_GOLD_UPGRADES = [
  ['archers:iron_spear', 'archers:golden_spear'],
  ['bards_rpg:iron_rapier', 'bards_rpg:golden_rapier'],
  ['berserker_rpg:iron_berserker_axe', 'berserker_rpg:golden_berserker_axe'],
  ['farmersdelight:iron_knife', 'farmersdelight:golden_knife'],
  ['forcemaster_rpg:iron_knuckle', 'forcemaster_rpg:golden_knuckle'],
  ['paladins:iron_claymore', 'paladins:golden_claymore'],
  ['paladins:iron_great_hammer', 'paladins:golden_great_hammer'],
  ['paladins:iron_kite_shield', 'paladins:golden_kite_shield'],
  ['paladins:iron_mace', 'paladins:golden_mace'],
  ['rogues:iron_dagger', 'rogues:golden_dagger'],
  ['rogues:iron_double_axe', 'rogues:golden_double_axe'],
  ['rogues:iron_glaive', 'rogues:golden_glaive'],
  ['rogues:iron_sickle', 'rogues:golden_sickle'],
  ['witcher_rpg:iron_witcher_sword', 'witcher_rpg:golden_witcher_sword']
];

const CUNICULUM_GOLD_TO_DIAMOND_UPGRADES = [
  ['archers:golden_spear', 'archers:diamond_spear'],
  ['bards_rpg:golden_lyre', 'bards_rpg:diamond_lyre'],
  ['bards_rpg:golden_rapier', 'bards_rpg:diamond_rapier'],
  ['berserker_rpg:golden_berserker_axe', 'berserker_rpg:diamond_berserker_axe'],
  ['farmersdelight:golden_knife', 'farmersdelight:diamond_knife'],
  ['forcemaster_rpg:golden_knuckle', 'forcemaster_rpg:diamond_knuckle'],
  ['paladins:golden_claymore', 'paladins:diamond_claymore'],
  ['paladins:golden_great_hammer', 'paladins:diamond_great_hammer'],
  ['paladins:golden_kite_shield', 'paladins:diamond_kite_shield'],
  ['paladins:golden_mace', 'paladins:diamond_mace'],
  ['rogues:golden_dagger', 'rogues:diamond_dagger'],
  ['rogues:golden_double_axe', 'rogues:diamond_double_axe'],
  ['rogues:golden_glaive', 'rogues:diamond_glaive'],
  ['rogues:golden_sickle', 'rogues:diamond_sickle'],
  ['witcher_rpg:golden_witcher_sword', 'witcher_rpg:diamond_witcher_sword']
];

ServerEvents.recipes(event => {
  CUNICULUM_NETHERITE_UPGRADES.forEach(pair => {
    cuniculumAddNetheriteUpgrade(event, pair[0], pair[1]);
  });

  CUNICULUM_IRON_TO_GOLD_UPGRADES.forEach(pair => {
    cuniculumAddSizedUpgrade(event, 'apotheosis:gold_upgrade_smithing_template', 'c:ingots/gold', 4, pair[0], pair[1]);
  });

  CUNICULUM_GOLD_TO_DIAMOND_UPGRADES.forEach(pair => {
    cuniculumAddSizedUpgrade(event, 'apotheosis:diamond_upgrade_smithing_template', 'c:gems/diamond', 4, pair[0], pair[1]);
  });
});
