import handleFindUrlDescriptor from './handlers/handleFindUrlDescriptor.js';
import log from './utils/log.js';

log.info(`backgroundScript.js loaded`);

/**
 * https://developer.chrome.com/docs/extensions/reference/runtime/#event-onMessage
 * 
 * must be sync
 * if returns true, chrome will wait for sendResponse
 * if returns falsey, chrome will ignore sendResponse
 */
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
  if (request?.type === 'findUrlDescriptor') {
    handleFindUrlDescriptor(request).then(response => {
      return sendResponse(response);
    });
    return true; /* wait for sendResponse call */
  }
});
