(function ($) {
	'use strict';

	$(function () {
		var config = window.wppsMenuOrganizerCustomLinks || {};
		var links = config.links || {};

		Object.keys(links).forEach(function (slug) {
			var entry = links[slug];
			var url = typeof entry === 'string' ? entry : entry.url;
			var openNewTab = typeof entry === 'object' && entry.openNewTab;

			if (!slug || !url) {
				return;
			}

			var selector =
				'#adminmenu a[href*="page=' + slug + '"],' +
				'#adminmenu .wp-submenu a[href*="page=' + slug + '"]';

			$(selector).each(function () {
				var $link = $(this);
				$link.attr('href', url);
				if (openNewTab) {
					$link.attr('target', '_blank').attr('rel', 'noopener noreferrer');
				}
			});
		});
	});
})(jQuery);
