import _ , { defaults } from 'lodash' ; import { Optional } from '@osd/utility-types' ; import { AggGroupNames , AggParam , AggGroupName } from '../../data/public' ; export interface ISchemas { [ AggGroupNames . Buckets ] : Schema [ ] ; [ AggGroupNames . Metrics ] : Schema [ ] ; all : Schema [ ] ; } export interface Schema { aggFilter : string [ ] ; editor : boolean | string ; group : AggGroupName ; max : number ; min : number ; name : string ; params : AggParam [ ] ; title : string ; defaults : unknown ; hideCustomLabel ? : boolean ; mustBeFirst ? : boolean ; aggSettings ? : any ; } export class Schemas implements ISchemas { all : Schema [ ] = [ ] ; [ AggGroupNames . Buckets ] : Schema [ ] = [ ] ; [ AggGroupNames . Metrics ] : Schema [ ] = [ ] ; constructor ( schemas ) { _ ( schemas || [ ] ) . chain ( ) . map ( ( schema ) => { if ( ! schema . name ) throw new Error ( 'all schema must have a unique name' ) ; if ( schema . name === 'split' ) { schema . params = [ { name : 'row' , default : true , } , ] as AggParam [ ] ; } defaults ( schema , { min : 0 , max : Infinity , group : AggGroupNames . Buckets , title : schema . name , aggFilter : '*' , editor : false , params : [ ] , } ) ; return schema as Schema ; } ) . tap ( ( fullSchemas ) => { this . all = fullSchemas ; } ) . groupBy ( 'group' ) . forOwn ( ( group , groupName ) => { this [ groupName ] = group ; } ) . commit ( ) ; } } export const getSchemaByName = ( schemas , schemaName ? ) => { return schemas . find ( ( s ) => s . name === schemaName ) || ( { } as Schema ) ; } ;