//#region src/components/pisellUrl/utils/urlHelper.ts
/**
* URL 辅助函数
*/
/**
* 规范化 URL（补全协议）
* @param url URL 地址
* @param defaultProtocol 默认协议
* @returns 规范化后的 URL
* 
* @example
* normalizeUrl('example.com', 'https://') // 'https://example.com'
* normalizeUrl('http://example.com', 'https://') // 'http://example.com'
*/
function normalizeUrl(url, defaultProtocol = "https://") {
	if (!url) return "";
	const trimmedUrl = url.trim();
	if (/^[a-z]+:\/\//i.test(trimmedUrl)) return trimmedUrl;
	return defaultProtocol + trimmedUrl;
}
/**
* 提取域名
* @param url URL 地址
* @returns 域名
* 
* @example
* extractDomain('https://www.example.com/path') // 'www.example.com'
*/
function extractDomain(url) {
	try {
		const normalizedUrl = url.startsWith("http") ? url : "https://" + url;
		return new URL(normalizedUrl).hostname;
	} catch (_unused) {
		return url;
	}
}
/**
* 处理链接跳转（安全性保障）
* @param url URL 地址
* @param openInNewWindow 是否新窗口打开
* @param defaultProtocol 默认协议
* 
* @example
* handleUrlClick('example.com', true, 'https://')
*/
function handleUrlClick(url, openInNewWindow = true, defaultProtocol = "https://") {
	if (!url) return;
	const normalizedUrl = normalizeUrl(url, defaultProtocol);
	try {
		new URL(normalizedUrl);
	} catch (_unused2) {
		console.warn("Invalid URL:", normalizedUrl);
		return;
	}
	if (openInNewWindow) window.open(normalizedUrl, "_blank", "noopener,noreferrer");
	else window.location.href = normalizedUrl;
}
//#endregion
exports.extractDomain = extractDomain;
exports.handleUrlClick = handleUrlClick;
exports.normalizeUrl = normalizeUrl;
