define([
    'jquery',
    'mage/adminhtml/grid'
], function ($) {
    'use strict';

    return function (config, element) {
        var $element = $(element);
        
        // Add timeout to prevent infinite loading
        var loadTimeout = setTimeout(function() {
            $element.find('.loading-mask').hide();
            console.error('Grid loading timeout - check server response');
        }, 30000); // 30 second timeout

        // Clear timeout when grid loads successfully
        $element.on('grid:loaded', function() {
            clearTimeout(loadTimeout);
        });

        // Handle AJAX errors
        $(document).ajaxError(function(event, xhr, settings) {
            if (settings.url && settings.url.indexOf('disableddates') !== -1) {
                clearTimeout(loadTimeout);
                $element.find('.loading-mask').hide();
                console.error('Grid AJAX error:', xhr.responseText);
            }
        });
    };
});
