import { isEmpty } from 'lodash' ; import { CustomLinkES , CustomLink , Filter , FilterKey , } from '../../../../common/custom_link/custom_link_types' ; export function fromESFormat ( customLinkES ) { const { id , label , url , '@timestamp' : timestamp , ... filters } = customLinkES ; return { id , '@timestamp' : timestamp , label , url , filters : Object . entries ( filters ) . map ( ( [ key , value ] ) => ( { key : key as FilterKey , value : isEmpty ( value ) ? '' : value . join ( ) , } ) ) , } ; } export function toESFormat ( customLink ) { const { label , url , filters = [ ] } = customLink ; const ESFilters = filters . filter ( ( { key , value } ) => key && value ) . reduce ( ( acc , { key , value } ) => { acc [ key ] = splitFilterValueByComma ( value ) ; return acc ; } , { } ) ; return { label , url , ... ESFilters } ; } export function splitFilterValueByComma ( filterValue ) { return filterValue . split ( ',' ) . map ( ( v ) => v . trim ( ) ) . filter ( ( v ) => v ) ; }