import os
import json

# Define the mapping of items to tags
item_to_tag = {
    "minecraft:copper_ingot": "forge:ingots/copper",
    "minecraft:gold_ingot": "forge:ingots/gold",
    "minecraft:iron_ingot": "forge:ingots/iron"
}

# Iterate over all files in the current directory
for filename in os.listdir():
    # Process only JSON files ending with "_printing.json"
    if filename.endswith("_printing.json"):
        try:
            # Read the JSON file
            with open(filename, "r", encoding="utf-8") as file:
                data = json.load(file)

            # Function to update only relevant "item" keys
            def update_items(obj):
                if isinstance(obj, dict):
                    return {("tag" if key == "item" and value in item_to_tag else key): item_to_tag.get(value, value) if key == "item" else update_items(value) for key, value in obj.items()}
                elif isinstance(obj, list):
                    return [update_items(item) for item in obj]
                else:
                    return obj

            updated_data = update_items(data)

            # Write the changes back to the file
            with open(filename, "w", encoding="utf-8") as file:
                json.dump(updated_data, file, indent=4)

            print(f"Updated {filename}")

        except Exception as e:
            print(f"Error processing {filename}: {e}")
