/**
 * 电话号码格式化工具
 */

/**
 * 中国大陆手机号格式化：138 0000 0000
 */
export function formatChinaMobile(phone) {
  var cleaned = phone.replace(/\D/g, '');
  if (cleaned.length <= 3) return cleaned;
  if (cleaned.length <= 7) {
    return "".concat(cleaned.slice(0, 3), " ").concat(cleaned.slice(3));
  }
  return "".concat(cleaned.slice(0, 3), " ").concat(cleaned.slice(3, 7), " ").concat(cleaned.slice(7, 11));
}

/**
 * 美国/加拿大号码格式化：(555) 123-4567
 */
export function formatUSPhone(phone) {
  var cleaned = phone.replace(/\D/g, '');
  if (cleaned.length <= 3) return cleaned;
  if (cleaned.length <= 6) {
    return "(".concat(cleaned.slice(0, 3), ") ").concat(cleaned.slice(3));
  }
  return "(".concat(cleaned.slice(0, 3), ") ").concat(cleaned.slice(3, 6), "-").concat(cleaned.slice(6, 10));
}

/**
 * 英国号码格式化：1234 567 8900
 */
export function formatUKPhone(phone) {
  var cleaned = phone.replace(/\D/g, '');
  if (cleaned.length <= 4) return cleaned;
  if (cleaned.length <= 7) {
    return "".concat(cleaned.slice(0, 4), " ").concat(cleaned.slice(4));
  }
  return "".concat(cleaned.slice(0, 4), " ").concat(cleaned.slice(4, 7), " ").concat(cleaned.slice(7, 11));
}

/**
 * 根据国家码格式化电话号码
 * @param phone 电话号码（纯数字）
 * @param countryCode 国家码
 * @returns 格式化后的电话号码
 */
export function formatPhone(phone, countryCode) {
  var 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****'
 */
export function maskPhone(phone) {
  var pattern = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'middle';
  var cleaned = phone.replace(/\D/g, '');
  if (cleaned.length < 7) return cleaned;
  switch (pattern) {
    case 'middle':
      // 中间4位脱敏
      if (cleaned.length === 11) {
        return cleaned.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2');
      }
      return cleaned;
    case 'last':
      // 后4位脱敏
      if (cleaned.length >= 7) {
        return cleaned.slice(0, -4) + '****';
      }
      return cleaned;
    default:
      return cleaned;
  }
}

/**
 * 检测号码类型（中国）
 * @param phone 电话号码
 * @returns 号码类型
 */
export function detectPhoneType(phone) {
  var cleaned = phone.replace(/\D/g, '');

  // 中国手机号：1开头，11位
  if (/^1[3-9]\d{9}$/.test(cleaned)) {
    return 'mobile';
  }

  // 中国固话：区号+号码
  if (/^0\d{2,3}\d{7,8}$/.test(cleaned)) {
    return 'landline';
  }
  return 'unknown';
}