import { describe, it, expect, vi } from 'vitest';
import { validateEnglishLetters } from '../validateEnglishLetters';
import { getLanguage } from '@/views/Survey/views/Preview/js/language';
import type { ITranslatedText } from '@/views/Survey/views/Preview/components/questions/types/translatedText';

// 模拟翻译函数
const mockTranslatedText: ITranslatedText = {
  ...getLanguage(['zh']),
  PleaseEnterEnglishLetters: '请输入英文字母'
};

// 模拟配置
interface ICompletionConfig {
  include_mark: number;
}

describe('validateEnglishLetters 函数测试', () => {
  it('当包含标点且输入有效英文和标点时，应返回空字符串', () => {
    const config: ICompletionConfig = { include_mark: 1 };
    const validInputs = [
      'Hello',
      'English Letters',
      'English, Punctuation!',
      'English. Punctuation?',
      'English: Punctuation;'
    ];

    validInputs.forEach((input) => {
      // 清除空格
      // const trimmedInput = input.replace(/\s+/g, '');
      const result = validateEnglishLetters(config, validInputs, mockTranslatedText);
      expect(result).toBe('');
    });
  });

  it('当包含标点但输入无效字符时，应返回错误信息', () => {
    const config: ICompletionConfig = { include_mark: 1 };
    const invalidInputs = ['中文', 'English123', '中文abc', '123456'];

    invalidInputs.forEach((input) => {
      const result = validateEnglishLetters(config, input, mockTranslatedText);
      expect(result).toBe(mockTranslatedText.PleaseEnterEnglishLetters);
    });
  });

  it('当不包含标点且输入纯英文字母时，应返回空字符串', () => {
    const config: ICompletionConfig = { include_mark: 2 };
    const validInputs = ['Hello', 'EnglishLetters', 'PureEnglishWithoutPunctuation'];

    validInputs.forEach((input) => {
      const result = validateEnglishLetters(config, input, mockTranslatedText);
      expect(result).toBe('');
    });
  });

  it('当不包含标点但输入包含标点或其他字符时，应返回错误信息', () => {
    const config: ICompletionConfig = { include_mark: 2 };
    const invalidInputs = [
      'English, Punctuation',
      'English. Punctuation',
      'English: Punctuation',
      'English123',
      '中文abc',
      '中文'
    ];

    invalidInputs.forEach((input) => {
      const result = validateEnglishLetters(config, input, mockTranslatedText);
      expect(result).toBe(mockTranslatedText.PleaseEnterEnglishLetters);
    });
  });

  it('应该处理空字符串', () => {
    const config1: ICompletionConfig = { include_mark: 1 };
    const config2: ICompletionConfig = { include_mark: 2 };

    const result1 = validateEnglishLetters(config1, '', mockTranslatedText);
    const result2 = validateEnglishLetters(config2, '', mockTranslatedText);

    expect(result1).toBe(mockTranslatedText.PleaseEnterEnglishLetters);
    expect(result2).toBe(mockTranslatedText.PleaseEnterEnglishLetters);
  });

  it('应该使用默认的翻译文本', () => {
    // 模拟 getLanguage 函数
    vi.mock('@/views/Survey/views/Preview/js/language', () => ({
      getLanguage: vi.fn().mockReturnValue({
        PleaseEnterEnglishLetters: '请输入英文字母'
      })
    }));

    const config: ICompletionConfig = { include_mark: 1 };
    const result = validateEnglishLetters(config, '中文');
    expect(result).toBe('请输入英文字母');
  });
});
