/**
 * Global unsaved-changes gate for admin navigation.
 */
import { __ } from '@wordpress/i18n';

let blocker = null;

/**
 * Register a leave checker. Return true to allow navigation.
 *
 * @param {null|(() => boolean)} next Next blocker.
 */
export function setUnsavedChangesBlocker( next ) {
	blocker = typeof next === 'function' ? next : null;
}

/**
 * Confirm leaving the current screen (WordPress-style).
 *
 * @return {boolean} Whether navigation may proceed.
 */
export function confirmLeave() {
	if ( typeof blocker !== 'function' ) {
		return true;
	}
	return blocker();
}

export function defaultLeaveMessage() {
	return __(
		'You have unsaved changes. If you proceed, you will lose your unsaved changes.',
		'joc-blockora-morph'
	);
}
