/**
 * This function takes a nested map containing country data and calculates the total area of all the countries.
 * The function handles errors such as missing or invalid data.
 *
 * @param countriesData A nested map containing country data.
 * @return The total area of all the countries.
 * @throws IllegalArgumentException If the area of a country is missing or zero.
 */
fun calculateTotalArea(countriesData: Map<String, Map<String, Any>>): Double {

    var totalArea = 0.0

    try {
        for ((country, data) in countriesData) {
            if (!data.containsKey("area")) {
                throw IllegalArgumentException("Invalid data: Area missing for $country.")
            }
            if (data["area"] == 0) {
                throw IllegalArgumentException("Invalid data: Zero area for $country.")
            }
            totalArea += (data["area"] as Number).toDouble()
        }
    } catch (e: IllegalArgumentException) {
        return e.message?.toDoubleOrNull() ?: 0.0
    }

    return totalArea
}

val countriesData = mapOf(
    "Canada" to mapOf(
        "area" to 9976140,
        "population" to 37589262,
        "capital" to "Ottawa"
    ),
    "United States" to mapOf(
        "area" to 9629091,
        "population" to 331002651,
        "capital" to "Washington D.C."
    )
)
/**
 * This function takes a nested map containing country data and calculates the total area of all the countries.
 * The function handles errors such as missing or invalid data.
 *
 * @param countriesData A nested map containing country data.
 * @return The total area of all the countries.
 * @throws IllegalArgumentException If the area of a country is missing or zero.
 */
fun calculateTotalArea(countriesData: Map<String, Map<String, Any>>): Double {
/**
 * This function takes a nested map containing country data and calculates the total area of all the countries.
 * The function handles errors such as missing or invalid data.
 *
 * @param countriesData A nested map containing country data.
 * @return The total area of all the countries.
 * @throws IllegalArgumentException If the area of a country is missing or zero.
 */
fun calculateTotalArea(countriesData: Map<String, Map<String, Any>>): Double {

    var totalArea = 0.0

    try {
        for ((country, data) in countriesData) {
            if (!data.containsKey("area")) {
                throw IllegalArgumentException("Invalid data: Area missing for $country.")
            }
            if (data["area"] == 0) {
                throw IllegalArgumentException("Invalid data: Zero area for $country.")
            }
            totalArea += (data["area"] as Number).toDouble()
        }
    } catch (e: IllegalArgumentException) {
        return e.message?.toDoubleOrNull() ?: 0.0
    }

    return totalArea
}

val countriesData = mapOf(
    "Canada" to mapOf(
        "area" to 9976140,
        "population" to 37589262,
        "capital" to "Ottawa"
    ),
    "United States" to mapOf(
        "area" to 9629091,
        "population" to 331002651,
        "capital" to "Washington D.C."
    )
)