(function ($) {
	'use strict';

	$(function () {
		var config = window.wppsMenuOrganizerApplied || {};
		var openNewTabSlugs = config.openNewTabSlugs || [];
		var adminPageBase = config.adminPageBase || '';

		if (!openNewTabSlugs.length) {
			return;
		}

		function maybeOpenInNewTab($link) {
			var href = $link.attr('href') || '';
			if (!href) {
				return;
			}

			openNewTabSlugs.some(function (slug) {
				if (!slug) {
					return false;
				}
				if (href.indexOf(slug) !== -1 || href === adminPageBase + slug) {
					$link.attr('target', '_blank').attr('rel', 'noopener noreferrer');
					return true;
				}
				return false;
			});
		}

		$('#adminmenu a').each(function () {
			maybeOpenInNewTab($(this));
		});

		$('#adminmenu .wp-submenu a').each(function () {
			maybeOpenInNewTab($(this));
		});
	});
})(jQuery);
