import { IQueryParameter } from '@kentico/kontent-core' ; export namespace Filters { const valueSeparator = ',' ; const defaultValue = '' ; const getParamValueForSystemFilter = ( param ) : string | undefined => { if ( ! param ) { return defaultValue ; } if ( Array . isArray ( param ) ) { let value = '' ; for ( let i = 0 ; i < param . length ; i ++ ) { value = value + param [ i ] . toString ( ) ; if ( i !== param . length - 1 ) { value = value + valueSeparator ; } } return value ; } return param . toString ( ) ; } ; export class TypeFilter implements IQueryParameter { constructor ( public type ) { } getParam ( ) { if ( Array . isArray ( this . type ) ) { return `system.type[in]=${ getParamValueForSystemFilter ( this . type ) }` ; } return `system.type=${ getParamValueForSystemFilter ( this . type ) }` ; } } export class CollectionFilter implements IQueryParameter { constructor ( public collection ) { } getParam ( ) { if ( Array . isArray ( this . collection ) ) { return `system.collection[in]=${ getParamValueForSystemFilter ( this . collection ) }` ; } return `system.collection=${ getParamValueForSystemFilter ( this . collection ) }` ; } } export class EmptyFilter implements IQueryParameter { constructor ( public element ) { } getParam ( ) { return `${ this . element . trim ( ) }[empty]` ; } } export class NotEmptyFilter implements IQueryParameter { constructor ( public element ) { } getParam ( ) { return `${ this . element . trim ( ) }[nempty]` ; } } export class EqualsFilter implements IQueryParameter { constructor ( public element , public value ) { } getParam ( ) { return `${ this . element . trim ( ) }[eq]=${ this . getParamValue ( ) }` ; } private getParamValue ( ) { if ( ! this . value ) { return defaultValue ; } return this . value ; } } export class NotEqualsFilter implements IQueryParameter { constructor ( public element , public value ) { } getParam ( ) { return `${ this . element . trim ( ) }[neq]=${ this . getParamValue ( ) }` ; } private getParamValue ( ) { if ( ! this . value ) { return defaultValue ; } return this . value ; } } export class AllFilter implements IQueryParameter { constructor ( public element , public values ) { } getParam ( ) { return `${ this . element . trim ( ) }[all]=${ this . getParamValue ( ) }` ; } private getParamValue ( ) { if ( ! this . values || ! Array . isArray ( this . values ) ) { return defaultValue ; } return this . values . map ( ( m ) => m . trim ( ) ) . join ( ',' ) ; } } export class AnyFilter implements IQueryParameter { constructor ( public element , public values ) { } getParam ( ) { return `${ this . element . trim ( ) }[any]=${ this . getParamValue ( ) }` ; } private getParamValue ( ) { if ( ! this . values || ! Array . isArray ( this . values ) ) { return defaultValue ; } return this . values . map ( ( m ) => m . trim ( ) ) . join ( ',' ) ; } } export class ContainsFilter implements IQueryParameter { constructor ( public element , public values ) { } getParam ( ) { return `${ this . element . trim ( ) }[contains]=${ this . getParamValue ( ) }` ; } private getParamValue ( ) { if ( ! this . values || ! Array . isArray ( this . values ) ) { return defaultValue ; } return this . values . map ( ( m ) => m . trim ( ) ) . join ( ',' ) ; } } export class GreaterThanFilter implements IQueryParameter { constructor ( public element , public value ) { } getParam ( ) { return `${ this . element . trim ( ) }[gt]=${ this . getParamValue ( ) }` ; } private getParamValue ( ) { if ( ! this . value ) { return defaultValue ; } return this . value ; } } export class GreaterThanOrEqualFilter implements IQueryParameter { constructor ( public element , public value ) { } getParam ( ) { return `${ this . element . trim ( ) }[gte]=${ this . getParamValue ( ) }` ; } getParamValue ( ) { if ( ! this . value ) { return defaultValue ; } return this . value ; } } export class InFilter implements IQueryParameter { constructor ( public element , public values ) { } getParam ( ) { return `${ this . element . trim ( ) }[in]=${ this . getParamValue ( ) }` ; } private getParamValue ( ) { if ( ! this . values || ! Array . isArray ( this . values ) ) { return defaultValue ; } return this . values . map ( ( m ) => { return m . trim ( ) ; } ) . join ( ',' ) ; } } export class NotInFilter implements IQueryParameter { constructor ( public element , public values ) { } getParam ( ) { return `${ this . element . trim ( ) }[nin]=${ this . getParamValue ( ) }` ; } private getParamValue ( ) { if ( ! this . values || ! Array . isArray ( this . values ) ) { return defaultValue ; } return this . values . map ( ( m ) => { return m . trim ( ) ; } ) . join ( ',' ) ; } } export class LessThanFilter implements IQueryParameter { constructor ( public element , public value ) { } getParam ( ) { return `${ this . element . trim ( ) }[lt]=${ this . getParamValue ( ) }` ; } private getParamValue ( ) { if ( ! this . value ) { return defaultValue ; } return this . value ; } } export class LessThanOrEqualFilter implements IQueryParameter { constructor ( public element , public value ) { } getParam ( ) { return `${ this . element . trim ( ) }[lte]=${ this . getParamValue ( ) }` ; } private getParamValue ( ) { if ( ! this . value ) { return defaultValue ; } return this . value ; } } export class RangeFilter implements IQueryParameter { constructor ( public element , public lowerValue , public higherValue ) { } getParam ( ) { return `${ this . element . trim ( ) }[range]=${ this . getParamValue ( ) }` ; } private getParamValue ( ) { let lowerVal = defaultValue ; let higherVal = defaultValue ; if ( this . lowerValue ) { lowerVal = this . lowerValue . toString ( ) ; } if ( this . higherValue ) { higherVal = this . higherValue . toString ( ) ; } return `${ lowerVal },${ higherVal }` ; } } }