import { TuplePage } from '../../query/tuple-page' ; import { Bucket , BucketId , BucketType , NumericValueBucket , RangeBucketValueIncluding } from '../../tuples/bucket-types' ; import { isEnumMeasureBucket , isMeasureBucket } from '../../tuples/bucket-utils' ; import { MeasureMethod } from '../../tuples/indicator-types' ; import { QueryBucket , QueryByBucketMethod } from '../../tuples/query-bucket-types' ; import { isFakedUuid } from '../../tuples/utils' ; import { getCurrentTime } from '../../utils' ; import { DemoBuckets , DemoQueryBuckets } from './mock-data-buckets' ; export const listMockBuckets = async ( options ) : Promise < TuplePage < QueryBucket > > => { const { pageNumber = 1 , pageSize = 9 } = options ; return new Promise < TuplePage < QueryBucket > > ( ( resolve ) => { setTimeout ( ( ) => { resolve ( { data : DemoQueryBuckets , itemCount : DemoQueryBuckets . length , pageNumber , pageSize , pageCount : 1 } ) ; } , 1000 ) ; } ) ; } ; export const fetchMockBucket = async ( bucketId ) : Promise < { bucket : Bucket ; } > => { let bucket ; const found = DemoBuckets . find ( ( { bucketId : id } ) => id == bucketId ) ; if ( found ) { bucket = JSON . parse ( JSON . stringify ( found ) ) ; } else { bucket = { bucketId , name : 'Mock Bucket' , type : BucketType . VALUE , include : RangeBucketValueIncluding . INCLUDE_MIN , segments : [ ] , createTime : getCurrentTime ( ) , lastModified : getCurrentTime ( ) } as NumericValueBucket ; } return { bucket } ; } ; let newBucketId = 10000 ; export const saveMockBucket = async ( bucket ) : Promise < void > => { return new Promise < void > ( ( resolve ) => { if ( isFakedUuid ( bucket ) ) { bucket . bucketId = `${ newBucketId ++ }` ; } setTimeout ( ( ) => resolve ( ) , 500 ) ; } ) ; } ; export const fetchMockBucketsForIndicatorValue = async ( search ) : Promise < Array < QueryBucket > > => { return new Promise < Array < QueryBucket > > ( ( resolve ) => { setTimeout ( ( ) => { const text = ( search || '' ) . toUpperCase ( ) ; resolve ( DemoQueryBuckets . filter ( bucket => bucket . type === BucketType . VALUE ) . filter ( bucket => bucket . name . toUpperCase ( ) . includes ( text ) ) ) ; } , 1000 ) ; } ) ; } ; export const fetchMockBucketsByIds = async ( bucketIds ) : Promise < Array < QueryBucket > > => { return new Promise < Array < QueryBucket > > ( ( resolve ) => { setTimeout ( ( ) => { resolve ( DemoQueryBuckets . filter ( bucket => bucketIds . some ( bucketId => bucketId == bucket . bucketId ) ) ) ; } , 1000 ) ; } ) ; } ; export const fetchMockBucketsByMethods = async ( methods ) : Promise < Array < QueryBucket > > => { return new Promise < Array < QueryBucket > > ( ( resolve ) => { setTimeout ( ( ) => { resolve ( DemoQueryBuckets . filter ( bucket => { if ( ! isMeasureBucket ( bucket ) ) { return false ; } const matchedMethod = methods . find ( ( { method } ) => method === bucket . measure ) ; if ( matchedMethod == null ) { return false ; } if ( matchedMethod . method === MeasureMethod . ENUM ) { return isEnumMeasureBucket ( bucket ) && bucket . enumId == ( matchedMethod as any ) . enumId ; } else { return true ; } } ) ) ; } , 1000 ) ; } ) ; } ;