'use strict' ; import { isObject } from './utils' ; import { logMessages } from './constants' ; import type { SelfDescribing , EventContext , ScreenViewProps , StructuredProps , PageViewProps , TimingProps , ConsentGrantedProps , ConsentWithdrawnProps , EcommerceItem , EcommerceTransactionProps , } from './types' ; param sd { Object } returns function isValidSD ( sd ) { return isObject ( sd ) && typeof sd . schema === 'string' && isObject ( sd . data ) ; } param contexts { Object } returns function validateContexts ( contexts ) { const isValid = Object . prototype . toString . call ( contexts ) === '[object Array]' && contexts . map ( ( c ) => isValidSD ( c ) ) . reduce ( ( acc , curr ) => acc !== false && curr , true ) ; if ( ! isValid ) { return Promise . reject ( new Error ( logMessages . context ) ) ; } return Promise . resolve ( true ) ; } param argmap { Object } returns function validateSelfDesc ( argmap ) { if ( ! isValidSD ( argmap ) ) { return Promise . reject ( new Error ( logMessages . selfDesc ) ) ; } return Promise . resolve ( true ) ; } param argmap { Object } returns function validateScreenView ( argmap ) { if ( ! isObject ( argmap ) ) { return Promise . reject ( new Error ( logMessages . evType ) ) ; } if ( typeof argmap . name !== 'string' ) { return Promise . reject ( new Error ( logMessages . screenViewReq ) ) ; } return Promise . resolve ( true ) ; } param argmap { Object } returns function validateStructured ( argmap ) { if ( ! isObject ( argmap ) ) { return Promise . reject ( new Error ( logMessages . evType ) ) ; } if ( typeof argmap . category !== 'string' || typeof argmap . action !== 'string' ) { return Promise . reject ( new Error ( logMessages . structuredReq ) ) ; } return Promise . resolve ( true ) ; } param argmap { Object } returns function validatePageView ( argmap ) { if ( ! isObject ( argmap ) ) { return Promise . reject ( new Error ( logMessages . evType ) ) ; } if ( typeof argmap . pageUrl !== 'string' ) { return Promise . reject ( new Error ( logMessages . pageviewReq ) ) ; } return Promise . resolve ( true ) ; } param argmap { Object } returns function validateTiming ( argmap ) { if ( ! isObject ( argmap ) ) { return Promise . reject ( new Error ( logMessages . evType ) ) ; } if ( typeof argmap . category !== 'string' || typeof argmap . variable !== 'string' || typeof argmap . timing !== 'number' ) { return Promise . reject ( new Error ( logMessages . timingReq ) ) ; } return Promise . resolve ( true ) ; } param argmap { Object } returns function validateConsentGranted ( argmap ) { if ( ! isObject ( argmap ) ) { return Promise . reject ( new Error ( logMessages . evType ) ) ; } if ( typeof argmap . expiry !== 'string' || typeof argmap . documentId !== 'string' || typeof argmap . version !== 'string' ) { return Promise . reject ( new Error ( logMessages . consentGReq ) ) ; } return Promise . resolve ( true ) ; } param argmap { Object } returns function validateConsentWithdrawn ( argmap ) { if ( ! isObject ( argmap ) ) { return Promise . reject ( new Error ( logMessages . evType ) ) ; } if ( typeof argmap . all !== 'boolean' || typeof argmap . documentId !== 'string' || typeof argmap . version !== 'string' ) { return Promise . reject ( new Error ( logMessages . consentWReq ) ) ; } return Promise . resolve ( true ) ; } param item { Object } returns function isValidEcomItem ( item ) { if ( isObject ( item ) && typeof item . sku === 'string' && typeof item . price === 'number' && typeof item . quantity === 'number' ) { return true ; } return false ; } param items { Object } returns function validItemsArg ( items ) { return Object . prototype . toString . call ( items ) === '[object Array]' && items . map ( ( i ) => isValidEcomItem ( i ) ) . reduce ( ( acc , curr ) => acc !== false && curr , true ) ; } param argmap { Object } returns function validateEcommerceTransaction ( argmap ) { if ( ! isObject ( argmap ) ) { return Promise . reject ( new Error ( logMessages . evType ) ) ; } if ( typeof argmap . orderId !== 'string' || typeof argmap . totalValue !== 'number' || ! validItemsArg ( argmap . items ) ) { return Promise . reject ( new Error ( logMessages . ecomReq ) ) ; } return Promise . resolve ( true ) ; } export { isValidSD , validateContexts , validateSelfDesc , validateScreenView , validateStructured , validatePageView , validateTiming , validateConsentGranted , validateConsentWithdrawn , isValidEcomItem , validItemsArg , validateEcommerceTransaction , } ;