_ _    _ _____  ___   __                       
 __      _(_) | _(_)___ / ( _ ) / /_   ___ ___  _ __ ___  
 \ \ /\ / / | |/ / | |_ \ / _ \| '_ \ / __/ _ \| '_ ` _ \ 
  \ V  V /| |   <| |___) | (_) | (_) | (_| (_) | | | | | |
   \_/\_/ |_|_|\_\_|____/ \___/ \___(_)___\___/|_| |_| |_|

MediaWiki:Gadget-ll-script-last-seen.js

W tym artykule będziemy odkrywać fascynujący świat MediaWiki:Gadget-ll-script-last-seen.js, który obejmuje szeroki zakres tematów i aspektów, od osobistych po społeczne. MediaWiki:Gadget-ll-script-last-seen.js był przedmiotem zainteresowania i analiz na przestrzeni dziejów, a jego wpływ był odczuwalny we wszystkich obszarach społeczeństwa. W tym artykule będziemy starali się lepiej zrozumieć MediaWiki:Gadget-ll-script-last-seen.js i jego znaczenie w naszym życiu, a także zbadać jego implikacje w różnych kontekstach. Od powstania do ewolucji MediaWiki:Gadget-ll-script-last-seen.js był przedmiotem debat i badań i mamy nadzieję rzucić światło na ten temat w sposób informacyjny i wnikliwy.
var api,
	lastSeenQuery = {
		list: ,
		usprop: 'gender',
		ucprop: 'timestamp',
		leprop: 'timestamp',
		uclimit: 1,
		lelimit: 1
	},
	millisPerDay = 60 * 60 * 24 * 1000,
	now = Date.now(),
	exports = {};

function makeOutput( outputType, latestTime, isActive, gender ) {
	var timestamp = new Date( latestTime ).toLocaleString( 'pl-PL', {
			dateStyle: 'medium',
			timeStyle: 'long'
		} ),
		daysElapsed = Math.floor( ( now - latestTime ) / millisPerDay );
	
	switch ( outputType ) {
		default:
		case 'timestamp':
			return timestamp;
		case 'status':
			return mw.msg( isActive ? 'last-seen-active' : 'last-seen-inactive', gender );
		case 'status-timestamp':
			return mw.msg( isActive ? 'last-seen-active-timestamp' : 'last-seen-inactive-timestamp', gender, timestamp );
		case 'days':
			return daysElapsed > 0
				? mw.msg( 'last-seen-days', daysElapsed )
				: mw.msg( 'last-seen-today' );
		case 'days-extended':
			return daysElapsed > 0
				? mw.msg( 'last-seen-days-extended', gender, daysElapsed )
				: mw.msg( 'last-seen-today-extended', gender );
	}
}

function updateOutput( $container, $placeholder, $active, $inactive, outputType, latestTime, refTime, gender ) {
	var isActive = latestTime >= refTime;
	
	if ( $container.length ) {
		$container.find( $placeholder ).text( makeOutput( outputType, latestTime, isActive, gender ) );
		
		if ( isActive ) {
			$container.find( $active ).show();
			$container.find( $inactive ).hide();
		} else {
			$container.find( $active ).hide();
			$container.find( $inactive ).show();
		}
	}
}

function processCallbackChain( $els ) {
	var $el = $els.first(),
		$contribs = $el.find( '.last-seen-contribs' ).addBack( '.last-seen-contribs' ).first(),
		$logs = $el.find( '.last-seen-logs' ).addBack( '.last-seen-logs' ).first(),
		$overall = $el.find( '.last-seen-overall' ).addBack( '.last-seen-overall' ).first(),
		$output = $el.find( '.last-seen-output' ).addBack( '.last-seen-output' ).first(),
		$loading = $el.find( '.last-seen-in-progress' ),
		$placeholder = $el.find( '.last-seen-placeholder' ),
		$active = $el.find( '.last-seen-output-active' ),
		$inactive = $el.find( '.last-seen-output-inactive' ),
		username = $el.data( 'username' ),
		outputType = $el.data( 'output' ),
		reference = $el.data( 'reference' ),
		refTime = typeof reference === 'string'
			? Date.parse( reference )
			: now - Number( reference ) * millisPerDay,
		query = $.extend( {}, lastSeenQuery, {
			ususers: username,
			ucuser: username,
			leuser: username
		} );
	
	$loading.show();
	$el.addClass( 'last-seen-loading' );
	
	api.get( query ).done( function ( res ) {
		var latestContribs, latestLogs, latestOverall, gender,
			user = res.query.users;
		
		if ( !user.missing ) {
			latestContribs = res.query.usercontribs.length ?
				Date.parse( res.query.usercontribs.timestamp ) :
				0;
			latestLogs = res.query.logevents.length ?
				Date.parse( res.query.logevents.timestamp ) :
				0;
			latestOverall = Math.max( latestContribs, latestLogs );
			gender = user.gender;
			
			updateOutput( $contribs, $placeholder, $active, $inactive, outputType, latestContribs, refTime, gender );
			updateOutput( $logs, $placeholder, $active, $inactive, outputType, latestLogs, refTime, gender );
			updateOutput( $overall, $placeholder, $active, $inactive, outputType, latestOverall, refTime, gender );
			
			$output.show();
			$loading.hide();
		} else {
			$loading.text( mw.msg( 'last-seen-missing' ) );
		}
	} ).fail( function ( code, data ) {
		$loading.text( mw.msg( 'last-seen-error' ) );
	} ).always( function () {
		$el.removeClass( 'last-seen-loading' );
		
		// https://stackoverflow.com/a/20979871/10404307
		$els.splice( 0, 1 );
		
		if ( $els.length ) {
			processCallbackChain( $els );
		}
	} );
}

exports.testPreconditions = $.noop;

exports.initialize = function () {
	mw.messages.set( {
		'last-seen-active': '{{GENDER:$1|aktywny|aktywna|aktywny/a}}',
		'last-seen-inactive': '{{GENDER:$1|nieaktywny|nieaktywna|nieaktywny/a}}',
		'last-seen-active-timestamp': '{{GENDER:$1|aktywny|aktywna|aktywny/a}} do $2',
		'last-seen-inactive-timestamp': '{{GENDER:$1|nieaktywny|nieaktywna|nieaktywny/a}} od $2',
		'last-seen-days': '$1 {{PLURAL:$1|dzień|dni}} temu',
		'last-seen-days-extended': 'ostatnio {{GENDER:$1|widziany|widziana|widziany/a}} $2 {{PLURAL:$2|dzień|dni}} temu',
		'last-seen-today': 'dzisiaj',
		'last-seen-today-extended': 'ostatnio {{GENDER:$1|widziany|widziana|widziany/a}} dzisiaj',
		'last-seen-missing': '???',
		'last-seen-error': 'błąd API'
	} );
	
	api = new mw.Api( { parameters: {
		formatversion: 2,
		maxage: 60 * 60 * 4, // 4 hours
		smaxage: 60 * 60 * 1 // 1 hour
	} } );
};

exports.filter = $.noop;

exports.process = function ( $els ) {
	processCallbackChain( $els );
};

module.exports = exports;