// Shows the free shipping notice on the block-based Checkout, which can't receive it the
// same way as the classic checkout.

const NOTICE_ID = 'esraw-free-shipping';
const CONTEXT = 'wc/checkout';

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

let lastMessage = null;

const sync = () => {
	const cart = select( 'wc/store/cart' ).getCartData();
	const data = cart && cart.extensions ? cart.extensions[ 'easy-shipping-rate' ] : null;
	const message = data && data.show ? data.message : null;

	if ( message === lastMessage ) {
		return;
	}
	lastMessage = message;

	const notices = dispatch( 'core/notices' );
	notices.removeNotice( NOTICE_ID, CONTEXT );
	if ( message ) {
		notices.createNotice( 'info', message, {
			id: NOTICE_ID,
			context: CONTEXT,
			isDismissible: false,
		} );
	}
};

subscribe( sync );
sync();
