import { IntlShape } from 'react-intl' ; import { IFunctionTransformer , TemplateTransformerData , TransformerPayload , IOfflineAddressPayload , IOfflineAddressCondition } from '@client/pdfRenderer/transformer/types' ; import { getValueFromApplicationDataByKey , getMatchedCondition } from '@client/pdfRenderer/transformer/utils' ; import { IAvailableCountries , ICountry } from '@client/views/PrintCertificate/utils' ; import { isDefaultCountry } from '@client/forms/utils' ; type KeyValues = { [ key ] : string ; } ; const getKeyValues = ( keys , templateData ) => keys && keys . reduce ( ( keyValues , key ) => { keyValues [ key ] = getValueFromApplicationDataByKey ( templateData . application . data , key . substr ( 1 , key . length - 2 ) ) || '' ; return keyValues ; } , { } ) ; const getCountryValue = ( countryCode , templateData ) => { return countryCode . toUpperCase ( ) === window . config . COUNTRY . toUpperCase ( ) ? countryCode : getValueFromApplicationDataByKey ( templateData . application . data , countryCode ) || '' ; } ; function getCountryMessage ( optionalData , countryValue , language ) { const countries = optionalData . filter ( ( country ) => { return country . language === language ; } ) [ 0 ] . countries as ICountry [ ] ; const country = countries . find ( ( country ) => { return country . value === countryValue ; } ) ; return ( country && country . name ) || '' ; } function getTransformedAddress ( keyValues , templateData , addressToParse , addressKey , addressType , countryMessage , countryValue , language ) { return Object . keys ( keyValues ) . reduce ( ( value , key ) => { if ( key . includes ( 'country' ) || key . includes ( 'nationality' ) ) { return value . replace ( new RegExp ( `${ key }` , 'g' ) , countryMessage || '' ) ; } else if ( ! isDefaultCountry ( countryValue ) ) { if ( language !== 'en' ) { return countryMessage ; } else { return value . replace ( new RegExp ( `${ key }` , 'g' ) , keyValues [ key ] ) ; } } else { const addresses = templateData . resource [ addressType as keyof typeof templateData . resource ] ; const address = addresses [ keyValues [ key ] as keyof typeof addresses ] ; return value . replace ( new RegExp ( `${ key }` , 'g' ) , address [ addressKey ] || '' ) ; } } , addressToParse ) ; } export const offlineTransformers = { OfflineCompanyLogo : ( templateData , intl ) => { return templateData . resource . assets . logo ; } , OfflineAddress : ( templateData , intl , payload ? , optionalData ? ) => { const params = payload && ( payload as IOfflineAddressPayload ) ; if ( ! params ) { throw new Error ( 'No payload found for this transformer' ) ; } if ( ! optionalData ) { throw new Error ( 'No countries found for this transformer' ) ; } const matchedCondition = getMatchedCondition ( params . conditions , templateData . application . data ) as IOfflineAddressCondition ; if ( ! matchedCondition ) { throw new Error ( 'No condition has matched for OfflineAddress transformer' ) ; } try { const countryValue = getCountryValue ( matchedCondition . addresses . countryCode , templateData ) ; if ( countryValue === '' ) { return '' ; } const countryMessage = getCountryMessage ( optionalData , countryValue , params . language ) ; let addressToParse = '' ; if ( isDefaultCountry ( countryValue ) ) { addressToParse = matchedCondition . addresses . localAddress ; } else { addressToParse = matchedCondition . addresses . internationalAddress as string ; } const keyValues = getKeyValues ( addressToParse . match ( /\{.*?\}/g ) , templateData ) ; let value = '' ; if ( addressToParse . match ( /\{.*?\}/g ) === null ) { value = matchedCondition . addresses . localAddress ; } else { value = getTransformedAddress ( keyValues , templateData , addressToParse , matchedCondition . addressKey , matchedCondition . addressType , countryMessage , countryValue , params . language ) ; } return value ; } catch ( error ) { console . error ( error ) ; return '' ; } } } ;