(function ($) {
	'use strict';

	var cfg = window.libmnsLmsPageLoader || {};
	var pages = cfg.pages || [];
	var readyClass = 'libmns-lms-pl-ready';
	var navClass = 'libmns-lms-pl-navigating';
	var screenClass = 'libmns-lms-pl-screen';
	var minVisibleMs = 280;
	var pageStart = Date.now();
	var hidePending = false;
	var hideTimer = null;

	function loaderEl() {
		return $('#libmns-lms-page-loader');
	}

	function setLoaderAria(show) {
		var $el = loaderEl();
		if (!$el.length) {
			return;
		}
		$el.attr('aria-hidden', show ? 'false' : 'true');
		$el.attr('aria-busy', show ? 'true' : 'false');
	}

	function hideLoader() {
		$('body').addClass(readyClass).removeClass(navClass);
		setLoaderAria(false);
		hidePending = false;
	}

	function showLoader() {
		if (hideTimer) {
			clearTimeout(hideTimer);
			hideTimer = null;
		}
		hidePending = false;
		$('body').removeClass(readyClass).addClass(navClass);
		setLoaderAria(true);
	}

	function scheduleHideLoader() {
		if (!$('body').hasClass(screenClass) || hidePending) {
			return;
		}
		hidePending = true;

		var elapsed = Date.now() - pageStart;
		var delay = Math.max(0, minVisibleMs - elapsed);

		if (hideTimer) {
			clearTimeout(hideTimer);
		}

		hideTimer = setTimeout(function () {
			hideTimer = null;
			requestAnimationFrame(function () {
				requestAnimationFrame(hideLoader);
			});
		}, delay);
	}

	function hrefTargetsLibraryAdmin(href) {
		if (!href || typeof href !== 'string') {
			return false;
		}
		var t = href.trim();
		if (!t || t === '#' || t.indexOf('javascript:') === 0 || t.indexOf('mailto:') === 0 || t.indexOf('tel:') === 0) {
			return false;
		}
		if (t.indexOf('admin.php') === -1 && t.indexOf('page=') === -1) {
			return false;
		}
		var m = /[?&]page=([^&]+)/.exec(t);
		if (!m) {
			return false;
		}
		var slug = decodeURIComponent(m[1].replace(/\+/g, ' '));
		return pages.indexOf(slug) !== -1;
	}

	$(function () {
		if (!$('body').hasClass('libmns-lms-pl-capable')) {
			return;
		}
		if ($('body').hasClass(screenClass)) {
			setLoaderAria(true);
			if (document.readyState === 'complete') {
				scheduleHideLoader();
			}
		} else {
			setLoaderAria(false);
		}
	});

	$(window).on('load', function () {
		if ($('body').hasClass('libmns-lms-pl-capable') && $('body').hasClass(screenClass)) {
			scheduleHideLoader();
		}
	});

	$(window).on('pageshow', function (e) {
		if (!e.originalEvent || !e.originalEvent.persisted) {
			return;
		}
		pageStart = Date.now();
		hidePending = false;
		if (hideTimer) {
			clearTimeout(hideTimer);
			hideTimer = null;
		}
		if ($('body').hasClass(screenClass)) {
			$('body').removeClass(readyClass).removeClass(navClass);
			setLoaderAria(true);
			scheduleHideLoader();
		}
	});

	$(document).on('click', 'a[href]', function (e) {
		if (!$('body').hasClass('libmns-lms-pl-capable')) {
			return;
		}
		var $a = $(this);
		if ($a.is('#wp-admin-bar-logout a, #user-logout a')) {
			return;
		}
		if ($a.attr('download') !== undefined) {
			return;
		}
		var target = $a.attr('target');
		if (target && target !== '' && target !== '_self') {
			return;
		}
		if (e.metaKey || e.ctrlKey || e.shiftKey || e.altKey) {
			return;
		}
		var href = $a.attr('href');
		if (!hrefTargetsLibraryAdmin(href)) {
			return;
		}
		showLoader();
	});

	$(document).on('submit', 'form', function (e) {
		if (!$('body').hasClass('libmns-lms-pl-capable') || !$('body').hasClass(screenClass)) {
			return;
		}
		var form = this;
		var action = (form.getAttribute('action') || '').trim();
		if (!action || action === '#' || action.indexOf('javascript:') === 0) {
			action = window.location.href;
		}
		if (!hrefTargetsLibraryAdmin(action)) {
			return;
		}
		setTimeout(function () {
			if (e.isDefaultPrevented()) {
				return;
			}
			showLoader();
		}, 0);
	});
})(jQuery);
