function appendCollectionsQueryParam(tileUrlTemplate, collections) {
  if (!collections.length) {
    return tileUrlTemplate;
  }

  // making sure we can always construct a URL instance.
  const url = new URL(tileUrlTemplate, 'file:/');

  if (url.pathname.split('/').includes('collections')) {
    logError(
      'The "collections" query parameter cannot be added to collection endpoints',
    );
    return tileUrlTemplate;
  }
  // According to conformance class
  // http://www.opengis.net/spec/ogcapi-tiles-1/1.0/conf/collections-selection
  // commata in the identifiers of the `collections` query parameter
  // need to be URLEncoded, while the commata separating the identifiers
  // should not.
  const encodedCollections = collections
    .map((c) => encodeURIComponent(c))
    .join(',');

  url.searchParams.append('collections', encodedCollections);
  const baseUrl = tileUrlTemplate.split('?')[0];
  const queryParams = decodeURIComponent(url.searchParams.toString());
  return `${baseUrl}?${queryParams}`;
}
@param {string} tileUrlTemplate Tile URL template.
@param {Array<string>} collections List of collections to include as query parameter.
@return {string} The tile URL template with appended collections query parameter.