//#region src/components/pisellPhone/utils/phoneFormatter.ts
/**
* 电话号码格式化工具
*/
/**
* 中国大陆手机号格式化：138 0000 0000
*/
function formatChinaMobile(phone) {
	const cleaned = phone.replace(/\D/g, "");
	if (cleaned.length <= 3) return cleaned;
	if (cleaned.length <= 7) return `${cleaned.slice(0, 3)} ${cleaned.slice(3)}`;
	return `${cleaned.slice(0, 3)} ${cleaned.slice(3, 7)} ${cleaned.slice(7, 11)}`;
}
/**
* 美国/加拿大号码格式化：(555) 123-4567
*/
function formatUSPhone(phone) {
	const cleaned = phone.replace(/\D/g, "");
	if (cleaned.length <= 3) return cleaned;
	if (cleaned.length <= 6) return `(${cleaned.slice(0, 3)}) ${cleaned.slice(3)}`;
	return `(${cleaned.slice(0, 3)}) ${cleaned.slice(3, 6)}-${cleaned.slice(6, 10)}`;
}
/**
* 英国号码格式化：1234 567 8900
*/
function formatUKPhone(phone) {
	const cleaned = phone.replace(/\D/g, "");
	if (cleaned.length <= 4) return cleaned;
	if (cleaned.length <= 7) return `${cleaned.slice(0, 4)} ${cleaned.slice(4)}`;
	return `${cleaned.slice(0, 4)} ${cleaned.slice(4, 7)} ${cleaned.slice(7, 11)}`;
}
/**
* 根据国家码格式化电话号码
* @param phone 电话号码（纯数字）
* @param countryCode 国家码
* @returns 格式化后的电话号码
*/
function formatPhone(phone, countryCode) {
	const cleaned = phone.replace(/\D/g, "");
	switch (countryCode) {
		case "+86": return formatChinaMobile(cleaned);
		case "+1": return formatUSPhone(cleaned);
		case "+44": return formatUKPhone(cleaned);
		default: return cleaned;
	}
}
/**
* 电话号码脱敏
* @param phone 电话号码
* @param pattern 脱敏模式
* @returns 脱敏后的号码
* 
* @example
* maskPhone('13800000000', 'middle') // '138****0000'
* maskPhone('13800000000', 'last')   // '1380000****'
*/
function maskPhone(phone, pattern = "middle") {
	const cleaned = phone.replace(/\D/g, "");
	if (cleaned.length < 7) return cleaned;
	switch (pattern) {
		case "middle":
			if (cleaned.length === 11) return cleaned.replace(/(\d{3})\d{4}(\d{4})/, "$1****$2");
			return cleaned;
		case "last":
			if (cleaned.length >= 7) return cleaned.slice(0, -4) + "****";
			return cleaned;
		default: return cleaned;
	}
}
//#endregion
export { formatPhone, maskPhone };
