import {__, CouponsPlus} from "../../../globals" import {ComponentMeta} from "../../ComponentsMeta" import {FieldType} from "../../fields" import {iconWithClassName} from "../../IconWithClassName" import {AmountValidatorOptions} from "../filterinterfaces" import {NumberProps} from "../../fields/Number"; import {CardRenderer, CardRendererContext} from "../../CardRenderers"; import {usingTestableOptions} from "../CardHelpers" import {getCardComponentData} from "../../cards" import {getWithCurrency} from "../../fields/Currency"; import {amountValidatorTierFields, TierType} from "../../tiers"; import classNames from "classnames"; import {ComponentCategory} from "../../components"; import {RowOptions} from "../Fields/RowOptions"; export type CartSubtotalOptions = AmountValidatorOptions const type = CouponsPlus?.components?.conditions?.CartSubtotal?.type || 'CartSubtotal' export const CartSubtotalMeta: ComponentMeta = { id: type, type: 'conditions', category: ComponentCategory.Cart, supportsTiers: true, tierFields: amountValidatorTierFields(), tierType: TierType.Numeric, configurable: context => context !== 'tier-base', icon: iconWithClassName(({className, context}) => { if (context === 'card') { // solid return } // outline return }), fieldsMap: [ [ new RowOptions({ type: 'featured', colWidth: 'auto' }), {id: 'quantity', renderType: FieldType.Number, fieldProps: {type: 'currency', border: false} as NumberProps} ] ] } export const CartSubtotalRenderer: CardRenderer = { type, icon: CartSubtotalMeta.icon!, prefix: scope => scope === 'tier-base' ? __('*') : __('[A] [Specific] *'), example: scope => scope === 'tier-base' ? __('$100 min. -> 10% OFF, $200 min. -> 20% OFF, etc.') : __('Minimum, Maximum or in Range'), AboveComputedValue: usingTestableOptions(({quantity: {type}}, {context}) => { if ((['tier-base', 'tier-subchild'] as CardRendererContext[]).includes(context)) { return false } switch (type) { case "equals": case "range": return __('Cart Subtotal') case "minimum": return __('Minimum Cart Subtotal') case "maximum": return __('Maximum Cart Subtotal') } }), ComputedValue: usingTestableOptions(({quantity: {amount, type, range}}, {context}) => { if (context === 'tier-base') { return getCardComponentData('condition', CartSubtotalMeta.id).name } let value: string | number | undefined = '' switch (type) { case "equals": case "minimum": case "maximum": value = getWithCurrency(amount) break case "range": value = `${getWithCurrency(range.minimum)} - ${getWithCurrency(range.maxmimum)}` break } if (context === 'tier-subchild') { return {value} {type === 'minimum' && } {type === 'maximum' && {__('max.')}} } return value }), }