import { getLanguage } from '@/views/Survey/views/Preview/js/language';
import type { IQuestionOption } from '@/types/question';
import type { IBaseConfig } from '@/types/baseConfig';

/**
 * 检验答案是否数量是否符合题目要求
 * @param answer 答案
 * @param config 配置
 * @param options 选项
 * @param translatedText 错误提示
 */
function validateAnswerNum<T extends IBaseConfig>(
  answer: number[],
  config: T,
  options: IQuestionOption[],
  translatedText = getLanguage(['zh'])
) {
  const { is_required } = config;
  const min = config.min || 0;
  const max = config.max || 0;
  let errorMessage: string = '';

  // 如果不是必答题，跳过检验
  if (!is_required) return errorMessage;

  // 检测答案数量是否在范围内
  answer.forEach((answerItem) => {
    // 如果不在范围内，提示错误， answerItem < min 或者 max 存在，并且 answerItem > max
    if (answerItem < min || (answerItem > max && max)) {
      errorMessage = translatedText.PleaseSelectAValueThatIsNoLessThanOneAndNoMoreThanOne(min, max);
    }
  });

  console.log('answer', answer.length, options.length);
  // 检测答题数量，若是小于 options 的数量，提示错误
  if (answer.length < options.length) {
    errorMessage = translatedText.ThisIsARequiredQuestion;
  }
  return errorMessage;
}

export { validateAnswerNum };
