// This script runs in the context of the webpage to intercept requests
function generateTodaysData() {
  const data = {};
  const startDate = new Date();
  startDate.setDate(startDate.getDate() + 1); // Start from tomorrow
  
  // Generate data for 45 days starting from tomorrow
  for (let dayOffset = 0; dayOffset < 45; dayOffset++) {
    const currentDate = new Date(startDate);
    currentDate.setDate(startDate.getDate() + dayOffset);
    
    // Format date as YYYY-MM-DD
    const dateKey = currentDate.toISOString().split('T')[0];
    
    // Generate time slots from 07:00 to 18:00 with 30-minute intervals
    data[dateKey] = {};
    
    for (let hour = 7; hour <= 18; hour++) {
      // Add :00 slot
      const timeSlot00 = `${hour.toString().padStart(2, '0')}:00`;
      data[dateKey][timeSlot00] = 1;
      
      // Add :30 slot (except for 18:30 which would be beyond 18:00)
      if (hour < 18) {
        const timeSlot30 = `${hour.toString().padStart(2, '0')}:30`;
        data[dateKey][timeSlot30] = 1;
      }
    }
  }  
  return JSON.stringify(data);
}

(function() {
    console.log('TLSContact Interceptor script injected.');

    // Keep a reference to the original XMLHttpRequest
    const OriginalXMLHttpRequest = window.XMLHttpRequest;

    // Override the XMLHttpRequest
    window.XMLHttpRequest = function() {
        const xhr = new OriginalXMLHttpRequest();
        let url = '';
        let method = '';
        let intercept = false;

        // Store the URL and method when open is called
        const originalOpen = xhr.open;
        xhr.open = function(method_, url_) {
            url = url_;
            method = method_;
            // console.log(url)
            // Check if the URL includes "normal" and the method is GET
            if (method.toUpperCase() === 'GET' && (url.includes('normal') || url.includes('prime time&'))) {
                intercept = true;
                console.log('Intercepting request:', url);
            } else {
                intercept = false;
            }
            return originalOpen.apply(this, arguments);
        };

        // Intercept the send method if needed
        const originalSend = xhr.send;
        xhr.send = function() {
            if (intercept) {
                console.log('Intercepted send for URL:', url);

                // Prevent the original send from happening
                // We don't call originalSend.apply(this, arguments);
                const jsonData = generateTodaysData();

                // Manually trigger the onload event
                Object.defineProperty(xhr, 'responseText', { value: jsonData, writable: true });
                Object.defineProperty(xhr, 'response', { value: jsonData, writable: true });
                Object.defineProperty(xhr, 'status', { value: 200, writable: true });
                Object.defineProperty(xhr, 'readyState', { value: 4, writable: true });

                // Trigger the readystatechange and load events
                if (typeof xhr.onreadystatechange === 'function') {
                    xhr.onreadystatechange();
                }
                if (typeof xhr.onload === 'function') {
                    xhr.onload();
                }
                    // Also trigger loadend
                if (typeof xhr.onloadend === 'function') {
                    xhr.onloadend();
                }

            } else {
                // If not intercepting, proceed with the original send
                return originalSend.apply(this, arguments);
            }
        };

        return xhr;
    };
})();
