(function ($) {
	'use strict';

	$(function () {
		var $adminMenu = $('#adminmenu');
		if (!$adminMenu.length) {
			return;
		}

		var strings = window.wppsMenuOrganizerToggle || {};
		var $toggle = $adminMenu.find('.wpps-show-all-toggle > a .wp-menu-name');

		function getShowAllLabel() {
			return strings.showAll || 'Show All';
		}

		function getShowLessLabel() {
			return strings.showLess || 'Show Less';
		}

		if ($toggle.length) {
			$toggle.text(getShowAllLabel());
		}

		$adminMenu.on('click', '.wpps-show-all-toggle > a', function (e) {
			e.preventDefault();
			var expanded = $adminMenu.toggleClass('wpps-show-hidden').hasClass('wpps-show-hidden');

			if ($toggle.length) {
				$toggle.text(expanded ? getShowLessLabel() : getShowAllLabel());
			}

			try {
				localStorage.setItem('wpps_menu_show_hidden', expanded ? '1' : '0');
			} catch (err) {
				// Ignore storage errors.
			}

			$(document).trigger('wp-window-resized');
		});

		try {
			if (localStorage.getItem('wpps_menu_show_hidden') === '1') {
				$adminMenu.addClass('wpps-show-hidden');
				if ($toggle.length) {
					$toggle.text(getShowLessLabel());
				}
			}
		} catch (err) {
			// Ignore storage errors.
		}
	});
})(jQuery);
