/** * WordPress dependencies */ import { select } from '@safe-wordpress/data'; /** * External dependencies */ import type { ConversionAction, ECommercePlugin, Experiment, GoalId, Maybe, } from '@nab/types'; /** * Internal dependencies */ import { store } from './store'; export function getECommercePlugin( experiment: Maybe< Experiment >, goalId?: GoalId ): Maybe< ECommercePlugin > { if ( ! experiment ) { return undefined; } goalId = goalId ?? experiment.goals[ 0 ]?.id; const goal = experiment.goals.find( ( g ) => g.id === goalId ); if ( ! goal ) { return undefined; } return getECommercePluginForActions( goal.conversionActions ); } export function getECommercePluginForActions( actions: ReadonlyArray< ConversionAction > ): Maybe< ECommercePlugin > { if ( ! actions.length ) { return undefined; } const getConversionActionType = select( store ).getConversionActionType; const plugins = actions.map( ( action ) => getConversionActionType( action.type )?.ecommercePlugin ); const plugin = plugins[ 0 ]; return plugin && plugins.every( ( value ) => value === plugin ) ? plugin : undefined; }