package com.checkoutreactnativecomponents.utils import com.checkout.components.interfaces.localisation.ComponentTranslationKey import com.checkout.components.interfaces.localisation.Locale import com.checkout.components.interfaces.localisation.Translations import com.facebook.react.bridge.ReadableMap public object TranslationsBuilder { public fun fromReadableMap(translationsMap: ReadableMap?): Translations? { if (translationsMap == null) { return null } val result = mutableMapOf>() val iterator = translationsMap.keySetIterator() while (iterator.hasNextKey()) { val localeString = iterator.nextKey() val localeTranslations = translationsMap.getMap(localeString) if (localeTranslations != null) { val locale = LocaleBuilder.fromString(localeString) if (locale != null) { val translationMap = buildTranslationMap(localeTranslations) if (translationMap.isNotEmpty()) { result[locale] = translationMap } } } } return result.ifEmpty { null } } private fun buildTranslationMap(localeTranslations: ReadableMap): Map { val result = mutableMapOf() val iterator = localeTranslations.keySetIterator() while (iterator.hasNextKey()) { val jsKey = iterator.nextKey() val translationValue = localeTranslations.getString(jsKey) if (!translationValue.isNullOrEmpty()) { val translationKey = mapJSKeyToComponentTranslationKey(jsKey) if (translationKey != null) { result[translationKey] = translationValue } } } return result } private fun mapJSKeyToComponentTranslationKey(jsKey: String): ComponentTranslationKey? { return when (jsKey) { // Address-related translations "addressLine1" -> ComponentTranslationKey.AddressAddressLine1 "addressLine2" -> ComponentTranslationKey.AddressAddressLine2 "addBillingAddress" -> ComponentTranslationKey.AddressBillingAdd "city" -> ComponentTranslationKey.AddressCity "country" -> ComponentTranslationKey.AddressCountry "selectCountry" -> ComponentTranslationKey.AddressCountrySelect "editAddress" -> ComponentTranslationKey.AddressEditAddress // Select State missing: "selectState" => ComponentTranslationKey.AddressSelectState "state" -> ComponentTranslationKey.AddressState "zip" -> ComponentTranslationKey.AddressZip // Card-related translations "card" -> ComponentTranslationKey.Card "useShippingAsBilling" -> ComponentTranslationKey.CardAddressUseShippingAsBilling "cardHolderName" -> ComponentTranslationKey.CardHolderName "cardNumber" -> ComponentTranslationKey.CardNumber "cardNumberInvalid" -> ComponentTranslationKey.CardNumberInvalid "cardNumberNotSupported" -> ComponentTranslationKey.CardNumberNotSupported "cardExpiryDate" -> ComponentTranslationKey.CardExpiryDate "cardExpiryDateInvalid" -> ComponentTranslationKey.CardExpiryDateInvalid "cardExpiryDateIncomplete" -> ComponentTranslationKey.CardExpiryDateIncomplete "cardExpiryDatePlaceholderMonth" -> ComponentTranslationKey.CardExpiryDatePlaceholderMonth "cardExpiryDatePlaceholderYear" -> ComponentTranslationKey.CardExpiryDatePlaceholderYear "cardSecurityCode" -> ComponentTranslationKey.CardSecurityCode "cardSecurityCodeInvalid" -> ComponentTranslationKey.CardSecurityCodeInvalid "cardSecurityCodePlaceholder" -> ComponentTranslationKey.CardSecurityCodePlaceholder // Form-related translations "formRequired" -> ComponentTranslationKey.FormRequired "addAddress" -> ComponentTranslationKey.FormAddAddress "address" -> ComponentTranslationKey.FormAddress "billingAddress" -> ComponentTranslationKey.FormBillingAddress "email" -> ComponentTranslationKey.FormEmail "emailFormatInvalid" -> ComponentTranslationKey.FormEmailFormatInvalid "firstName" -> ComponentTranslationKey.FormFirstName "lastName" -> ComponentTranslationKey.FormLastName "insufficientCharacters" -> ComponentTranslationKey.FormInsufficientCharacters "noMatchesFound" -> ComponentTranslationKey.FormNoMatchesFound "optional" -> ComponentTranslationKey.FormOptional "phoneNumber" -> ComponentTranslationKey.FormPhoneNumber "search" -> ComponentTranslationKey.FormSearch "trySearchingWithAnotherTerm" -> ComponentTranslationKey.FormTrySearchingWithAnotherTerm // Pay button-related translations "payButtonPay" -> ComponentTranslationKey.PayButtonPay "confirm" -> ComponentTranslationKey.PayButtonConfirm "payButtonPaymentComplete" -> ComponentTranslationKey.PayButtonPaymentComplete "payButtonPaymentProcessing" -> ComponentTranslationKey.PayButtonPaymentProcessing // Payment declined-related translations "paymentDeclinedInvalidCustomerData" -> ComponentTranslationKey.PaymentDeclinedInvalidCustomerData "paymentDeclinedInvalidPaymentSessionData" -> ComponentTranslationKey.PaymentDeclinedInvalidPaymentSessionData "paymentDeclinedMerchantMisconfiguration" -> ComponentTranslationKey.PaymentDeclinedMerchantMisconfiguration "paymentDeclinedNotEnoughFunds" -> ComponentTranslationKey.PaymentDeclinedNotEnoughFunds "paymentDeclinedTryAgain" -> ComponentTranslationKey.PaymentDeclinedTryAgain // Preferred scheme-related translations "preferredSchemeCta" -> ComponentTranslationKey.PreferredSchemeCta "preferredSchemeDescription" -> ComponentTranslationKey.PreferredSchemeDescription // Unknown key else -> { null } } } }