11import type { Mapping , WalkerOS } from '@elbwalker/types' ;
22import { getGrantedConsent } from './consent' ;
33import { getByPath } from './byPath' ;
4- import { isArray , isDefined , isObject , isString } from './is' ;
4+ import { isArray , isDefined , isString } from './is' ;
55import { castToProperty } from './property' ;
66
77export function getMappingEvent (
@@ -51,20 +51,22 @@ export function getMappingEvent(
5151}
5252
5353export function getMappingValue (
54- obj : WalkerOS . PartialEvent | WalkerOS . AnyObject ,
55- data : Mapping . Data ,
54+ value : WalkerOS . DeepPartialEvent | unknown | undefined ,
55+ data : Mapping . Data = { } ,
5656 options : Mapping . Options = { } ,
5757) : WalkerOS . Property | undefined {
58+ if ( ! isDefined ( value ) ) return ;
59+
5860 const mappings = isArray ( data ) ? data : [ data ] ;
5961
6062 for ( const mapping of mappings ) {
61- const result = processMappingValue ( obj , mapping , options ) ;
63+ const result = processMappingValue ( value , mapping , options ) ;
6264 if ( isDefined ( result ) ) return result ;
6365 }
6466}
6567
6668function processMappingValue (
67- obj : WalkerOS . PartialEvent | WalkerOS . AnyObject ,
69+ value : WalkerOS . DeepPartialEvent | unknown ,
6870 mapping : Mapping . Value ,
6971 options : Mapping . Options = { } ,
7072) : WalkerOS . Property | undefined {
@@ -77,61 +79,70 @@ function processMappingValue(
7779 return mappings . reduce ( ( acc , mappingItem ) => {
7880 if ( acc ) return acc ; // A valid result was already found
7981
80- const { condition, consent, fn, key, loop, map, set, validate, value } =
81- isString ( mappingItem )
82- ? ( { key : mappingItem } as Mapping . ValueConfig )
83- : mappingItem ;
82+ const {
83+ condition,
84+ consent,
85+ fn,
86+ key,
87+ loop,
88+ map,
89+ set,
90+ validate,
91+ value : staticValue ,
92+ } = isString ( mappingItem ) ? { key : mappingItem } : mappingItem ;
8493
8594 // Check if this mapping should be used
86- if ( condition && ! condition ( obj , mappingItem , instance ) ) return ;
95+ if ( condition && ! condition ( value , mappingItem , instance ) ) return ;
8796
8897 // Check if consent is required and granted
89- if ( consent && ! getGrantedConsent ( consent , instance ?. consent ) ) return value ;
98+ if ( consent && ! getGrantedConsent ( consent , instance ?. consent ) )
99+ return staticValue ;
100+
101+ let mappingValue : unknown = staticValue || value ;
90102
91- let mappingValue ;
92103 if ( fn ) {
93104 // Use a custom function to get the value
94- mappingValue = fn ( obj , mappingItem , options ) ;
95- } else {
105+ mappingValue = fn ( value as WalkerOS . PartialEvent , mappingItem , options ) ; // @TODO stop casting
106+ }
107+
108+ if ( key ) {
96109 // Get dynamic value from the event
97- mappingValue = getByPath ( obj , key , value ) ;
110+ mappingValue = getByPath ( value , key , staticValue ) ;
98111 }
99112
100113 if ( loop ) {
101114 const [ scope , itemMapping ] = loop ;
102115
103116 const data =
104- scope === 'this' ? [ obj ] : getMappingValue ( obj , scope , options ) ;
117+ scope === 'this' ? [ value ] : getMappingValue ( value , scope , options ) ;
105118
106119 if ( isArray ( data ) ) {
107120 mappingValue = data
108- . map ( ( item ) =>
109- getMappingValue ( isObject ( item ) ? item : { } , itemMapping , options ) ,
110- )
121+ . map ( ( item ) => getMappingValue ( item , itemMapping , options ) )
111122 . filter ( isDefined ) ;
112123 }
113124 } else if ( map ) {
114125 mappingValue = Object . entries ( map ) . reduce (
115126 ( mappedObj , [ mapKey , mapValue ] ) => {
116- const result = getMappingValue ( obj , mapValue , options ) ;
127+ const result = getMappingValue ( value , mapValue , options ) ;
117128 if ( isDefined ( result ) ) mappedObj [ mapKey ] = result ;
118129
119130 return mappedObj ;
120131 } ,
121132 { } as WalkerOS . AnyObject ,
122133 ) ;
123134 } else if ( set ) {
124- mappingValue = set . map ( ( item ) => processMappingValue ( obj , item , options ) ) ;
135+ mappingValue = set . map ( ( item ) =>
136+ processMappingValue ( value , item , options ) ,
137+ ) ;
125138 }
126139
127140 // Validate the value
128- if ( validate && ! validate ( mappingValue ) ) {
129- mappingValue = undefined ;
130- }
141+ if ( validate && ! validate ( mappingValue ) ) mappingValue = undefined ;
131142
132143 const property = castToProperty ( mappingValue ) ;
133144
134145 // Finally, check and convert the type
135- return isDefined ( property ) ? property : value ; // Always use value as a fallback
146+ return isDefined ( property ) ? property : castToProperty ( staticValue ) ; // Always use value as a fallback
136147 } , undefined as WalkerOS . Property | undefined ) ;
137148}
0 commit comments