import { SavedObjectAttributes , SavedObjectReference } from 'opensearch-dashboards/public' ; import { SavedObjectDashboard } from './saved_dashboard' ; export function extractReferences ( { attributes , references = [ ] , } ) { const panelReferences = [ ] ; const panels = JSON . parse ( String ( attributes . panelsJSON ) ) ; panels . forEach ( ( panel , i ) => { if ( ! panel . type ) { throw new Error ( `"type" attribute is missing from panel "${ i }"` ) ; } if ( ! panel . id ) { return ; } panel . panelRefName = `panel_${ i }` ; panelReferences . push ( { name : `panel_${ i }` , type : panel . type , id : panel . id , } ) ; delete panel . type ; delete panel . id ; } ) ; return { references : [ ... references , ... panelReferences ] , attributes : { ... attributes , panelsJSON : JSON . stringify ( panels ) , } , } ; } export function injectReferences ( savedObject , references ) { if ( typeof savedObject . panelsJSON !== 'string' ) { return ; } const panels = JSON . parse ( savedObject . panelsJSON ) ; if ( ! Array . isArray ( panels ) ) { return ; } panels . forEach ( ( panel ) => { if ( ! panel . panelRefName ) { return ; } const reference = references . find ( ( ref ) => ref . name === panel . panelRefName ) ; if ( ! reference ) { throw new Error ( `Could not find reference "${ panel . panelRefName }"` ) ; } panel . id = reference . id ; panel . type = reference . type ; delete panel . panelRefName ; } ) ; savedObject . panelsJSON = JSON . stringify ( panels ) ; }