import Foundation

#if !TESTING
import CheckoutComponentsSDK
#endif

/// Handles all translation configuration for the Checkout Components SDK
@objc(Translations)
public class Translations: NSObject {
  
  // MARK: - Public Methods
  
  public func extractTranslations(from translations: [String: Any]) -> [String: [CheckoutComponents.TranslationKey: String]] {    
    let processedTranslations = extractLocaleTranslations(from: translations)
    return processedTranslations
  }
  
  // MARK: - Private Methods
  
  private func extractLocaleTranslations(from localeTranslations: [String: Any]) -> [String: [CheckoutComponents.TranslationKey: String]] {
    var result: [String: [CheckoutComponents.TranslationKey: String]] = [:]
    
    for (localeKey, translations) in localeTranslations {
      guard let translationsDict = translations as? [String: Any] else {
        continue
      }
      
      let translationKeys = extractTranslationKeys(from: translationsDict)
      if !translationKeys.isEmpty {
        result[localeKey] = translationKeys
      }
    }
    
    return result
  }
  
  /// Extracts and converts translation keys from JavaScript format to CheckoutComponents.TranslationKey
  private func extractTranslationKeys(from translationsDict: [String: Any]) -> [CheckoutComponents.TranslationKey: String] {
    var result: [CheckoutComponents.TranslationKey: String] = [:]
    
    for (jsKey, value) in translationsDict {
      guard let stringValue = value as? String else {
        continue
      }
      
      // Convert JavaScript camelCase key to CheckoutComponents.TranslationKey
      if let translationKey = mapJSKeyToTranslationKey(jsKey) {
        result[translationKey] = stringValue
      }
    }
    
    return result
  }
  
  /// Maps JavaScript camelCase keys to CheckoutComponents.TranslationKey enum cases
  private func mapJSKeyToTranslationKey(_ jsKey: String) -> CheckoutComponents.TranslationKey? {
    switch jsKey {
    case "addBillingAddress":
      return .addBillingAddress
    case "addAddress":
      return .addAddress
    case "address":
      return .address
    case "addressLine1":
      return .addressLine1
    case "addressLine2":
      return .addressLine2
    case "billingAddress":
      return .billingAddress
    case "card":
      return .card
    case "cardExpiryDate":
      return .cardExpiryDate
    case "cardExpiryDateIncomplete":
      return .cardExpiryDateIncomplete
    case "cardExpiryDateInvalid":
      return .cardExpiryDateInvalid
    case "cardExpiryDatePlaceholderMonth":
      return .cardExpiryDatePlaceholderMonth
    case "cardExpiryDatePlaceholderYear":
      return .cardExpiryDatePlaceholderYear
    case "cardHolderName":
      return .cardHolderName
    case "cardNumber":
      return .cardNumber
    case "cardNumberInvalid":
      return .cardNumberInvalid
    case "cardNumberNotSupported":
      return .cardNumberNotSupported
    case "cardSecurityCode":
      return .cardSecurityCode
    case "cardSecurityCodeInvalid":
      return .cardSecurityCodeInvalid
    case "cardSecurityCodePlaceholder":
      return .cardSecurityCodePlaceholder
    case "city":
      return .city
    case "confirm":
      return .confirm
    case "country":
      return .country
    case "selectCountry":
      return .selectCountry
    case "editAddress":
      return .editAddress
    case "email":
      return .email
    case "emailFormatInvalid":
      return .emailFormatInvalid
    case "firstName":
      return .firstName
    case "formRequired":
      return .formRequired
    case "lastName":
      return .lastName
    case "insufficientCharacters":
      return .insufficientCharacters
    case "noMatchesFound":
      return .noMatchesFound
    case "optional":
      return .optional
    case "payButtonPay":
      return .payButtonPay
    case "payButtonPaymentComplete":
      return .payButtonPaymentComplete
    case "payButtonPaymentProcessing":
      return .payButtonPaymentProcessing
    case "paymentDeclinedInvalidCustomerData":
      return .paymentDeclinedInvalidCustomerData
    case "paymentDeclinedInvalidPaymentSessionData":
      return .paymentDeclinedInvalidPaymentSessionData
    case "paymentDeclinedMerchantMisconfiguration":
      return .paymentDeclinedMerchantMisconfiguration
    case "paymentDeclinedNotEnoughFunds":
      return .paymentDeclinedNotEnoughFunds
    case "paymentDeclinedTryAgain":
      return .paymentDeclinedTryAgain
    case "phoneNumber":
      return .phoneNumber
    case "search":
      return .search
    case "state":
      return .state
    case "trySearchingWithAnotherTerm":
      return .trySearchingWithAnotherTerm
    case "useShippingAsBilling":
      return .useShippingAsBilling
    case "zip":
      return .zip
    case "preferredSchemeCta":
      return .preferredSchemeCta
    case "preferredSchemeDescription":
      return .preferredSchemeDescription
    case "selectState":
      return .selectState
    default:
      return nil
    }
  }
}
