import { useState } from 'react' ; import { XJsonMode } from '@kbn/ace' ; import { StepDefineExposedState } from '../common' ; import { XJson } from '../../../../../../../../../../src/plugins/es_ui_shared/public' ; const { useXJsonMode } = XJson ; const xJsonMode = new XJsonMode ( ) ; export const useAdvancedRuntimeMappingsEditor = ( defaults ) => { const stringifiedRuntimeMappings = JSON . stringify ( defaults . runtimeMappings , null , 2 ) ; const [ runtimeMappingsUpdated , setRuntimeMappingsUpdated ] = useState ( defaults . runtimeMappingsUpdated ) ; const [ runtimeMappings , setRuntimeMappings ] = useState ( defaults . runtimeMappings ) ; const [ isRuntimeMappingsEditorSwitchModalVisible , setRuntimeMappingsEditorSwitchModalVisible ] = useState ( false ) ; const [ isRuntimeMappingsEditorEnabled , setRuntimeMappingsEditorEnabled ] = useState ( defaults . isRuntimeMappingsEditorEnabled ) ; const [ isRuntimeMappingsEditorApplyButtonEnabled , setRuntimeMappingsEditorApplyButtonEnabled ] = useState ( false ) ; const [ advancedEditorRuntimeMappingsLastApplied , setAdvancedEditorRuntimeMappingsLastApplied ] = useState ( stringifiedRuntimeMappings ) ; const { convertToJson , setXJson : setAdvancedRuntimeMappingsConfig , xJson : advancedRuntimeMappingsConfig , } = useXJsonMode ( stringifiedRuntimeMappings ?? '' ) ; const applyRuntimeMappingsEditorChanges = ( ) => { const parsedRuntimeMappings = advancedRuntimeMappingsConfig === '' ? { } : JSON . parse ( advancedRuntimeMappingsConfig ) ; const prettySourceConfig = JSON . stringify ( parsedRuntimeMappings , null , 2 ) ; setRuntimeMappingsUpdated ( true ) ; setRuntimeMappings ( parsedRuntimeMappings ) ; setAdvancedRuntimeMappingsConfig ( prettySourceConfig ) ; setAdvancedEditorRuntimeMappingsLastApplied ( prettySourceConfig ) ; setRuntimeMappingsEditorApplyButtonEnabled ( false ) ; } ; const toggleRuntimeMappingsEditor = ( reset = false ) => { if ( reset === true ) { setRuntimeMappingsUpdated ( false ) ; setAdvancedRuntimeMappingsConfig ( advancedEditorRuntimeMappingsLastApplied ) ; } setRuntimeMappingsEditorEnabled ( ! isRuntimeMappingsEditorEnabled ) ; setRuntimeMappingsEditorApplyButtonEnabled ( false ) ; } ; return { actions : { applyRuntimeMappingsEditorChanges , setRuntimeMappingsEditorApplyButtonEnabled , setRuntimeMappingsEditorEnabled , setAdvancedEditorRuntimeMappingsLastApplied , setRuntimeMappingsEditorSwitchModalVisible , setRuntimeMappingsUpdated , toggleRuntimeMappingsEditor , convertToJson , setAdvancedRuntimeMappingsConfig , } , state : { advancedEditorRuntimeMappingsLastApplied , isRuntimeMappingsEditorApplyButtonEnabled , isRuntimeMappingsEditorEnabled , isRuntimeMappingsEditorSwitchModalVisible , runtimeMappingsUpdated , advancedRuntimeMappingsConfig , xJsonMode , runtimeMappings , } , } ; } ;