import { extractDomain } from "./urlHelper.js";
//#region src/components/pisellUrl/utils/urlFormatter.ts
/**
* URL 格式化工具函数
*/
/**
* 格式化 URL 显示
* @param url URL 地址
* @param options 格式化选项
* @returns 格式化后的 URL
* 
* @example
* formatUrlDisplay('https://www.example.com/path', { showProtocol: false })
* // 'www.example.com/path'
*/
function formatUrlDisplay(url, options) {
	if (!url) return "";
	if (options.displayFormat === "custom" && options.customFormat) return options.customFormat(url);
	if (options.displayFormat === "domain") return extractDomain(url);
	let display = url;
	if (options.showProtocol === false) display = display.replace(/^https?:\/\//, "");
	if (options.showWww === false) display = display.replace(/^(https?:\/\/)?(www\.)/, "$1");
	if (options.showPathname === false) try {
		const urlObj = new URL(url.startsWith("http") ? url : "https://" + url);
		display = urlObj.hostname;
		if (options.showProtocol !== false && urlObj.protocol) display = urlObj.protocol + "//" + display;
	} catch (_unused) {}
	return display;
}
//#endregion
export { formatUrlDisplay };
