/*
Establishes global mwDev object, measures page loading, constructs promise-like structure to make custom JS wait for mediawiki JS
See Dev/mediawiki for more info
deferrable:NO -- This is essential for the rest of the loading process
*/

/* jshint esversion: 8 */

// ----------
// 0. Globals

// tool - tool scripts for dev to use across all scripts, test - testing tools, data - general data to use across all scripts, log - global logging data, app - global data between scripts
window.mwDev = { tools: { test: {} }, data: { log: {}, app: {} } };

let urlObj = new URL(window.location);
window.mwDev.data.app.baseUrl = urlObj.origin + urlObj.pathname.replace('/wiki/Homepage','');

// ------------
// 1. Dev Tools

window.mwDev.tools.test.pageLoading = (function() {
	let events = [];
	return function( event ) {
		let now = new Date();
		events.push( [ event, now ] );
		console.info( 'mwDev.tools.test.pageLoading : event "' + event + '" at ' + now.toLocaleTimeString('en-GB', {
			hour: '2-digit', minute: '2-digit', second: '2-digit', fractionalSecondDigits:3,
		}));
		for( var i = 0; i < events.length - 1; i++ ) {
			// output the diff to all previous events
			console.info( 'mwDev.tools.test.pageLoading : diff (' + event + ',' + events[i][0] + ') = ' + (now.valueOf() - events[i][1]) + ' ms'  );
		}
	};
})();

// Pagespeed measurement
mwDev.tools.test.pageLoading('pageLoadStart');

// ---------
// 2. All JS
// This is available after Common.js is loaded

function triggerWhenReady() {
	// URL parameter to delay page loading
	if( (new URLSearchParams(window.location.search)).get('delayedPageLoad') ) {
		document.body.style.display = 'none';
		setTimeout( function() {
			document.body.style.display = 'block';
		}, 20000 );
	}
	
	// Setup for controlled deactivation of isolated scripts
	let dontloadString = new URLSearchParams(window.location.search).get("dontload");
	mwDev.data.log.jsload = {};
	if( dontloadString && typeof dontloadString == "string" ) {
		dontloadString.split(",").forEach( function(el) {
			mwDev.data.log.jsload[ "MediaWiki:" + el + ".js" ] = false;
		});
	}
/*WRAPPEDCONTENT*/	

	// Hash Control initial trigger
	mwDev.tools.hashControl.trigger( 'initial' );

	// Log completed Page Loading
	mwDev.tools.test.pageLoading('pageLoadFinished');
}

if( window.mediaWikiCommonJsIsLoaded ) {
	triggerWhenReady();
} else {
	window.addEventListener( 'mediaWikiCommonJsIsLoaded', triggerWhenReady );
}

/*
[[Category:MultiWiki]]
*/