import { getDisplayColHeaders } from './display-col-headers'
import { Col } from '../dc-validator/models/col.model'

const makeCol = (overrides: Partial<Col>): Col => ({
  NAME: '',
  VARNUM: 0,
  LABEL: '',
  FMTNAME: '',
  DDTYPE: '',
  TYPE: '',
  CLS_RULE: '',
  MEMLABEL: '',
  DESC: '',
  LONGDESC: '',
  ...overrides
})

describe('getDisplayColHeaders', () => {
  const colNames = ['SOME_CHAR', 'SOME_NUM', 'SOME_DATE']
  const cols: Col[] = [
    makeCol({ NAME: 'SOME_CHAR', LABEL: 'Some Character Column' }),
    makeCol({ NAME: 'SOME_NUM', LABEL: '' }),
    makeCol({ NAME: 'SOME_DATE', LABEL: 'Some Date' })
  ]

  it('returns colNames unchanged when useLabels is false', () => {
    expect(getDisplayColHeaders(colNames, cols, false)).toEqual(colNames)
  })

  it('returns matching LABELs when useLabels is true', () => {
    expect(getDisplayColHeaders(colNames, cols, true)).toEqual([
      'Some Character Column',
      'SOME_NUM',
      'Some Date'
    ])
  })

  it('falls back to NAME when LABEL is blank', () => {
    const result = getDisplayColHeaders(['SOME_NUM'], cols, true)
    expect(result).toEqual(['SOME_NUM'])
  })

  it('falls back to NAME when the column is missing from cols', () => {
    const result = getDisplayColHeaders(
      ['SOME_CHAR', 'UNKNOWN_COL'],
      cols,
      true
    )
    expect(result).toEqual(['Some Character Column', 'UNKNOWN_COL'])
  })

  it('preserves colNames order, not cols order', () => {
    const reordered = ['SOME_DATE', 'SOME_CHAR']
    expect(getDisplayColHeaders(reordered, cols, true)).toEqual([
      'Some Date',
      'Some Character Column'
    ])
  })
})
