// Import scraping tool
import { parse } from 'node-html-parser';

// Constants
const titleSelector = 'h2.Subhead-heading';
const descriptionSelector = 'div.markdown-body>p';

/**
 * Scrapes the GitHub advisory page for a CVE and returns the title and
 * description of the CVE.
 * @author Allison Zhang
 * @author Gabe Abrams
 * @param githubLink The URL of the GitHub advisory page for the CVE
 * @returns A promise that contains both the title and description of the CVE
 */
const scrapeCVE = async (githubLink: string): Promise<{
  title: string,
  description: string,
}> => {
  // Send request
  let response: Response;
  try {
    response = await fetch(githubLink);
  } catch (error) {
    return {
      title: 'N/A',
      description: 'N/A',
    };
  }
  
  // Get the body text
  const bodyText = await response.text();
  const root = parse(bodyText);

  // Get and trim the title text
  const heading = (
    root.querySelector(titleSelector)
      ?.textContent
      ?.trim()
  );

  // Get and trim the description text
  const description = (
    root.querySelector(descriptionSelector)
      ?.textContent
      ?.trim()
  );

  return {
    title: heading || 'N/A',
    description: description || 'N/A',
  };
};

export default scrapeCVE;
