import type { ProductItem } from './ProductItem'; import { productItemArrayFromJson } from './ProductItem'; import type { IntentStateDetails } from './IntentStateDetails'; import { intentStateDetailsFromJson } from './IntentStateDetails'; import { IntentData, intentDataFromJson } from './IntentData'; import type { TransactionData } from './TransactionData'; import { transactionDataFromJson } from './TransactionData'; import { LastUsedMethod } from './LastUsedMethod'; import { lastUsedMethodFromJson } from './LastUsedMethod'; import type { PaymentStatus } from './PaymentStatus'; import { paymentStatusFromJson } from './PaymentStatus'; import { subscriptionInfoFromJson, type SubscriptionInfo, } from './subscriptions/SubscriptionInfo'; export class IntentDetails { selectedMethod?: string; intent?: IntentData; walletBalance?: number; transaction?: TransactionData; intentState?: IntentStateDetails; productItems?: ProductItem[]; lastUsedMethod?: LastUsedMethod; paymentStatus?: PaymentStatus; subscriptionInfo?: SubscriptionInfo; constructor(params: { selectedMethod?: string; intent?: IntentData; walletBalance?: number; transaction?: TransactionData; intentState?: IntentStateDetails; productItems?: ProductItem[]; lastUsedMethod?: LastUsedMethod; paymentStatus?: PaymentStatus; subscriptionInfo?: SubscriptionInfo; }) { this.selectedMethod = params.selectedMethod; this.intent = params.intent; this.walletBalance = params.walletBalance; this.transaction = params.transaction; this.intentState = params.intentState; this.productItems = params.productItems; this.lastUsedMethod = params.lastUsedMethod; this.paymentStatus = params.paymentStatus; this.subscriptionInfo = params.subscriptionInfo; } } export function intentDetailsFromJson(jsonMap: { [key: string]: any; }): IntentDetails { const rawState = jsonMap.state; // Handle `state` being a stringified JSON (iOS case because iOS gets it from KMM as string and pass it throu to flutter) let parsedState; // Check if the raw state is a string that needs to be parsed if (typeof rawState === 'string') { try { // Attempt to parse the JSON string parsedState = JSON.parse(rawState); } catch (e) { // Log error if parsing fails and set to undefined console.error('Failed to parse state string:', rawState, e); parsedState = undefined; } } else if (typeof rawState === 'object') { // If it's already an object, use it directly parsedState = rawState; } return new IntentDetails({ selectedMethod: jsonMap.selected_method || jsonMap.selectedMethod, intent: jsonMap.intent ? intentDataFromJson(jsonMap.intent) : undefined, walletBalance: (jsonMap.walletBalance || jsonMap.wallet)?.toFixed(2), subscriptionInfo: jsonMap.subscription_info || jsonMap.subscriptionInfo ? subscriptionInfoFromJson( jsonMap.subscription_info || jsonMap.subscriptionInfo ) : undefined, transaction: jsonMap.transaction ? transactionDataFromJson(jsonMap.transaction) : undefined, productItems: Array.isArray(jsonMap.productItems) ? productItemArrayFromJson(jsonMap.productItems) : undefined, intentState: parsedState ? intentStateDetailsFromJson(parsedState) : undefined, lastUsedMethod: jsonMap.last_used_method || jsonMap.lastUsedMethod ? lastUsedMethodFromJson( jsonMap.last_used_method || jsonMap.lastUsedMethod ) : undefined, paymentStatus: jsonMap.payment_status || jsonMap.paymentStatus ? paymentStatusFromJson(jsonMap.payment_status || jsonMap.paymentStatus) : undefined, }); }