(function() {
	function onReady(callback) {
		if (document.readyState === 'loading') {
			document.addEventListener('DOMContentLoaded', callback);
			return;
		}

		callback();
	}

	function makeUpgradeLinkExternal() {
		var links = document.querySelectorAll('#adminmenu .sitefort-upgrade-nav-link');
		links.forEach(function(span) {
			var anchor = span.closest('a');
			if (!anchor) {
				return;
			}

			anchor.target = '_blank';
			anchor.rel = 'noopener noreferrer';
		});
	}

	function bindSetupNotice() {
		var notice = document.getElementById('sitefort-setup-notice');
		if (!notice || notice.getAttribute('data-sitefort-bound') === '1') {
			return;
		}

		var button = notice.querySelector('[data-sitefort-dismiss-notice]');
		var ajaxUrl = notice.getAttribute('data-ajax-url') || '';
		var nonce = notice.getAttribute('data-nonce') || '';
		if (!button || !ajaxUrl || !nonce || typeof window.fetch !== 'function' || typeof window.FormData === 'undefined') {
			return;
		}

		notice.setAttribute('data-sitefort-bound', '1');
		button.addEventListener('click', function() {
			var payload = new FormData();
			payload.append('action', 'sitefort_dismiss_setup_notice');
			payload.append('nonce', nonce);
			button.disabled = true;

			window.fetch(ajaxUrl, {
				method: 'POST',
				body: payload,
				credentials: 'same-origin'
			}).then(function(response) {
				if (!response.ok) {
					throw new Error('sitefort_notice_dismiss_failed');
				}

				return response.json();
			}).then(function(data) {
				if (!data || data.success !== true) {
					throw new Error('sitefort_notice_dismiss_rejected');
				}

				notice.remove();
			}).catch(function() {
				button.disabled = false;
			});
		});
	}

	function bindPlanNotice() {
		var notice = document.getElementById('sitefort-plan-notice');
		if (!notice || notice.getAttribute('data-sitefort-bound') === '1') {
			return;
		}

		var button = notice.querySelector('[data-sitefort-dismiss-plan-notice]');
		var ajaxUrl = notice.getAttribute('data-ajax-url') || '';
		var nonce = notice.getAttribute('data-nonce') || '';
		if (!button || !ajaxUrl || !nonce || typeof window.fetch !== 'function' || typeof window.FormData === 'undefined') {
			return;
		}

		notice.setAttribute('data-sitefort-bound', '1');
		button.addEventListener('click', function() {
			var payload = new FormData();
			payload.append('action', 'sitefort_dismiss_plan_notice');
			payload.append('_ajax_nonce', nonce);
			button.disabled = true;

			window.fetch(ajaxUrl, {
				method: 'POST',
				body: payload,
				credentials: 'same-origin'
			}).then(function(response) {
				if (!response.ok) {
					throw new Error('sitefort_plan_notice_dismiss_failed');
				}

				return response.json();
			}).then(function(data) {
				if (!data || data.success !== true) {
					throw new Error('sitefort_plan_notice_dismiss_rejected');
				}

				notice.remove();
			}).catch(function() {
				button.disabled = false;
			});
		});
	}

	function bindConsoleGateNotice() {
		var notice = document.getElementById('sitefort-console-gate-pending-notice');
		if (!notice || notice.getAttribute('data-sitefort-bound') === '1') {
			return;
		}

		var button = notice.querySelector('[data-sitefort-dismiss-console-gate-notice]');
		var ajaxUrl = notice.getAttribute('data-ajax-url') || '';
		var nonce = notice.getAttribute('data-nonce') || '';
		if (!button || !ajaxUrl || !nonce || typeof window.fetch !== 'function' || typeof window.FormData === 'undefined') {
			return;
		}

		notice.setAttribute('data-sitefort-bound', '1');
		button.addEventListener('click', function() {
			var payload = new FormData();
			payload.append('action', 'sitefort_dismiss_console_gate_notice');
			payload.append('_ajax_nonce', nonce);
			button.disabled = true;

			window.fetch(ajaxUrl, {
				method: 'POST',
				body: payload,
				credentials: 'same-origin'
			}).then(function(response) {
				if (!response.ok) {
					throw new Error('sitefort_console_gate_notice_dismiss_failed');
				}

				return response.json();
			}).then(function(data) {
				if (!data || data.success !== true) {
					throw new Error('sitefort_console_gate_notice_dismiss_rejected');
				}

				notice.remove();
			}).catch(function() {
				button.disabled = false;
			});
		});
	}

	onReady(function() {
		makeUpgradeLinkExternal();
		bindSetupNotice();
		bindPlanNotice();
		bindConsoleGateNotice();
	});
}());
