var normalizeCallingCode = function normalizeCallingCode(countryCallingCode) {
  return "".concat(countryCallingCode || '').replace(/\D/g, '');
};
var normalizePhoneValue = function normalizePhoneValue(value) {
  return "".concat(value || '').replace(/\D/g, '');
};
var joinGroups = function joinGroups(groups, separator) {
  return groups.filter(Boolean).join(separator === 'dash' ? '-' : ' ');
};
var splitByGroups = function splitByGroups(phone, groups) {
  var result = [];
  var start = 0;
  groups.forEach(function (size) {
    if (start >= phone.length) return;
    result.push(phone.slice(start, start + size));
    start += size;
  });
  if (start < phone.length) {
    result.push(phone.slice(start));
  }
  return result;
};
var formatByGroups = function formatByGroups(phone, groups, separator) {
  return joinGroups(splitByGroups(phone, groups), separator);
};
var formatWithOptionalLeadingZero = function formatWithOptionalLeadingZero(phone, withZeroGroups, withoutZeroGroups, separator) {
  return formatByGroups(phone, phone.startsWith('0') ? withZeroGroups : withoutZeroGroups, separator);
};

/**
 * 按 phoneValidation 已覆盖的国家区号格式化手机号展示，返回值仅用于 UI 展示。
 * @param phone 手机号，允许传入含空格或短横线的字符串
 * @param countryCallingCode 国家区号，支持 61 或 +61
 * @param separator 展示分隔符
 */
export var formatPhoneDisplay = function formatPhoneDisplay(phone, countryCallingCode) {
  var separator = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'space';
  var normalizedPhone = normalizePhoneValue(phone);
  var normalizedCode = normalizeCallingCode(countryCallingCode);
  if (!normalizedPhone) {
    return '';
  }
  switch (normalizedCode) {
    case '61':
      return formatWithOptionalLeadingZero(normalizedPhone, [2, 4, 4], [1, 4, 4], separator);
    case '86':
      return formatByGroups(normalizedPhone, [3, 4, 4], separator);
    case '852':
    case '853':
    case '65':
      return formatByGroups(normalizedPhone, [4, 4], separator);
    case '886':
      return formatWithOptionalLeadingZero(normalizedPhone, [4, 3, 3], [3, 3, 3], separator);
    case '1':
      return formatByGroups(normalizedPhone, [3, 3, 4], separator);
    case '44':
      return formatWithOptionalLeadingZero(normalizedPhone, [4, 3, 4], [4, 3, 3], separator);
    case '81':
      return formatWithOptionalLeadingZero(normalizedPhone, [3, 4, 4], [2, 4, 4], separator);
    case '82':
      return formatByGroups(normalizedPhone, [3, 3, 4], separator);
    case '60':
      return formatByGroups(normalizedPhone, [2, 4, 4], separator);
    case '66':
    case '84':
    case '351':
      return formatByGroups(normalizedPhone, [3, 3, 3], separator);
    case '63':
    case '91':
      return formatByGroups(normalizedPhone, [3, 3, 4], separator);
    case '62':
      return formatByGroups(normalizedPhone, [3, 4, 4], separator);
    default:
      return formatByGroups(normalizedPhone, [3, 3, 3, 3, 3], separator);
  }
};