@@ -21,13 +21,16 @@ import type {
2121 ValidatedTextCollector ,
2222 InferValueObjectCollectorType ,
2323 ObjectValueCollectorTypes ,
24+ AutoCollectorTypes ,
2425 UnknownCollector ,
26+ InferAutoCollectorType ,
2527} from './collector.types.js' ;
2628import type {
2729 DeviceAuthenticationField ,
2830 DeviceRegistrationField ,
2931 MultiSelectField ,
3032 PhoneNumberField ,
33+ ProtectField ,
3134 ReadOnlyField ,
3235 RedirectField ,
3336 SingleSelectField ,
@@ -253,6 +256,66 @@ export function returnSingleValueCollector<
253256 }
254257}
255258
259+ /**
260+ * @function returnAutoCollector - Creates an AutoCollector object based on the provided field, index, and optional collector type.
261+ * @param {DaVinciField } field - The field object containing key, label, type, and links.
262+ * @param {number } idx - The index to be used in the id of the AutoCollector.
263+ * @param {AutoCollectorTypes } [collectorType] - Optional type of the AutoCollector.
264+ * @returns {AutoCollector } The constructed AutoCollector object.
265+ */
266+ export function returnAutoCollector <
267+ Field extends ProtectField ,
268+ CollectorType extends AutoCollectorTypes = 'SingleValueAutoCollector' ,
269+ > ( field : Field , idx : number , collectorType : CollectorType , data ?: string ) {
270+ let error = '' ;
271+ if ( ! ( 'key' in field ) ) {
272+ error = `${ error } Key is not found in the field object. ` ;
273+ }
274+ if ( ! ( 'type' in field ) ) {
275+ error = `${ error } Type is not found in the field object. ` ;
276+ }
277+
278+ if ( collectorType === 'ProtectCollector' ) {
279+ return {
280+ category : 'SingleValueAutoCollector' ,
281+ error : error || null ,
282+ type : collectorType ,
283+ id : `${ field ?. key } -${ idx } ` ,
284+ name : field . key ,
285+ input : {
286+ key : field . key ,
287+ value : data || '' ,
288+ type : field . type ,
289+ } ,
290+ output : {
291+ key : field . key ,
292+ type : field . type ,
293+ config : {
294+ behavioralDataCollection : field . behavioralDataCollection ,
295+ universalDeviceIdentification : field . universalDeviceIdentification ,
296+ } ,
297+ } ,
298+ } as InferAutoCollectorType < 'ProtectCollector' > ;
299+ } else {
300+ return {
301+ category : 'SingleValueAutoCollector' ,
302+ error : error || null ,
303+ type : collectorType || 'SingleValueAutoCollector' ,
304+ id : `${ field . key } -${ idx } ` ,
305+ name : field . key ,
306+ input : {
307+ key : field . key ,
308+ value : data || '' ,
309+ type : field . type ,
310+ } ,
311+ output : {
312+ key : field . key ,
313+ type : field . type ,
314+ } ,
315+ } as InferAutoCollectorType < CollectorType > ;
316+ }
317+ }
318+
256319/**
257320 * @function returnPasswordCollector - Creates a PasswordCollector object based on the provided field and index.
258321 * @param {DaVinciField } field - The field object containing key, label, type, and links.
@@ -272,6 +335,7 @@ export function returnPasswordCollector(field: StandardField, idx: number) {
272335export function returnTextCollector ( field : StandardField , idx : number , data : string ) {
273336 return returnSingleValueCollector ( field , idx , 'TextCollector' , data ) ;
274337}
338+
275339/**
276340 * @function returnSingleSelectCollector - Creates a SingleCollector object based on the provided field and index.
277341 * @param {DaVinciField } field - The field object containing key, label, type, and links.
@@ -282,6 +346,16 @@ export function returnSingleSelectCollector(field: SingleSelectField, idx: numbe
282346 return returnSingleValueCollector ( field , idx , 'SingleSelectCollector' , data ) ;
283347}
284348
349+ /**
350+ * @function returnProtectCollector - Creates a ProtectCollector object based on the provided field and index.
351+ * @param {DaVinciField } field - The field object containing key, label, type, and links.
352+ * @param {number } idx - The index to be used in the id of the ProtectCollector.
353+ * @returns {ProtectCollector } The constructed ProtectCollector object.
354+ */
355+ export function returnProtectCollector ( field : ProtectField , idx : number , data : string ) {
356+ return returnAutoCollector ( field , idx , 'ProtectCollector' , data ) ;
357+ }
358+
285359/**
286360 * @function returnMultiValueCollector - Creates a MultiValueCollector object based on the provided field, index, and optional collector type.
287361 * @param {DaVinciField } field - The field object containing key, label, type, and links.
0 commit comments