import { Col } from '../models/col.model'
import { DcValidation } from '../models/dc-validation.model'
import { mergeColsRules } from '../utils/mergeColsRules'

describe('DC Validator - merge spec rules', () => {
  it('should return array of merged specs', () => {
    const rules: DcValidation[] = [
      {
        data: 'test_col'
      }
    ]
    const cols: Col[] = [
      {
        NAME: 'test_col',
        MEMLABEL: 'string',
        DESC: 'test_desc',
        LONGDESC: 'string',
        TYPE: '',
        CLS_RULE: 'cls_rule',
        VARNUM: 0,
        LABEL: 'string',
        FMTNAME: 'string',
        DDTYPE: 'string'
      }
    ]
    const $dataFormats: any = {
      vars: {
        test_col: {
          format: 'best.',
          label: 'PRIMARY_KEY_FIELD',
          length: '8',
          type: 'test_type'
        }
      }
    }
    const expected: DcValidation[] = [
      {
        data: 'test_col',
        desc: 'test_desc',
        clsRule: 'cls_rule',
        length: 8,
        sasType: 'test_type'
      }
    ]

    expect(mergeColsRules(cols, rules, $dataFormats)).toEqual(expected)
    expect(cols[0].TYPE).toEqual('test_type')
  })

  it('should populate sasType for num and char cols', () => {
    const rules: DcValidation[] = [{ data: 'num_col' }, { data: 'char_col' }]
    const cols: Col[] = [
      {
        NAME: 'num_col',
        MEMLABEL: '',
        DESC: '',
        LONGDESC: '',
        TYPE: '',
        CLS_RULE: '',
        VARNUM: 0,
        LABEL: '',
        FMTNAME: '',
        DDTYPE: ''
      },
      {
        NAME: 'char_col',
        MEMLABEL: '',
        DESC: '',
        LONGDESC: '',
        TYPE: '',
        CLS_RULE: '',
        VARNUM: 0,
        LABEL: '',
        FMTNAME: '',
        DDTYPE: ''
      }
    ]
    const $dataFormats: any = {
      vars: {
        num_col: { format: 'best.', label: '', length: '8', type: 'num' },
        char_col: { format: '$32.', label: '', length: '32', type: 'char' }
      }
    }

    const merged = mergeColsRules(cols, rules, $dataFormats)
    expect(merged.find((r) => r.data === 'num_col')?.sasType).toEqual('num')
    expect(merged.find((r) => r.data === 'char_col')?.sasType).toEqual('char')
  })
})
