/**
 * Shared SSE completion handler for download/extraction streams.
 *
 * Returns true if the stream is complete (error or success), false otherwise.
 * The caller should reset its own controller reference when true is returned.
 *
 * @param {Object} data - Parsed SSE event data
 * @param {Function} onSuccess - Called (with data) on successful completion
 * @returns {boolean} Whether the stream completed
 */
function handleSSECompletion(data, onSuccess) {
    if (!data.complete) return false;

    // closeProgressModal is page-specific — defined inline in
    // download_manager.html and library.html (the only callers of this
    // utility). Look it up via window so a caller from a different page
    // doesn't crash, and so eslint doesn't flag it as undefined.
    const closeModalIfDefined = () => {
        if (typeof window.closeProgressModal === 'function') {
            window.closeProgressModal();
        }
    };

    if (data.error) {
        setTimeout(() => {
            closeModalIfDefined();
            alert(data.error);
        }, 1000);
    } else {
        setTimeout(() => {
            closeModalIfDefined();
            onSuccess(data);
        }, 2000);
    }
    return true;
}
