/**
 * Provide block-editor mediaUpload settings so MediaUploadCheck / MediaUpload
 * work in the admin SPA (outside the post editor).
 */
import { BlockEditorProvider } from '@wordpress/block-editor';
import { uploadMedia } from '@wordpress/media-utils';

const SETTINGS = {
	mediaUpload: ( { onError, ...rest } ) => {
		uploadMedia( {
			...rest,
			onError: ( error ) => {
				// eslint-disable-next-line no-console
				console.error( error );
				if ( typeof onError === 'function' ) {
					onError( error );
				}
			},
		} );
	},
};

export default function AdminMediaProvider( { children } ) {
	return (
		<BlockEditorProvider
			value={ [] }
			onInput={ () => {} }
			onChange={ () => {} }
			settings={ SETTINGS }
		>
			{ children }
		</BlockEditorProvider>
	);
}
