/**
 * selectValueTypeHelper 工具函数测试
 */
import {
  processOptionsForValueType,
  findLabelByValue,
  processBothModeData,
  processLabelModeData,
  getOriginValue
} from '../selectValueTypeHelper'

describe('selectValueTypeHelper', () => {
  const mockOptions = [
    { value: '1', label: '选项1' },
    { value: '2', label: '选项2' },
    { key: 'key1', title: '标题1' },
    { value: 'val1', text: '文本1' }
  ]

  const mockTreeOptions = [
    {
      value: 'parent1',
      label: '父级1',
      children: [
        { value: 'child1', label: '子级1' },
        { value: 'child2', label: '子级2' }
      ]
    }
  ]

  describe('processOptionsForValueType', () => {
    test('value模式应该返回原始数据', () => {
      const result = processOptionsForValueType(mockOptions, 'value')
      expect(result).toEqual(mockOptions)
    })

    test('label模式应该交换value和label', () => {
      const result = processOptionsForValueType(mockOptions, 'label')
      expect(result[0]).toEqual({
        value: '选项1',
        label: '选项1',
        originValue: '1'
      })
    })

    test('both模式应该返回原始数据', () => {
      const result = processOptionsForValueType(mockOptions, 'both')
      expect(result).toEqual(mockOptions)
    })

    test('应该处理树形结构', () => {
      const result = processOptionsForValueType(mockTreeOptions, 'label')
      expect(result[0].children[0]).toEqual({
        value: '子级1',
        label: '子级1',
        originValue: 'child1'
      })
    })
  })

  describe('findLabelByValue', () => {
    test('应该找到对应的label', () => {
      const result = findLabelByValue('1', mockOptions)
      expect(result).toBe('选项1')
    })

    test('应该处理数组值（多选）', () => {
      const result = findLabelByValue(['1', '2'], mockOptions)
      expect(result).toEqual(['选项1', '选项2'])
    })

    test('找不到时应该返回原值', () => {
      const result = findLabelByValue('notfound', mockOptions)
      expect(result).toBe('notfound')
    })

    test('应该处理null和undefined', () => {
      expect(findLabelByValue(null, mockOptions)).toBeNull()
      expect(findLabelByValue(undefined, mockOptions)).toBeNull()
    })
  })

  describe('processBothModeData', () => {
    test('应该在labelData中创建label字段', () => {
      const form = { status: '1' }
      const mockSetField = jest.fn()

      processBothModeData(form, 'status', '1', mockOptions, mockSetField)

      expect(mockSetField).toHaveBeenCalledWith(form, 'labelData', {})
      expect(mockSetField).toHaveBeenCalledWith(form.labelData, 'status', '选项1')
    })

    test('当labelData已存在时不应该重复创建', () => {
      const form = { status: '1', labelData: {} }
      const mockSetField = jest.fn()

      processBothModeData(form, 'status', '1', mockOptions, mockSetField)

      expect(mockSetField).toHaveBeenCalledWith(form.labelData, 'status', '选项1')
      expect(mockSetField).toHaveBeenCalledTimes(1)
    })
  })

  describe('processLabelModeData', () => {
    test('应该在originData中创建原始值字段', () => {
      const labelModeOptions = [
        { value: '选项1', originValue: '1', label: '选项1' }
      ]
      const form = { status: '选项1' }
      const mockSetField = jest.fn()

      processLabelModeData(form, 'status', '选项1', labelModeOptions, mockSetField)

      expect(mockSetField).toHaveBeenCalledWith(form, 'originData', {})
      expect(mockSetField).toHaveBeenCalledWith(form.originData, 'status', '1')
    })

    test('当originData已存在时不应该重复创建', () => {
      const labelModeOptions = [
        { value: '选项1', originValue: '1', label: '选项1' }
      ]
      const form = { status: '选项1', originData: {} }
      const mockSetField = jest.fn()

      processLabelModeData(form, 'status', '选项1', labelModeOptions, mockSetField)

      expect(mockSetField).toHaveBeenCalledWith(form.originData, 'status', '1')
      expect(mockSetField).toHaveBeenCalledTimes(1)
    })
  })

  describe('getOriginValue', () => {
    const labelModeOptions = [
      { value: '选项1', originValue: '1', label: '选项1' },
      { value: '选项2', originValue: '2', label: '选项2' }
    ]

    test('应该获取原始值', () => {
      const result = getOriginValue('选项1', labelModeOptions)
      expect(result).toBe('1')
    })

    test('应该处理数组值', () => {
      const result = getOriginValue(['选项1', '选项2'], labelModeOptions)
      expect(result).toEqual(['1', '2'])
    })

    test('没有originValue时应该返回原值', () => {
      const result = getOriginValue('test', mockOptions)
      expect(result).toBe('test')
    })
  })
})
