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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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
import { getCurrencyObject } from '@automattic/number-formatters';
import clsx from 'clsx';
import { useTranslate, TranslateResult } from 'i18n-calypso';
import { Component, createElement } from 'react';
import { Badge } from '../';

import './style.scss';

export class PlanPrice extends Component< PlanPriceProps > {
	render() {
		const {
			currencyCode = 'USD',
			rawPrice,
			isSmallestUnit,
			original,
			discounted,
			className,
			displayFlatPrice,
			displayPerMonthNotation,
			productDisplayPrice,
			isOnSale,
			taxText,
			omitHeading,
			priceDisplayWrapperClassName,
			isLargeCurrency,
		} = this.props;

		const classes = clsx( 'plan-price', className, {
			'is-original': original,
			'is-discounted': discounted,
			'is-large-currency': isLargeCurrency,
		} );

		const tagName = omitHeading ? 'span' : 'h4';

		const areThereMultipleRawPrices = rawPrice && Array.isArray( rawPrice ) && rawPrice.length > 1;

		if ( productDisplayPrice && ! areThereMultipleRawPrices ) {
			return createElement(
				tagName,
				{ className: classes },
				<span
					className="plan-price__integer"
					// eslint-disable-next-line react/no-danger
					dangerouslySetInnerHTML={ { __html: productDisplayPrice } }
				/>
			);
		}

		if ( ! currencyCode || rawPrice === undefined || rawPrice === null ) {
			return null;
		}

		// "Normalize" the input price or price range.
		const rawPriceRange = Array.isArray( rawPrice ) ? rawPrice.slice( 0, 2 ) : [ rawPrice ];

		// If zero is in an array of length 2, render nothing
		if ( Array.isArray( rawPrice ) && rawPriceRange.includes( 0 ) ) {
			return null;
		}

		if ( displayFlatPrice ) {
			return (
				<FlatPriceDisplay
					smallerPrice={ rawPriceRange[ 0 ] }
					higherPrice={ rawPriceRange[ 1 ] }
					currencyCode={ currencyCode }
					className={ classes }
					isSmallestUnit={ isSmallestUnit }
				/>
			);
		}

		return (
			<MultiPriceDisplay
				className={ classes }
				tagName={ tagName }
				smallerPrice={ rawPriceRange[ 0 ] }
				higherPrice={ rawPriceRange[ 1 ] }
				currencyCode={ currencyCode }
				taxText={ taxText }
				displayPerMonthNotation={ displayPerMonthNotation }
				isOnSale={ isOnSale }
				priceDisplayWrapperClassName={ priceDisplayWrapperClassName }
				isSmallestUnit={ isSmallestUnit }
			/>
		);
	}
}

export default PlanPrice;

export interface PlanPriceProps {
	/**
	 * Can be either a floating point price to display, or a pair of floating
	 * point prices.
	 *
	 * If there are a pair of prices, they will be displayed as a range (eg:
	 * '$5-$10').
	 *
	 * If either of the numbers is a 0, nothing will be rendered at all.
	 *
	 * If specified along with `productDisplayPrice`, this will be ignored unless
	 * it is an array, in which case `productDisplayPrice` will be ignored.
	 */
	rawPrice?: number | [ number, number ] | null;

	/**
	 * If true, the number(s) in `rawPrice` will be interpreted as integers in
	 * the currency's smallest unit rather than floating point numbers.
	 *
	 * Has no effect if `productDisplayPrice` is being used.
	 */
	isSmallestUnit?: boolean;

	/**
	 * Adds the `is-original` CSS class.
	 */
	original?: boolean;

	/**
	 * Adds the `is-discounted` CSS class.
	 */
	discounted?: boolean;

	/**
	 * The currency code for the price.
	 *
	 * Ignored if `productDisplayPrice` is set and `rawPrice` is not an array.
	 *
	 * Defaults to `USD` if not set or if undefined, but if it is `null`, the
	 * component will render nothing at all (if using `rawPrice`).
	 */
	currencyCode?: string | null;

