/**
 * 残疾证号解析工具函数单元测试
 */

import { describe, it, expect } from 'vitest';
import {
  parseDisabilityId,
  getStandardDisabilityTypes,
  getStandardDisabilityLevels,
  isValidDisabilityType,
  isValidDisabilityLevel
} from '../../src/utils/disabilityIdParser';

describe('disabilityIdParser', () => {
  describe('parseDisabilityId - 标准格式解析', () => {
    it('应该正确解析"视力残疾123456一级"格式', () => {
      const result = parseDisabilityId('视力残疾123456一级');
      expect(result.disabilityType).toBe('视力残疾');
      expect(result.disabilityLevel).toBe('一级');
      expect(result.isValid).toBe(true);
    });

    it('应该正确解析"肢体残疾987654二级"格式', () => {
      const result = parseDisabilityId('肢体残疾987654二级');
      expect(result.disabilityType).toBe('肢体残疾');
      expect(result.disabilityLevel).toBe('二级');
      expect(result.isValid).toBe(true);
    });

    it('应该正确解析"听力残疾111111三级"格式', () => {
      const result = parseDisabilityId('听力残疾111111三级');
      expect(result.disabilityType).toBe('听力残疾');
      expect(result.disabilityLevel).toBe('三级');
      expect(result.isValid).toBe(true);
    });

    it('应该正确解析"精神残疾222222四级"格式', () => {
      const result = parseDisabilityId('精神残疾222222四级');
      expect(result.disabilityType).toBe('精神残疾');
      expect(result.disabilityLevel).toBe('四级');
      expect(result.isValid).toBe(true);
    });
  });

  describe('parseDisabilityId - 简写格式解析', () => {
    it('应该正确解析"视力123456一级"简写格式', () => {
      const result = parseDisabilityId('视力123456一级');
      expect(result.disabilityType).toBe('视力残疾');
      expect(result.disabilityLevel).toBe('一级');
      expect(result.isValid).toBe(true);
    });

    it('应该正确解析"肢体987654二级"简写格式', () => {
      const result = parseDisabilityId('肢体987654二级');
      expect(result.disabilityType).toBe('肢体残疾');
      expect(result.disabilityLevel).toBe('二级');
      expect(result.isValid).toBe(true);
    });

    it('应该正确解析"言语111111三级"简写格式', () => {
      const result = parseDisabilityId('言语111111三级');
      expect(result.disabilityType).toBe('言语残疾');
      expect(result.disabilityLevel).toBe('三级');
      expect(result.isValid).toBe(true);
    });
  });

  describe('parseDisabilityId - 数字等级格式', () => {
    it('应该正确解析"视力1234561级"格式', () => {
      const result = parseDisabilityId('视力1234561级');
      expect(result.disabilityType).toBe('视力残疾');
      expect(result.disabilityLevel).toBe('一级');
      expect(result.isValid).toBe(true);
    });

    it('应该正确解析"肢体9876542级"格式', () => {
      const result = parseDisabilityId('肢体9876542级');
      expect(result.disabilityType).toBe('肢体残疾');
      expect(result.disabilityLevel).toBe('二级');
      expect(result.isValid).toBe(true);
    });
  });

  describe('parseDisabilityId - 部分匹配', () => {
    it('应该只解析残疾类别，不解析等级', () => {
      const result = parseDisabilityId('视力残疾123456');
      expect(result.disabilityType).toBe('视力残疾');
      expect(result.disabilityLevel).toBeNull();
      expect(result.isValid).toBe(true); // 至少解析出一个字段
    });

    it('应该只解析等级，不解析类别', () => {
      const result = parseDisabilityId('123456一级');
      expect(result.disabilityType).toBeNull();
      expect(result.disabilityLevel).toBe('一级');
      expect(result.isValid).toBe(true);
    });
  });

  describe('parseDisabilityId - 边界情况', () => {
    it('应该处理空字符串', () => {
      const result = parseDisabilityId('');
      expect(result.disabilityType).toBeNull();
      expect(result.disabilityLevel).toBeNull();
      expect(result.isValid).toBe(false);
    });

    it('应该处理只有空格的字符串', () => {
      const result = parseDisabilityId('   ');
      expect(result.disabilityType).toBeNull();
      expect(result.disabilityLevel).toBeNull();
      expect(result.isValid).toBe(false);
    });

    it('应该处理无法匹配的格式', () => {
      const result = parseDisabilityId('ABC123');
      expect(result.disabilityType).toBeNull();
      expect(result.disabilityLevel).toBeNull();
      expect(result.isValid).toBe(false);
    });

    it('应该处理带空格的残疾证号', () => {
      const result = parseDisabilityId(' 视力残疾 123456 一级 ');
      expect(result.disabilityType).toBe('视力残疾');
      expect(result.disabilityLevel).toBe('一级');
      expect(result.isValid).toBe(true);
    });
  });

  describe('parseDisabilityId - 所有残疾类别', () => {
    it('应该正确解析所有七种残疾类别', () => {
      const types = [
        '视力残疾',
        '听力残疾',
        '言语残疾',
        '肢体残疾',
        '智力残疾',
        '精神残疾',
        '多重残疾'
      ];

      types.forEach(type => {
        const result = parseDisabilityId(`${type}123456一级`);
        expect(result.disabilityType).toBe(type);
        expect(result.isValid).toBe(true);
      });
    });
  });

  describe('getStandardDisabilityTypes', () => {
    it('应该返回所有七种标准残疾类别', () => {
      const types = getStandardDisabilityTypes();
      expect(types).toEqual([
        '视力残疾',
        '听力残疾',
        '言语残疾',
        '肢体残疾',
        '智力残疾',
        '精神残疾',
        '多重残疾'
      ]);
    });
  });

  describe('getStandardDisabilityLevels', () => {
    it('应该返回所有四种标准残疾等级', () => {
      const levels = getStandardDisabilityLevels();
      expect(levels).toEqual(['一级', '二级', '三级', '四级']);
    });
  });

  describe('isValidDisabilityType', () => {
    it('应该验证有效的残疾类别', () => {
      expect(isValidDisabilityType('视力残疾')).toBe(true);
      expect(isValidDisabilityType('肢体残疾')).toBe(true);
      expect(isValidDisabilityType('多重残疾')).toBe(true);
    });

    it('应该拒绝无效的残疾类别', () => {
      expect(isValidDisabilityType('其他残疾')).toBe(false);
      expect(isValidDisabilityType('视力')).toBe(false);
      expect(isValidDisabilityType('')).toBe(false);
    });
  });

  describe('isValidDisabilityLevel', () => {
    it('应该验证有效的残疾等级', () => {
      expect(isValidDisabilityLevel('一级')).toBe(true);
      expect(isValidDisabilityLevel('二级')).toBe(true);
      expect(isValidDisabilityLevel('三级')).toBe(true);
      expect(isValidDisabilityLevel('四级')).toBe(true);
    });

    it('应该拒绝无效的残疾等级', () => {
      expect(isValidDisabilityLevel('五级')).toBe(false);
      expect(isValidDisabilityLevel('1级')).toBe(false);
      expect(isValidDisabilityLevel('')).toBe(false);
    });
  });
});
