/* quotes.js; Deven Blake 2021-2022 */
/* @license magnet:?xt=urn:btih:5ac446d35272cc2e4e85e4325b146d0b7ca8f50c&dn=unlicense.txt Unlicense */
/* With thanks to Ками on Discord. */

/* To use:
 * - add two elements with the IDs QUOTE_AUTHOR_ID (see code) and
 *   QUOTE_VALUE_ID (see code) respectively
 * - make a JSON file with a "quotes" object that's an array of arrays
 *   (see code)
 * - change window.QUOTES_FILE_LOCATION to wherever you put it
 * - window.quotes_initialize();
 * - use window.quote_new() to get a new quote. */

/* firefox doesn't support import assertions yet (2022-02-20; fx 97.0) */
window.quotes = [ ["Couldn't get quotes array.", "quotes.js"] ];
window.QUOTES_FILE_LOCATION = "/js/quotes.json";
window.QUOTES_L_QUOTE_MARK = '"';
window.QUOTES_R_QUOTE_MARK = '"';
window.QUOTES_QUOTE_AUTHOR_ID = "quote_author";
window.QUOTES_QUOTE_AUTHOR_PREFIX = "~ ";
window.QUOTES_QUOTE_VALUE_ID = "quote_value";

window.quotes_initialize = function(){
	/* Weird JavaScript web voodoo. */
	fetch(window.QUOTES_FILE_LOCATION)
		.then(response => {return response.json();})
		.then(data => window.quotes = data["quotes"]);
};

/* Will not give you the same quote twice in a row! */
window.quote_new = function(){
	window.quote_old = window.quote; /* The quote currently in use. */
	quote_index = Math.floor(Math.random() * window.quotes.length);
	window.quote = window.quotes.splice(quote_index, 1)[0];
	if(window.quote_old)
		window.quotes.push(window.quote_old);

	if(quote_text = document.getElementById(window.QUOTES_QUOTE_VALUE_ID))
		quote_text.textContent =
			window.QUOTES_L_QUOTE_MARK
			+ window.quote[0]
			+ window.QUOTES_R_QUOTE_MARK;

	if(quote_author = document.getElementById(
			window.QUOTES_QUOTE_AUTHOR_ID))
		quote_author.textContent = window.QUOTES_QUOTE_AUTHOR_PREFIX
			+ window.quote[1];
};

/* @license-end */
