<script>
(function() {
	if (window.rpcMenuLeaveConfirm) {
		return;
	}

	function normalizeSpace(text) {
		return String(text || '').replace(/\s+/g, ' ').trim();
	}

	function resolveLabel(anchorEl, fallback) {
		var txt = normalizeSpace(anchorEl && anchorEl.textContent ? anchorEl.textContent : '');
		if (/신규등록하기|신규등록/.test(txt)) {
			return '신규등록하기';
		}
		if (/목록보기|내상품관리/.test(txt)) {
			return '목록보기';
		}
		return fallback || '목록보기';
	}

	function buildMessage(label) {
		return "현재 입력 중인 내용이 저장되지 않았다면 이동 시 사라질 수 있습니다.\n'" + label + "' 페이지로 이동하시겠습니까?";
	}

	function canHandleAnchor(anchorEl) {
		if (!anchorEl) {
			return false;
		}

		var href = anchorEl.getAttribute('href') || '';
		if (!href || href === '#' || /^javascript:/i.test(href)) {
			return false;
		}

		var target = String(anchorEl.getAttribute('target') || '').toLowerCase();
		if (target === '_blank') {
			return false;
		}

		return true;
	}

	function navigate(anchorEl) {
		if (!anchorEl) {
			return;
		}
		var href = anchorEl.getAttribute('href') || '';
		if (!href || href === '#') {
			return;
		}
		window.location.href = href;
	}

	function showConfirm(message, onConfirm) {
		if (typeof window.showCopilotConfirm === 'function') {
			window.showCopilotConfirm(message, function() {
				if (typeof onConfirm === 'function') {
					onConfirm();
				}
			}, null, {
				title: '페이지 이동 확인',
				type: 'warn',
				okText: '이동하기',
				cancelText: '계속 입력'
			});
			return;
		}

		if (window.confirm(message) && typeof onConfirm === 'function') {
			onConfirm();
		}
	}

	function checkFormDirty() {
		if (typeof window.rpcIsFormDirty === 'function') {
			return window.rpcIsFormDirty();
		}
		return true;
	}

	function bindToAnchor(anchorEl, preferredLabel) {
		if (!anchorEl) {
			return;
		}

		if (anchorEl.dataset.leaveConfirmBound === '1') {
			return;
		}
		anchorEl.dataset.leaveConfirmBound = '1';

		anchorEl.addEventListener('click', function(e) {
			if (!canHandleAnchor(anchorEl)) {
				return;
			}

			if (!checkFormDirty()) {
				return;
			}

			e.preventDefault();
			e.stopPropagation();

			var label = resolveLabel(anchorEl, preferredLabel);
			showConfirm(buildMessage(label), function() {
				navigate(anchorEl);
			});
		});
	}

	function bindDocumentLinksByText(options) {
		var opts = options || {};
		var bindingKey = String(opts.bindingKey || 'default');
		var guardKey = '__rpcMenuLeaveConfirmDelegated__' + bindingKey;
		if (window[guardKey]) {
			return;
		}
		window[guardKey] = true;

		var textPattern = (opts.textPattern instanceof RegExp)
			? opts.textPattern
			: /목록보기|신규등록하기|신규등록/;

		function handler(anchorEl, eventObj) {
			if (!anchorEl) {
				return;
			}

			var text = normalizeSpace(anchorEl.textContent || '');
			if (!textPattern.test(text)) {
				return;
			}

			if (!canHandleAnchor(anchorEl)) {
				return;
			}

			if (!checkFormDirty()) {
				return;
			}

			eventObj.preventDefault();
			eventObj.stopPropagation();

			var label = /신규등록하기|신규등록/.test(text) ? '신규등록하기' : '목록보기';
			showConfirm(buildMessage(label), function() {
				navigate(anchorEl);
			});
		}

		if (typeof window.jQuery !== 'undefined') {
			window.jQuery(document).on('click', 'a', function(e) {
				handler(this, e);
			});
			return;
		}

		document.addEventListener('click', function(e) {
			var target = e.target;
			if (!target) {
				return;
			}
			var anchor = target.closest ? target.closest('a') : null;
			handler(anchor, e);
		}, true);
	}

	window.rpcMenuLeaveConfirm = {
		bindToAnchor: bindToAnchor,
		bindDocumentLinksByText: bindDocumentLinksByText,
		navigate: navigate
	};
})();
</script>
