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 {ICompletionConfig} config - 相关配置
 * @param {ICompletionAnswer} answer - 答案
 * @param {ITranslatedText} translatedText - 语言转换文本
 */
export function validateChineseLetter(
  config: ICompletionConfig,
  answer: string,
  translatedText = getLanguage(['zh']) as ITranslatedText
) {
  let isError: boolean;

  // 1. 包含标点 2. 不包含标点
  // 如果包含标点，则使用正则校验，如果不包含标点，则使用
  // /^(?:[\u3400-\u4DB5\u4E00-\u9FEA\uFA0E\uFA0F\uFA11\uFA13\uFA14\uFA1F\uFA21\uFA23\uFA24\uFA27-\uFA29]|[\uD840-\uD868\uD86A-\uD86C\uD86F-\uD872\uD874-\uD879][\uDC00-\uDFFF]|\uD869[\uDC00-\uDED6\uDF00-\uDFFF]|\uD86D[\uDC00-\uDF34\uDF40-\uDFFF]|\uD86E[\uDC00-\uDC1D\uDC20-\uDFFF]|\uD873[\uDC00-\uDEA1\uDEB0-\uDFFF]|\uD87A[\uDC00-\uDFE0])+$/.test(newValue) || !newValue.length
  // 如果包含标点，校验规则
  // 如果不包含标点，校验规则
  isError =
    config.include_mark === 1
      ? !/^(?:[\u3400-\u4DB5\u4E00-\u9FEA\uFA0E\uFA0F\uFA11\uFA13\uFA14\uFA1F\uFA21\uFA23\uFA24\uFA27-\uFA29]|[\uD840-\uD868\uD86A-\uD86C\uD86F-\uD872\uD874-\uD879][\uDC00-\uDFFF]|\uD869[\uDC00-\uDED6\uDF00-\uDFFF]|\uD86D[\uDC00-\uDF34\uDF40-\uDFFF]|\uD86E[\uDC00-\uDC1D\uDC20-\uDFFF]|\uD873[\uDC00-\uDEA1\uDEB0-\uDFFF]|\uD87A[\uDC00-\uDFE0]|[a-zA-Z·~！@#￥%…&*（）—\-+={}|《》？：“”【】、；‘’，。`!$^()_<>?:",./;'\\[\]])+$/.test(
          answer
        ) || !answer.length
      : !/^(?:[\u3400-\u4DB5\u4E00-\u9FEA\uFA0E\uFA0F\uFA11\uFA13\uFA14\uFA1F\uFA21\uFA23\uFA24\uFA27-\uFA29]|[\uD840-\uD868\uD86A-\uD86C\uD86F-\uD872\uD874-\uD879][\uDC00-\uDFFF]|\uD869[\uDC00-\uDED6\uDF00-\uDFFF]|\uD86D[\uDC00-\uDF34\uDF40-\uDFFF]|\uD86E[\uDC00-\uDC1D\uDC20-\uDFFF]|\uD873[\uDC00-\uDEA1\uDEB0-\uDFFF]|\uD87A[\uDC00-\uDFE0])+$/.test(
          answer
        ) || !answer.length;
  return isError ? translatedText.PleaseEnterChineseWords : '';
}
