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

export type { ITranslatedText } from '@/views/Survey/views/Preview/components/questions/types/translatedText';

/**
 * 验证邮箱格式
 * @param answer {ICompletionAnswer} 答案
 * @param translatedText {ITranslatedText} 语言转换文本
 */
export function validateEmail(
  answer: string,
  translatedText = getLanguage(['zh']) as ITranslatedText
) {
  let isError: boolean;
  isError =
    !/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/.test(
      answer
    );
  return isError ? translatedText.PleaseEnterACorrectEmail : '';
}
