// Helper to polyfill chrome.runtime.getURL in Main World for Dev mode
(function() {
  try {
    const currentScript = document.currentScript;
    const extensionId = currentScript?.dataset?.extensionId;
    
    if (extensionId) {
      window.chrome = window.chrome || {};
      window.chrome.runtime = window.chrome.runtime || {};
      
      if (!window.chrome.runtime.getURL) {
        window.chrome.runtime.getURL = function(path) {
          // Handle paths starting with / or not
          const prefix = `chrome-extension://${extensionId}`;
          if (!path) return prefix + '/'; // Ensure trailing slash for root
          return path.startsWith('/') ? prefix + path : prefix + '/' + path;
        };
      }

      // Polyfill chrome.runtime.connect to avoid errors with Vite HMR client
      if (!window.chrome.runtime.connect) {
        window.chrome.runtime.connect = function() {
          return {
            onMessage: {
              addListener: () => {},
              removeListener: () => {}
            },
            onDisconnect: {
              addListener: () => {},
              removeListener: () => {}
            },
            postMessage: () => {},
            disconnect: () => {}
          };
        };
      }

      // Polyfill chrome.runtime.id
      if (!window.chrome.runtime.id) {
        window.chrome.runtime.id = extensionId;
      }
    }
  } catch (e) {
    console.error('[DecoMaster] Failed to polyfill chrome.runtime.getURL', e);
  }
})();
