import { i18n } from '@osd/i18n' ; import { ExpressionFunctionDefinition , ExpressionValueBoxed , OpenSearchDashboardsDatatable , OpenSearchDashboardsDatatableColumn , } from '../../../expressions/public' ; interface Arguments { accessor : string | number ; format ? : string ; formatParams ? : string ; } type ExpressionValueVisDimension = ExpressionValueBoxed < 'vis_dimension' , { accessor : number | OpenSearchDashboardsDatatableColumn ; format : { id ? : string ; params : unknown ; } ; } > ; export const visDimension = ( ) : ExpressionFunctionDefinition < 'visdimension' , OpenSearchDashboardsDatatable , Arguments , ExpressionValueVisDimension > => ( { name : 'visdimension' , help : i18n . translate ( 'visualizations.function.visDimension.help' , { defaultMessage : 'Generates visConfig dimension object' , } ) , type : 'vis_dimension' , inputTypes : [ 'opensearch_dashboards_datatable' ] , args : { accessor : { types : [ 'string' , 'number' ] , aliases : [ '_' ] , help : i18n . translate ( 'visualizations.function.visDimension.accessor.help' , { defaultMessage : 'Column in your dataset to use (either column index or column name)' , } ) , } , format : { types : [ 'string' ] , default : 'string' , help : i18n . translate ( 'visualizations.function.visDimension.format.help' , { defaultMessage : 'Format' , } ) , } , formatParams : { types : [ 'string' ] , default : '"{}"' , help : i18n . translate ( 'visualizations.function.visDimension.formatParams.help' , { defaultMessage : 'Format params' , } ) , } , } , fn : ( input , args ) => { const accessor = typeof args . accessor === 'number' ? args . accessor : input . columns . find ( ( c ) => c . id === args . accessor ) ; if ( accessor === undefined ) { throw new Error ( i18n . translate ( 'visualizations.function.visDimension.error.accessor' , { defaultMessage : 'Column name provided is invalid' , } ) ) ; } return { type : 'vis_dimension' , accessor , format : { id : args . format , params : JSON . parse ( args . formatParams ! ) , } , } ; } , } ) ;