/* global wp, window */
const { select, dispatch, subscribe } = wp.data;

wp.domReady( () => {
    const html         = window.FRESHENUP_REVISION_HTML || '';
    const parsedBlocks = wp.blocks.parse( html );

    // Wait until core has loaded its own blocks, then replace once
    const unsubscribe = subscribe( () => {
        if ( select( 'core/block-editor' ).getBlocks().length ) {
            unsubscribe();                         // prevent loop

            // Run in next tick to escape Redux notify stack
            setTimeout( () => {
                dispatch( 'core/block-editor' ).resetBlocks( parsedBlocks );
            }, 0 );
        }
    } );
} );