	/**
	 * A string to add to the className of the component's output.
	 */
	className?: string;

	/**
	 * Displays a "Sale" banner over the price if set.
	 *
	 * Ignored if `productDisplayPrice` is set and `rawPrice` is not an array.
	 * Also ignored if `displayFlatPrice` is set.
	 */
	isOnSale?: boolean;

	/**
	 * Text to display adjacent to the price referring to taxes.
	 *
	 * It is rendered inside the string '(+X tax)' where `X` is this prop.
	 *
	 * Ignored if `productDisplayPrice` is set and `rawPrice` is not an array.
	 * Also ignored if `displayFlatPrice` is set.
	 */
	taxText?: string;

	/**
	 * Whether to display a "per month" label adjacent to the price.
	 *
	 * Ignored if `productDisplayPrice` is set and `rawPrice` is not an array.
	 * Also ignored if `displayFlatPrice` is set.
	 */
	displayPerMonthNotation?: boolean;

	/**
	 * A pre-formatted price to display.
	 *
	 * Ignored if `rawPrice` is set and is an array.
	 */
	productDisplayPrice?: string | TranslateResult;

	/**
	 * If set, the component will render a `span` instead of an `h4`.
	 */
	omitHeading?: boolean;

	/**
	 * If set, the component will render without separating the integer part of
	 * the price from the decimal part of the price.
	 *
	 * If set, many of the other formatting options will be ignored.
	 *
	 * Ignored if `productDisplayPrice` is set and `rawPrice` is not an array.
	 */
	displayFlatPrice?: boolean;

	/**
	 * If set, this renders each price inside a `div` with the class passed,
	 * but is otherwise identical to the output normally used by `rawPrice`.
	 *
	 * Ignored if `displayFlatPrice` is set.
	 *
	 * Ignored if `productDisplayPrice` is set and `rawPrice` is not an array.
	 */
	priceDisplayWrapperClassName?: string;

	/**
	 * If true, this renders the price with a smaller font size by adding the `is-large-currency` class.
	 */
	isLargeCurrency?: boolean;
}

function PriceWithoutHtml( {
	price,
	currencyCode,
	isSmallestUnit,
}: {
	price: number;
	currencyCode: string;
	isSmallestUnit?: boolean;
} ) {
	const priceObj = getCurrencyObject( price, currencyCode, { isSmallestUnit } );
	if ( priceObj.hasNonZeroFraction ) {
		return <>{ `${ priceObj.integer }${ priceObj.fraction }` }</>;
	}
	return <>{ priceObj.integer }</>;
}

function FlatPriceDisplay( {
	smallerPrice,
	higherPrice,
	currencyCode,
	className,
	isSmallestUnit,
}: {
	smallerPrice: number;
	higherPrice?: number;
	currencyCode: string;
	className?: string;
	isSmallestUnit?: boolean;
} ) {
	const { symbol: currencySymbol, symbolPosition } = getCurrencyObject(
		smallerPrice,
		currencyCode
	);
	const translate = useTranslate();

	if ( ! higherPrice ) {
		return (
			<span className={ className }>
				{ symbolPosition === 'before' ? currencySymbol : null }
				<PriceWithoutHtml
					price={ smallerPrice }
					currencyCode={ currencyCode }
					isSmallestUnit={ isSmallestUnit }
				/>
				{ symbolPosition === 'after' ? currencySymbol : null }
			</span>
		);
	}

	return (
		<span className={ className }>
			{ symbolPosition === 'before' ? currencySymbol : null }
			{ translate( '%(smallerPrice)s-%(higherPrice)s', {
				args: {
					smallerPrice: (
						<PriceWithoutHtml
							price={ smallerPrice }
							currencyCode={ currencyCode }
							isSmallestUnit={ isSmallestUnit }
						/>
					),
					higherPrice: (
						<PriceWithoutHtml
							price={ higherPrice }
							currencyCode={ currencyCode }
							isSmallestUnit={ isSmallestUnit }
						/>
					),
				},
				comment: 'The price range for a particular product',
			} ) }
			{ symbolPosition === 'after' ? currencySymbol : null }
		</span>
	);
}

