import {__, CouponsPlus} from "../../../globals"
import {ComponentMeta} from "../../ComponentsMeta"
import {iconWithClassName} from "../../IconWithClassName"
import {CardRenderer, CardRendererContext} from "../../CardRenderers";
import {usingTestableOptions} from "../CardHelpers"
import {ComponentCategory} from "../../components";
import {CustomFieldComputedValue, customFieldFieldsMap, CustomFieldOptions} from "./CustomField";
import {FieldType} from "../../fields";
import {MultiSelectSearchProps} from "../../fields/MultiSelectSearch";
import {CalendarProps} from "../../fields/Calendar";
import {NumberProps} from "../../fields/Number";
import dayjs from "dayjs";
const type = CouponsPlus?.components?.conditions?.UserRegistrationTime?.type || 'UserRegistrationTime'
export type AccountRegistrationTimeOptions = {
type: 'recently' | 'range',
recently: {
value: number,
unit: 'hours' | 'days',
allowGuests: boolean
},
range: {
from: string,
to: string
}
}
export const AccountRegistrationTimeMeta: ComponentMeta = {
id: type,
type: 'conditions',
category: ComponentCategory.Account,
fieldsTitlePrefix: false,
icon: iconWithClassName(({className, context}) => {
if (context === 'card') {
// solid
return
}
// outline
return
}),
// @ts-ignore
fieldsMap: ({options: {type}}: Partial<{ options: AccountRegistrationTimeOptions }>) => [
false && [{id: 'type', renderType: FieldType.Tab},],
...type === 'recently' ? [
[{
id: 'recently.value',
renderType: FieldType.Number,
fieldProps: {hasAmountStructure: false} as NumberProps
}],
[{
id: 'recently.unit',
renderType: FieldType.Select,
fieldOptions: {/*optionsKey: '_allowed',*/ centered: true},
fieldProps: {size: 'small', style: 'gray', width: 'auto'} as MultiSelectSearchProps,
}],
[{
id: 'recently.allowGuests',
renderType: FieldType.BooleanTab,
}]
] : [],
...type === 'range' ? [
[
{
id: 'range.from',
renderType: FieldType.Date,
fieldProps: {size: 'small'} as CalendarProps,
fieldOptions: {topBorder: false},
title: __('Start Date')
},
{
id: 'range.to',
renderType: FieldType.Date,
fieldProps: {size: 'small'} as CalendarProps,
fieldOptions: {topBorder: false},
title: __('End Date')
},
],
] : [],
]
}
export const AccountRegistrationTimeRenderer: CardRenderer = {
type,
icon: AccountRegistrationTimeMeta.icon!,
prefix: '*',
example: __("Within 24 hours, from March 1, 2016 to October 31, 2016, etc."),
AboveComputedValue: usingTestableOptions(({type}, {context}) => {
if ((['tier-base', 'tier-subchild'] as CardRendererContext[]).includes(context)) {
return false
}
if (type === 'recently') {
return __('Registration Time');
}
return __('Registration Date');
}),
ComputedValue: usingTestableOptions(({type, recently, range}, {context}) => {
if (type === 'recently') {
let allowGuestsText = '';
switch (recently.unit) {
case 'hours':
allowGuestsText = __('Within * hours').replace('*', recently.value);
break;
case 'days':
allowGuestsText = __('Within * days').replace('*', recently.value);
break;
}
if (recently.allowGuests) {
allowGuestsText += ` ${__('(including guests)')}`;
} else {
allowGuestsText += ` ${__('(excluding guests)')}`;
}
return allowGuestsText;
}
// todo: this is repeated from Date.tsx, should be refactored
const parseFormat = 'YYYY-MM-DD HH:mm';
return
{__('From')}
{dayjs(range.from, parseFormat).format('ll')}
{__('To')}
{dayjs(range.to, parseFormat).format('ll')}
;
}),
}