// Utilities for scraping data from the Lodestone
import {regExpEscape} from '../util/regexp.js';
/**
* Creates a regular expression that matches a link to the named item and
* captures its EDB ID from the matched link's `href` attribute.
* @param {string} name
* @returns {RegExp}
*/
const itemLinkRegExp = name => new RegExp(`]*>(?${regExpEscape(name)})`, 'i');
/**
* Gets the ID of the named item in Eorzea Database.
* @param {string} name
* @returns {Promise}
*/
export async function findItemEDBID (name) {
// execute a search for the item's name
const searchURL = `https://na.finalfantasyxiv.com/lodestone/playguide/db/item/?q=${encodeURIComponent(name.replace(/\([^)]+\)|[&-]/g, ' '))}`;
const response = await fetch(searchURL);
const body = await response.text();
// find an `` in the HTML response whose text exactly matches the name
const match = body.match(itemLinkRegExp(name));
// return the ID parsed from the URL in the `href` attribute
return match?.groups.id;
}