// Visit the wiki for more info - https://kubejs.com/
console.info('Hello, World! (Loaded startup example script)')
console.log("Startup script LOADED");

const tfcMetals = [
    "bismuth",
    "bismuth_bronze",
    "black_bronze",
    "black_steel",
    "blue_steel",
    "bronze",
    "brass",
    "cast_iron",
    "copper",
    "gold",
    "nickel",
    "red_steel",
    "rose_gold",
    "silver",
    "steel",
    "sterling_silver",
    "tin",
    "wrought_iron",
    "zinc",
    "chromium",
    "stainless_steel"
];

function metalMod(metal) {
    return (metal === "chromium" || metal === "stainless_steel")
        ? "firmalife"
        : "tfc";
}

StartupEvents.registry("item", event => {
    tfcMetals.forEach(metal => {
        //Transitional item for Create forging 
        event.create(`incomplete_sheet_${metal}`)
            .texture(`${metalMod(metal)}:item/metal/double_ingot/${metal}`)
        //Transitional item for ingot welding
        event.create(`incomplete_weld_ingot_${metal}`)
            .texture(`${metalMod(metal)}:item/metal/ingot/${metal}`)
        //Transitional item for sheet welding
        event.create(`incomplete_weld_sheet_${metal}`)
            .texture(`${metalMod(metal)}:item/metal/sheet/${metal}`)
        });
    hammerMetals.forEach(metal => {
        //Transitional item for Metal Boots
        event.create(`incomplete_weld_armor_boots_${metal}`)
            .texture(`tfc:item/metal/unfinished_boots/${metal}`)
            .maxStackSize(1);
        //Transitional item for Metal Chest
        event.create(`incomplete_weld_armor_chestplate_${metal}`)
            .texture(`tfc:item/metal/unfinished_chestplate/${metal}`)
            .maxStackSize(1);
        //Transitional item for Metal Legs
        event.create(`incomplete_weld_armor_greaves_${metal}`)
            .texture(`tfc:item/metal/unfinished_greaves/${metal}`)
            .maxStackSize(1);
        //Transitional item for Metal Helmet
        event.create(`incomplete_weld_armor_helmet_${metal}`)
            .texture(`tfc:item/metal/unfinished_helmet/${metal}`)
            .maxStackSize(1);
        });
    //Transitional item for outlire items
    event.create("incomplete_basic_burner")
        .texture("tfc:item/metal/double_sheet/cast_iron")
        .maxStackSize(1);

});


// Hammer tiers (lowest → highest)
const hammerMetals = [
    "copper",
    "bronze",
    "bismuth_bronze",
    "black_bronze",
    "wrought_iron",
    "steel",
    "black_steel",
    "blue_steel",
    "red_steel"
];
const metalTiers = [
    ["copper", "bismuth", "tin", "zinc", "nickel", "rose_gold", "sterling_silver", "gold", "silver","chromium"],
    ["bronze", "bismuth_bronze", "black_bronze", "brass"],
    ["wrought_iron", "cast_iron"],
    ["steel","stainless_steel"],
    ["black_steel"],
    ["blue_steel", "red_steel"]
];



// Build metal → tier index (GLOBAL so server_scripts can use it)
global.metalTier = {};

metalTiers.forEach((tierList, index) => {
    tierList.forEach(metal => {
        global.metalTier[metal] = index;
    });
});


// Build tier → allowed hammers
const hammerByTier = {};
metalTiers.forEach((tierList, index) => {
    const allowed = [];
    for (let i = index; i < metalTiers.length; i++) {
        metalTiers[i].forEach(metal => {
            if (hammerMetals.includes(metal)) {
                allowed.push(`tfc:metal/hammer/${metal}`);
            }
        });
    }
    hammerByTier[index] = allowed;
});


// Build tier → allowed tongs
const tongsByTier = {};
metalTiers.forEach((tierList, index) => {
    const allowed = [];
    for (let i = index; i < metalTiers.length; i++) {
        metalTiers[i].forEach(metal => {
            allowed.push(`tfchotornot:tongs/${metal}`);
        });
    }
    tongsByTier[index] = allowed;
});

global.hammerByTier = hammerByTier;
global.tongsByTier = tongsByTier;
global.tfcMetals = tfcMetals;
