File size: 4,648 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import { select } from '@wordpress/data';
import deprecate from '@wordpress/deprecated';
import {
	DEFAULT_PAID_PLAN,
	TIMELESS_PLAN_ECOMMERCE,
	TIMELESS_PLAN_FREE,
	STORE_KEY,
	FREE_PLAN_PRODUCT_ID,
} from './constants';
import type { State } from './reducer';
import type {
	Plan,
	PlanFeature,
	FeaturesByType,
	PlanProduct,
	PlanPath,
	PlanSlug,
	PlansSelect,
	StorePlanSlug,
} from './types';

// Some of these selectors require unused parameters because those
// params are used by the associated resolver.
/* eslint-disable @typescript-eslint/no-unused-vars */

export const getFeatures = ( state: State, locale: string ): Record< string, PlanFeature > =>
	state.features[ locale ] ?? {};

export const getFeaturesByType = ( state: State, locale: string ): Array< FeaturesByType > =>
	state.featuresByType[ locale ] ?? [];

export const getPlanByProductId = (
	_state: State,
	productId: number | undefined,
	locale: string
): Plan | undefined => {
	if ( ! productId ) {
		return undefined;
	}

	return ( select( STORE_KEY ) as PlansSelect )
		.getSupportedPlans( locale )
		.find( ( plan: Plan ) => plan.productIds.indexOf( productId ) > -1 );
};

export const getPlanProductById = (
	_state: State,
	productId: number | undefined
): PlanProduct | undefined => {
	if ( ! productId ) {
		return undefined;
	}

	return ( select( STORE_KEY ) as PlansSelect )
		.getPlansProducts()
		.find( ( product: PlanProduct ) => product.productId === productId );
};

export const getPlanByPeriodAgnosticSlug = (
	_state: State,
	slug: PlanSlug | undefined,
	locale: string
): Plan | undefined => {
	if ( ! slug ) {
		return undefined;
	}
	return ( select( STORE_KEY ) as PlansSelect )
		.getSupportedPlans( locale )
		.find( ( plan: Plan ) => plan.periodAgnosticSlug === slug );
};

export const getDefaultPaidPlan = ( _: State, locale: string ): Plan | undefined => {
	return ( select( STORE_KEY ) as PlansSelect )
		.getSupportedPlans( locale )
		.find( ( plan: Plan ) => plan.periodAgnosticSlug === DEFAULT_PAID_PLAN );
};

export const getDefaultFreePlan = ( _: State, locale: string ): Plan | undefined => {
	return ( select( STORE_KEY ) as PlansSelect )
		.getSupportedPlans( locale )
		.find( ( plan: Plan ) => plan.periodAgnosticSlug === TIMELESS_PLAN_FREE );
};

export const getSupportedPlans = ( state: State, _locale: string ): Plan[] => {
	return state.plans[ _locale ] ?? [];
};

export const getPlansProducts = ( state: State ): PlanProduct[] => {
	return state.planProducts;
};

/**
 * @deprecated  getPrices is deprecated, please use plan.price directly
 * @param _state the state
 * @param _locale the locale
 */
export const getPrices = ( _state: State, _locale: string ): Record< StorePlanSlug, string > => {
	deprecate( 'getPrices', {
		alternative: 'getPlanProduct().price',
	} );
	return ( select( STORE_KEY ) as PlansSelect ).getPlansProducts().reduce(
		( prices: Record< StorePlanSlug, string >, plan: PlanProduct ) => {
			prices[ plan.storeSlug ] = plan.price;
			return prices;
		},
		{} as Record< StorePlanSlug, string >
	);
};

export const getPlanByPath = (
	_state: State,
	path: PlanPath | undefined,
	locale: string
): Plan | undefined => {
	if ( ! path ) {
		return undefined;
	}

	const planProduct = ( select( STORE_KEY ) as PlansSelect )
		.getPlansProducts()
		.find( ( product: PlanProduct ) => product.pathSlug === path );

	if ( ! planProduct ) {
		return undefined;
	}

	return ( select( STORE_KEY ) as PlansSelect )
		.getSupportedPlans( locale )
		.find( ( plan: Plan ) => plan.periodAgnosticSlug === planProduct.periodAgnosticSlug );
};

export const getPlanProduct = (
	_state: State,
	periodAgnosticSlug: string | undefined,
	billingPeriod: PlanProduct[ 'billingPeriod' ] | undefined
): PlanProduct | undefined => {
	if ( ! periodAgnosticSlug || ! billingPeriod ) {
		return undefined;
	}

	return ( select( STORE_KEY ) as PlansSelect )
		.getPlansProducts()
		.find( ( product: PlanProduct ) => {
			const matchesSlug = product.periodAgnosticSlug === periodAgnosticSlug;
			// The billing period doesn't matter when dealing with free plan
			const matchesBillingPeriod =
				periodAgnosticSlug === TIMELESS_PLAN_FREE || product.billingPeriod === billingPeriod;

			return matchesSlug && matchesBillingPeriod;
		} );
};

export const isPlanEcommerce = ( _: State, planSlug?: PlanSlug ): boolean => {
	return planSlug === TIMELESS_PLAN_ECOMMERCE;
};

export const isPlanFree = ( _: State, planSlug?: PlanSlug ): boolean => {
	return planSlug === TIMELESS_PLAN_FREE;
};

export const isPlanProductFree = ( _: State, planProductId: number | undefined ): boolean =>
	planProductId === FREE_PLAN_PRODUCT_ID;