/* Minification failed. Returning unminified contents.
(20,41-42): run-time error JS1195: Expected expression: .
(20,60-61): run-time error JS1195: Expected expression: .
(24,2-3): run-time error JS1002: Syntax error: }
(25,59-60): run-time error JS1004: Expected ';': {
(26,40-41): run-time error JS1195: Expected expression: .
(39,8-9): run-time error JS1014: Invalid character: `
(39,14-15): run-time error JS1193: Expected ',' or ')': :
(40,19-20): run-time error JS1195: Expected expression: >
(40,36-37): run-time error JS1004: Expected ';': )
(41,24-25): run-time error JS1004: Expected ';': {
(42,27-28): run-time error JS1195: Expected expression: .
(44,19-20): run-time error JS1195: Expected expression: .
(44,79-80): run-time error JS1197: Too many errors. The file might not be a JavaScript file: )
(33,3-9): run-time error JS1018: 'return' statement outside of function: return
 */
(function(window, undefined) {
	//  Only perform check from authorized domains
	//  and for clients that support fetch()
	if(!window.fetch || (window.location.hostname.indexOf('trb.org') === -1))
		return;

	//  Determine if there are any sciencedirect.com record URIs for this record
	var sdUri = document.querySelector('.record-info > li > ul > a[href*="sciencedirect.com/"]');

	if(!sdUri) //no sciencedirect uris found
		return;

	//  Find an identifier accepted by the Eslevier Entitlement API to pass
	var apiArgs = { idType: '', id: '' };

	//  Try DOI first
	var doiUri = document.querySelector('.record-info > li > ul > a[href*="doi.org/"]');

	if(doiUri) {
		var doi = doiUri.getAttribute('href')?.split('doi.org/')?.[1];

		apiArgs.idType = 'doi';
		apiArgs.id = doi;
	}
	else if(sdUri.getAttribute('href').indexOf('/pii/') > 0) { //try Publication Item Identifier (pii)
		var pii = sdUri.getAttribute('href')?.split('/pii/')?.[1];

		apiArgs.idType = 'pii';
		apiArgs.id = pii;
	}

	if(!apiArgs.id)
		return;

	//  Call Elsevier Entitlement api to determine access
	//  No error handler here; we want this to fail silently as it's
	//  not important for the display of the record or the running
	//  of the application
	fetch(`https://api.elsevier.com/content/article/entitlement/${apiArgs.idType}/${apiArgs.id}?APIKey=0a041792960d258f6d9ef95e7fef3ccf`, { mode: 'cors' })
		.then(response => response.json())
		.then(function(data) {
			var entitlement = data?.['entitlement-response']?.['document-entitlement'];

			if(entitlement?.entitled && entitlement?.link && entitlement?.link['@href']) {
				var uri = entitlement?.link['@href'];
				var flagsContainer = document.querySelector('#publication-flags-container');
				var mediaInfoContainer = document.querySelector('#media-info-container');

				//  Insert markup into record title
				var s = document.createElement('span');
				s.innerHTML = `<abbr title="The Elsevier Science Direct Full Text Entitlement API has determined that you have access to the full text associated with this record." class="pubicon">
									<i class="icon-elsevier"> </i>
								</abbr>`;
				document.querySelector('#record-title')?.prepend(s);

				//  Insert markup into record media info
				var li = document.createElement('li');
				li.innerHTML = (!flagsContainer ? `<li>
					<strong>Publication flags: </strong>` : '') +
					`<p class="listing">
						<abbr class="pubicon" title="The Elsevier Science Direct Full Text Entitlement API has determined that you have access to the full text associated with this record.">
							<i class="icon-elsevier"> </i>
							<a href="${uri}">You have access to this record's full text</a>
						</abbr>
					</p>` +
					(flagsContainer ? '</li>' : '');

				if(flagsContainer)
					flagsContainer?.append(li);
				else
					mediaInfoContainer?.append(li);
			}
		})
		.catch((ex) => console.log('Error from api.elsevier.com: ' + ex?.message));
})(window);;
