function lookupLocale(locales, possibleLocales) {
  // TODO: lookupLocale may need to be split into two functions, one that canonicalizes
  // locales and one that looks up the best locale filename for a given locale.
  // e.g. `en-IE` is canonical, but uses `en-GB.json`. See TODO in locales.js

  if (typeof Intl !== 'object') {
    // If Node was built with `--with-intl=none`, `Intl` won't exist.
    throw new Error('Lighthouse must be run in Node with `Intl` support. See https://nodejs.org/api/intl.html for help');
  }

  const canonicalLocales = Intl.getCanonicalLocales(locales);

  // Filter by what's available in this runtime.
  const availableLocales = Intl.NumberFormat.supportedLocalesOf(canonicalLocales);

  // Get available locales and transform into object to match `lookupClosestLocale`'s API.
  const localesWithMessages = possibleLocales || getAvailableLocales();
  const localesWithmessagesObj = /** @type {Record<LH.Locale, LhlMessages>} */ (
    Object.fromEntries(localesWithMessages.map(l => [l, {}])));

  const closestLocale = lookupClosestLocale(availableLocales, localesWithmessagesObj);

  if (!closestLocale) {
    // Log extra info if we're pretty sure this version of Node was built with `--with-intl=small-icu`.
    if (Intl.NumberFormat.supportedLocalesOf('es').length === 0) {
      log.warn('i18n', 'Requested locale not available in this version of node. The `full-icu` npm module can provide additional locales. For help, see https://github.com/GoogleChrome/lighthouse/blob/main/readme.md#how-do-i-get-localized-lighthouse-results-via-the-cli');
    }
    // eslint-disable-next-line max-len
    log.warn('i18n', `locale(s) '${locales}' not available. Falling back to default '${DEFAULT_LOCALE}'`);
  }

  return closestLocale || DEFAULT_LOCALE;
}
Look up the best available locale for the requested language through these fall backs:
- exact match
- progressively shorter prefixes (`de-CH-1996` -> `de-CH` -> `de`)
- supported locales in Intl formatters

If `locale` isn't provided or one could not be found, DEFAULT_LOCALE is returned.

By default any of the locales Lighthouse has strings for can be returned, but this
can be overridden with `possibleLocales`, useful e.g. when Lighthouse is bundled and
only DEFAULT_LOCALE is available, but `possibleLocales` can be used to select a
locale available to be downloaded on demand.
@param {string|string[]=} locales
@param {Array<string>=} possibleLocales
@return {LH.Locale}