/** * External dependencies */ import type { KeyValueMatch, DateMatch, Match, NumberMatch, StringMatch, TimeValue, } from '@nelio/popups/types'; export function parseKeyValueMatch( kvm: Partial< KeyValueMatch > ): KeyValueMatch { if ( ! kvm.exists ) { return { exists: false, key: kvm.key ?? '', }; } return { exists: true, key: kvm.key ?? '', ...( kvm.value && { value: parseStringMatch( kvm.value ) } ), }; } export function parseMatch( sm: Partial< Match > ): Match { return { matchType: sm.matchType ?? 'is', matchValue: sm.matchValue ?? [], }; } export function parseStringMatch( sm: Partial< StringMatch > ): StringMatch { return { matchType: sm.matchType ?? 'is', matchValue: sm.matchValue ?? '', }; } export function parseDateMatch( sm: Partial< DateMatch > ): DateMatch { if ( 'between' === sm.matchType ) { return { matchType: 'between', minMatchValue: sm.minMatchValue ?? '', maxMatchValue: sm.maxMatchValue ?? '', }; } if ( 'less-than' === sm.matchType ) { return { matchType: 'less-than', matchValue: sm.matchValue ?? '', }; } if ( 'greater-than' === sm.matchType ) { return { matchType: 'greater-than', matchValue: sm.matchValue ?? '', }; } return { matchType: 'greater-than', matchValue: '', }; } export function parseNumberMatch( sm: Partial< NumberMatch > ): NumberMatch { if ( 'between' === sm.matchType ) { return { matchType: 'between', minMatchValue: sm.minMatchValue ?? 0, maxMatchValue: sm.maxMatchValue ?? 0, }; } if ( 'less-than' === sm.matchType ) { return { matchType: 'less-than', matchValue: sm.matchValue ?? 0, }; } if ( 'greater-than' === sm.matchType ) { return { matchType: 'greater-than', matchValue: sm.matchValue ?? 0, }; } return { matchType: 'greater-than', matchValue: 0, }; } export function parseTimeValue( tv?: Partial< TimeValue > ): TimeValue { return { unit: 'seconds', value: 0, ...tv, }; }