const httpRequest = require('./httpRequest');
const fs = require('fs');
const cultureCodes = JSON.parse(fs.readFileSync('./data/language/cultureCodes.json'));

/**
 * @summary Scan the url
 * @param {string} url - the url to scan
 * @returns {Object} the result
 */
exports.scanUrl = async (url) => {
  const html = await httpRequest.execute(url);
  return this.scanHTML(html);
}

/**
 * @summary Scan the HTML
 * @param {string} html - the html to scan
 * @returns {Object} the result
 */
exports.scanHTML = (html) => {
  const result = {};
  result.hrefLangs = this.scanHrefLangs(html);
  result.internationalLinks = this.scanLinks(html);
  return result;
}

/**
 * @typedef {Object} hrefLangResult
 * @property {string} lang - the hreflang value
 * @property {string} href - the href value
 */

/**
 * @summary Scan hrefLang in the HTML
 * @param {string} html - the html to scan
 * @returns {hrefLangResult[]} the hrefLang presents in the HTML
 */
exports.scanHrefLangs = (html) => {
  const hreflangs = html.match(/<link[^>]+hreflang=[^>]+>/gi);
  if (!hreflangs) {
    return [];
  }

  const result = hreflangs.map((regexResult) => {
    const langResult = regexResult.match(/hreflang="([^"]+)"/i);
    const lang = langResult ? langResult[1] : null;
    const hrefResult = regexResult.match(/href="([^"]+)"/i);
    const href = hrefResult ? hrefResult[1] : null;
    return {
      lang,
      href
    }
  })

  return result;
}

/**
 * @summary Scan international links in the HTML
 * @param {string} html - the html to scan
 * @returns {hrefLangResult[]} the international links presents in the HTML
 */
exports.scanLinks = (html) => {
  const regexResults = html.match(/<a [^>]+>[^<]+<\/a>/gi);
  if (!regexResults) {
    return [];
  }

  // filter international links
  const links = regexResults.filter((result) => {
    const hrefResult = result.match(/href="([^"]+)"/i);
    if (!hrefResult) {
      return false;
    }

    const hrefValue = hrefResult[1];
    for (let i = 0; i < cultureCodes.length; i++) {
      const cultureCode = cultureCodes[i].code;
      const regex = new RegExp('\/' + cultureCode + '\/', 'i');
      if (hrefValue.match(regex)) {
        return true;
      }
    }
    return false;
  })

  // returns only the href value
  const result = links.map((link) => {
    return link.match(/href="([^"]+)"/i)[1];
  })

  return result;
}
@@ -153,16 +153,199 @@
 on'));%0A%0A
+/**%0A * @typedef %7BObject%7D languageScanResult%0A * @property %7BhrefLangResult%5B%5D%7D hrefLangs - the href lang result%0A * @property %7Bstring%5B%5D%7D internationalLinks - the international links%0A */%0A%0A
 /**%0A * @
@@ -411,37 +411,49 @@
 an%0A * @returns %7B
-Objec
+languageScanResul
 t%7D the result%0A *
@@ -655,21 +655,33 @@
 eturns %7B
-Objec
+languageScanResul
 t%7D the r
@@ -1744,38 +1744,30 @@
 * @returns %7B
-hrefLangResult
+string
 %5B%5D%7D the inte

Fix language scan JSDoc