package com.rooksdk.utils import com.facebook.react.bridge.Arguments import com.facebook.react.bridge.ReadableMap import com.facebook.react.bridge.ReadableType import com.facebook.react.bridge.WritableMap import com.rookmotion.rook.sdk.domain.enums.HCNutritionInsertionEventType import com.rookmotion.rook.sdk.domain.model.HCNutritionInsertionEvent import java.time.ZonedDateTime /** * ReadableToWritable is a utility object that provides methods to convert between * React Native's ReadableMap and WritableMap, and Kotlin's HashMap. * This is useful when you need to transform data between JavaScript and native code. */ object ReadableToWritable { /** * Converts a ReadableMap (from JavaScript/React Native) to a HashMap (native Kotlin data structure). * It iterates through all the keys in the ReadableMap and converts its values based on their type. * * @param readableMap The ReadableMap to convert. * @return A HashMap with equivalent values. */ fun readableMapToHashMap(readableMap: ReadableMap): HashMap { val map = HashMap() val iterator = readableMap.keySetIterator() while (iterator.hasNextKey()) { val key = iterator.nextKey() when (readableMap.getType(key)) { ReadableType.Boolean -> map[key] = readableMap.getBoolean(key) ReadableType.Number -> map[key] = readableMap.getDouble(key) ReadableType.String -> map[key] = readableMap.getString(key) ?: "" ReadableType.Map -> map[key] = readableMapToHashMap(readableMap.getMap(key)!!) ReadableType.Array -> map[key] = readableMap.getArray(key)!!.toArrayList() else -> {} // Manejar otros tipos si es necesario } } return map } /** * Converts a HashMap (native Kotlin data structure) to a WritableMap (to be sent to JavaScript/React Native). * It iterates through all the entries in the HashMap and converts its values based on their type. * * @param hashMap The HashMap to convert. * @return A WritableMap with equivalent values. */ fun hashMapToWritableMap(hashMap: HashMap): WritableMap { val writableMap = Arguments.createMap() for ((key, value) in hashMap) { when (value) { is Boolean -> writableMap.putBoolean(key, value) is Double -> writableMap.putDouble(key, value) is String -> writableMap.putString(key, value) is HashMap<*, *> -> writableMap.putMap(key, hashMapToWritableMap(value as HashMap)) is ArrayList<*> -> writableMap.putArray(key, Arguments.fromArray(value)) } } return writableMap } fun mapReadableMapToHCMealData(map: ReadableMap): HCNutritionInsertionEvent { // Helper for micro-nutrients: range [0.0, 100.0] fun getGramValue(key: String): Double? { return if (map.hasKey(key) && !map.isNull(key)) { map.getDouble(key).takeIf { it in 0.0..100.0 } } else null } // Helper for energy and macronutrients: range [0.0, 100,000.0] fun getLargeValue(key: String): Double? { return if (map.hasKey(key) && !map.isNull(key)) { map.getDouble(key).takeIf { it in 0.0..100000.0 } } else null } val typeString = if (map.hasKey("type")) map.getString("type") else null val mealType = when (typeString?.uppercase()) { "BREAKFAST" -> HCNutritionInsertionEventType.BREAKFAST "LUNCH" -> HCNutritionInsertionEventType.LUNCH "DINNER" -> HCNutritionInsertionEventType.DINNER "SNACK" -> HCNutritionInsertionEventType.SNACK else -> HCNutritionInsertionEventType.SNACK // Default fallback } return HCNutritionInsertionEvent( startDateTime = ZonedDateTime.parse(map.getString("startDateTime")), endDateTime = ZonedDateTime.parse(map.getString("endDateTime")), name = map.getString("name") ?: "Meal", type = mealType, // Energy & Macros (0 to 100,000 range) energyKcal = getLargeValue("energyKcal"), energyFromFatKcal = getLargeValue("energyFromFatKcal"), dietaryFiberGrams = getLargeValue("dietaryFiberGrams"), monounsaturatedFatGrams = getLargeValue("monounsaturatedFatGrams"), polyunsaturatedFatGrams = getLargeValue("polyunsaturatedFatGrams"), proteinGrams = getLargeValue("proteinGrams"), saturatedFatGrams = getLargeValue("saturatedFatGrams"), sugarGrams = getLargeValue("sugarGrams"), totalCarbohydrateGrams = getLargeValue("totalCarbohydrateGrams"), totalFatGrams = getLargeValue("totalFatGrams"), transFatGrams = getLargeValue("transFatGrams"), unsaturatedFatGrams = getLargeValue("unsaturatedFatGrams"), // Micro-nutrients & Minerals (0 to 100 range) biotinGrams = getGramValue("biotinGrams"), caffeineGrams = getGramValue("caffeineGrams"), calciumGrams = getGramValue("calciumGrams"), chlorideGrams = getGramValue("chlorideGrams"), cholesterolGrams = getGramValue("cholesterolGrams"), chromiumGrams = getGramValue("chromiumGrams"), copperGrams = getGramValue("copperGrams"), folateGrams = getGramValue("folateGrams"), folicAcidGrams = getGramValue("folicAcidGrams"), iodineGrams = getGramValue("iodineGrams"), ironGrams = getGramValue("ironGrams"), magnesiumGrams = getGramValue("magnesiumGrams"), manganeseGrams = getGramValue("manganeseGrams"), molybdenumGrams = getGramValue("molybdenumGrams"), niacinGrams = getGramValue("niacinGrams"), pantothenicAcidGrams = getGramValue("pantothenicAcidGrams"), phosphorusGrams = getGramValue("phosphorusGrams"), potassiumGrams = getGramValue("potassiumGrams"), riboflavinGrams = getGramValue("riboflavinGrams"), seleniumGrams = getGramValue("seleniumGrams"), sodiumGrams = getGramValue("sodiumGrams"), thiaminGrams = getGramValue("thiaminGrams"), vitaminAGrams = getGramValue("vitaminAGrams"), vitaminB12Grams = getGramValue("vitaminB12Grams"), vitaminB6Grams = getGramValue("vitaminB6Grams"), vitaminCGrams = getGramValue("vitaminCGrams"), vitaminDGrams = getGramValue("vitaminDGrams"), vitaminEGrams = getGramValue("vitaminEGrams"), vitaminKGrams = getGramValue("vitaminKGrams"), zincGrams = getGramValue("zincGrams") ) } }