function MultiPriceDisplay( {
	tagName,
	className,
	smallerPrice,
	higherPrice,
	currencyCode,
	taxText,
	displayPerMonthNotation,
	isOnSale,
	priceDisplayWrapperClassName,
	isSmallestUnit,
}: {
	tagName: 'h4' | 'span';
	className?: string;
	smallerPrice: number;
	higherPrice?: number;
	currencyCode: string;
	taxText?: string;
	displayPerMonthNotation?: boolean;
	isOnSale?: boolean;
	priceDisplayWrapperClassName?: string;
	isSmallestUnit?: boolean;
} ) {
	const { symbol: currencySymbol, symbolPosition } = getCurrencyObject(
		smallerPrice,
		currencyCode
	);
	const translate = useTranslate();

	return createElement(
		tagName,
		{ className },
		<>
			{ symbolPosition === 'before' ? (
				<sup className="plan-price__currency-symbol">{ currencySymbol }</sup>
			) : null }

			{ ! higherPrice && (
				<HtmlPriceDisplay
					price={ smallerPrice }
					currencyCode={ currencyCode }
					priceDisplayWrapperClassName={ priceDisplayWrapperClassName }
					isSmallestUnit={ isSmallestUnit }
				/>
			) }
			{ higherPrice &&
				translate( '{{smallerPrice/}}-{{higherPrice/}}', {
					components: {
						smallerPrice: (
							<HtmlPriceDisplay
								price={ smallerPrice }
								currencyCode={ currencyCode }
								priceDisplayWrapperClassName={ priceDisplayWrapperClassName }
								isSmallestUnit={ isSmallestUnit }
							/>
						),
						higherPrice: (
							<HtmlPriceDisplay
								price={ higherPrice }
								currencyCode={ currencyCode }
								priceDisplayWrapperClassName={ priceDisplayWrapperClassName }
								isSmallestUnit={ isSmallestUnit }
							/>
						),
					},
					comment: 'The price range for a particular product',
				} ) }

			{ symbolPosition === 'after' ? (
				<sup className="plan-price__currency-symbol">{ currencySymbol }</sup>
			) : null }

			{ taxText && (
				<sup className="plan-price__tax-amount">
					{ translate( '(+%(taxText)s tax)', { args: { taxText } } ) }
				</sup>
			) }
			{ displayPerMonthNotation && (
				<span className="plan-price__term">
					{ translate( 'per{{newline/}}month', {
						components: { newline: <br /> },
						comment:
							'Displays next to the price. You can remove the "{{newline/}}" if it is not proper for your language.',
					} ) }
				</span>
			) }
			{ isOnSale && (
				<Badge>
					{ translate( 'Sale', {
						comment: 'Shown next to a domain that has a special discounted sale price',
					} ) }
				</Badge>
			) }
		</>
	);
}

function HtmlPriceDisplay( {
	price,
	currencyCode,
	priceDisplayWrapperClassName,
	isSmallestUnit,
}: {
	price: number;
	currencyCode: string;
	priceDisplayWrapperClassName?: string;
	isSmallestUnit?: boolean;
} ) {
	const priceObj = getCurrencyObject( price, currencyCode, { isSmallestUnit } );

	if ( priceDisplayWrapperClassName ) {
		return (
			<div className={ priceDisplayWrapperClassName }>
				<span className="plan-price__integer">
					{ priceObj.integer }
					{ priceObj.hasNonZeroFraction && priceObj.fraction }
				</span>
			</div>
		);
	}

	return (
		<>
			<span className="plan-price__integer">{ priceObj.integer }</span>
			<sup className="plan-price__fraction">
				{ priceObj.hasNonZeroFraction && priceObj.fraction }
			</sup>
		</>
	);
}