File size: 2,012 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import { EXAMPLE_FLOW, NEW_HOSTED_SITE_FLOW } from '@automattic/onboarding';
import validUrl from 'valid-url';

// Only override the back button from an external URL source on the below step(s) which is typically where we'd send them to as the 'entry'.
// We don't want to send them "back" to the source URL if they click back on "domains-launch/mapping" for example. Just send them back to the previous step.
export const backUrlExternalSourceStepsOverrides = [ 'use-your-domain' ];

// Override Back link if source parameter is found below
export const backUrlSourceOverrides = {
	'business-name-generator': '/business-name-generator',
	domains: '/domains',
};

export function getExternalBackUrl( source, sectionName = null ) {
	if ( ! source ) {
		return false;
	}

	if ( backUrlSourceOverrides[ source ] ) {
		return backUrlSourceOverrides[ source ];
	}

	if (
		backUrlExternalSourceStepsOverrides.includes( sectionName ) &&
		validUrl.isWebUri( source )
	) {
		return source;
	}

	return false;
}

/**
 * Check if we should use multiple domains in domain flows.
 */
export function shouldUseMultipleDomainsInCart( flowName ) {
	const enabledFlows = [
		'domain',
		'onboarding',
		'domains',
		'domains/add',
		EXAMPLE_FLOW,
		NEW_HOSTED_SITE_FLOW,
	];

	return enabledFlows.includes( flowName );
}

export function sortProductsByPriceDescending( productsInCart ) {
	// Sort products by price descending, considering promotions.
	const getSortingValue = ( product ) => {
		if ( product.item_subtotal_integer !== 0 ) {
			return product.item_subtotal_integer;
		}

		// Use the lowest non-zero new_price or fallback to item_original_cost_integer.
		const nonZeroPrices =
			product.cost_overrides
				?.map( ( override ) => override.new_price * 100 )
				.filter( ( price ) => price > 0 ) || [];

		return nonZeroPrices.length ? Math.min( ...nonZeroPrices ) : product.item_original_cost_integer;
	};

	return productsInCart.sort( ( a, b ) => {
		return getSortingValue( b ) - getSortingValue( a );
	} );
}