// config/helper/MetadataTemplateProcessor.kt package com.complycube.reactnative.config.helper import com.complycube.reactnative.config.toAnyList import com.complycube.reactnative.config.toStringAnyMap import com.complycube.sdk.common.data.Country import com.complycube.sdk.common.data.Stage import com.complycube.sdk.data.remote.model.response.ComponentFormat import com.complycube.sdk.data.remote.model.response.ComponentType import com.complycube.sdk.data.remote.model.response.Constraint import com.complycube.sdk.data.remote.model.response.QuestionItemComponentFormat import com.complycube.sdk.data.remote.model.response.QuestionOptionsItem import com.complycube.sdk.common.data.CustomerInfoField import com.complycube.sdk.common.data.CustomerInfoPersonField import com.complycube.sdk.common.data.CustomerInfoCompanyField import com.complycube.sdk.common.data.CustomerInfoDetail class MetadataTemplateProcessor { fun processCountryTemplates( filteredCountries: List, countrySelectKey: String, metadataTemplates: List>, customerInfoFields: MutableList ) { filteredCountries.filterNot { it.name == "US" || it.name == "CA" }.forEach { country -> val key = country.name val effectiveKey = if (countrySelectKey.isBlank()) "Tax residences" else countrySelectKey val constraint = "metadata.$effectiveKey contains $key" customerInfoFields.add(injectTemplate(key, constraint, metadataTemplates, "TIN_HAS")) customerInfoFields.add(injectTemplate(key, "metadata.${key}_TIN_HAS contains yes", metadataTemplates, "TIN")) customerInfoFields.add(injectTemplate(key, "metadata.${key}_TIN_HAS contains no", metadataTemplates, "TIN_REASON")) customerInfoFields.add(injectTemplate(key, "metadata.${key}_TIN_REASON contains other", metadataTemplates, "TIN_REASON_OTHER")) } } private fun getTemplateByKey(templates: List>, key: String): Map? { return templates.find { it["templateKey"] == key } } private fun injectTemplate( baseKey: String, constraintExpr: String, templates: List>, templateKey: String ): CustomerInfoField.Metadata { val template = getTemplateByKey(templates, templateKey) ?: throw IllegalArgumentException("Missing template: $templateKey") val question = (template["question"] as String).replace("{country}", baseKey) val description = (template["description"] as? String)?.replace("{country}", baseKey) val componentType = ComponentType.valueOf(template["componentType"] as String) val options = template["options"].toAnyList().mapNotNull { val optionMap = it.toStringAnyMap() val label = optionMap["label"] as? String ?: return@mapNotNull null val value = optionMap["value"] as? String ?: return@mapNotNull null QuestionOptionsItem(label = label, value = value) }.ifEmpty { null } val formatMap = template["format"] as? Map<*, *> val format = formatMap?.let { QuestionItemComponentFormat( type = ComponentFormat.valueOf(it["type"] as String), validation = it["validation"] as String ) } return CustomerInfoField.Metadata( key = "${baseKey}_$templateKey", question = question, componentType = componentType, format = format, options = options, constraint = Constraint(condition = constraintExpr), required = true, description = description ) } }