/**
 * Modern Message Box: Global alert/confirm/prompt wrappers
 *
 * Loads after modern-messagebox.js to ensure MsgBox exists. Replaces native
 * alert() with the branded message box while keeping confirm() and prompt()
 * functional for legacy synchronous code. Provides async helpers so modern
 * flows can adopt the new API without touching native methods.
 */

// Preserve original browser dialogs so we can fall back when needed
window._originalAlert = window.alert;
window._originalConfirm = window.confirm;
window._originalPrompt = window.prompt;

// Override alert() to render via Modern Message Box but stay non-blocking
window.alert = function(message) {
    setTimeout(function() {
        if (window.MsgBox && typeof window.MsgBox.alert === 'function') {
            window.MsgBox.alert(String(message), 'info');
        } else if (typeof window._originalAlert === 'function') {
            window._originalAlert(String(message));
        }
    }, 0);
};

// Keep confirm()/prompt() synchronous by default. These wrappers warn so
// legacy code can be migrated gradually to the async helpers below.
/*
window.confirm = function(message) {
    console.warn('Legacy confirm() invoked. Prefer modernConfirm() for async flows.');
    return typeof window._originalConfirm === 'function'
        ? window._originalConfirm(message)
        : true;
};
*/

window.prompt = function(message, defaultValue) {
    console.warn('Legacy prompt() invoked. Prefer modernPrompt() for async flows.');
    return typeof window._originalPrompt === 'function'
        ? window._originalPrompt(message, defaultValue)
        : null;
};

// Async helpers expose the full Modern Message Box capabilities
window.modernAlert = async function(message, type) {
    if (window.MsgBox && typeof window.MsgBox.alert === 'function') {
        return window.MsgBox.alert(message, type || 'info');
    }
    return Promise.resolve();
};

window.modernConfirm = async function(message, options) {
    if (window.MsgBox && typeof window.MsgBox.confirm === 'function') {
        return window.MsgBox.confirm(message, options || {});
    }
    // Fallback to original confirm if MsgBox is not available
    return Promise.resolve(Boolean(window._originalConfirm(message)));
};

window.modernPrompt = async function(message, defaultValue, placeholder) {
    if (window.MsgBox && typeof window.MsgBox.prompt === 'function') {
        return window.MsgBox.prompt(message, defaultValue || '', placeholder || '');
    }
    return Promise.resolve(window.prompt(message, defaultValue));
};
