import type { ICompletionAnswer } from '@/views/Survey/views/Preview/components/questions/types/previewCompletion';
import { getLanguage } from '@/views/Survey/views/Preview/js/language';
import type { ITranslatedText } from '@/views/Survey/views/Preview/components/questions/types/translatedText';

/**
 * 验证手机号
 * @param answer {ICompletionAnswer} 答案
 * @param translatedText {ITranslatedText} 语言转换文本
 */
export function validateIDCard(
  answer: string,
  translatedText = getLanguage(['zh']) as ITranslatedText
) {
  let isError: boolean;

  isError =
    !/^[1-9]\d{5}(?:18|19|20)\d{2}(?:0[1-9]|10|11|12)(?:0[1-9]|[1-2]\d|30|31)\d{3}[\dXx]$/.test(
      answer
    );

  return isError ? translatedText.PleaseEnterACorrectID : '';
}
