import type { IFormMetadataField } from '../types'; export interface IParsedField extends IFormMetadataField { path: string[]; isNested: boolean; inputType: string; } export function parseFieldCode(code: string): string[] { return code.split('.'); } export function getInputType(code: string): string { const lowerCode = code.toLowerCase(); if (lowerCode.includes('email')) return 'email'; if (lowerCode.includes('phone')) return 'tel'; if (lowerCode.includes('birthdate') || lowerCode.includes('date')) return 'date'; if (lowerCode.includes('amount')) return 'number'; if (lowerCode.includes('zipcode') || lowerCode.includes('zip')) return 'text'; if (lowerCode === 'type') return 'radio'; if (lowerCode === 'method') return 'select'; if (lowerCode === 'currency') return 'select'; return 'text'; } export function parseFormFields(fields: IFormMetadataField[]): IParsedField[] { return fields ?.map(field => ({ ...field, path: parseFieldCode(field.code), isNested: field.code.includes('.'), inputType: getInputType(field.code), })) .sort((a, b) => a.sort - b.sort); } export function shouldShowField( field: IParsedField, config: { paymentType?: 'deposit' | 'withdrawal'; currency?: string } ): boolean { const code = field.code.toLowerCase(); // Always show type if not in config if (code === 'type' && !config.paymentType) return true; if (code === 'type' && config.paymentType) return false; // Always show method (but we might not render it as it's already selected) if (code === 'method') return false; // Method is already selected, don't show in form // Show currency if not in config if (code === 'currency' && !config.currency) return true; if (code === 'currency' && config.currency) return false; // Show all other fields return true; } export function buildNestedObject(path: string[], value: unknown): Record { const result: Record = {}; let current = result; for (let i = 0; i < path.length - 1; i++) { current[path[i]] = {}; current = current[path[i]] as Record; } current[path[path.length - 1]] = value; return result; } export function mergeNestedObjects( obj1: Record, obj2: Record ): Record { const result = { ...obj1 }; for (const key in obj2) { if (typeof obj2[key] === 'object' && obj2[key] !== null && !Array.isArray(obj2[key])) { result[key] = mergeNestedObjects( (result[key] as Record) || {}, obj2[key] as Record ); } else { result[key] = obj2[key]; } } return result; } export function groupFieldsBySection(fields: IParsedField[]): { payment: IParsedField[]; customer: IParsedField[]; account: IParsedField[]; } { const payment: IParsedField[] = []; const customer: IParsedField[] = []; const account: IParsedField[] = []; fields?.forEach(field => { if (field.path[0] === 'customer' && field.path[1] === 'account') { account.push(field); } else if (field.path[0] === 'customer') { customer.push(field); } else { payment.push(field); } }); return { payment, customer, account }; }