import { classifyRow } from './classifyRow'

describe('classifyRow', () => {
  const headerPks = ['PRIMARY_KEY_FIELD']

  it('is D when the row is marked for delete, regardless of other field changes', () => {
    const dataRow = {
      PRIMARY_KEY_FIELD: 1,
      SOME_CHAR: 'changed',
      _____DELETE__THIS__RECORD_____: 'Yes'
    }
    const dataSourceUnchanged = [
      { PRIMARY_KEY_FIELD: 1, SOME_CHAR: 'original' }
    ]

    expect(classifyRow(dataRow, dataSourceUnchanged, headerPks)).toEqual('D')
  })

  it('is A when no row in dataSourceUnchanged matches the primary key(s)', () => {
    const dataRow = { PRIMARY_KEY_FIELD: 99, SOME_CHAR: 'new row' }
    const dataSourceUnchanged = [
      { PRIMARY_KEY_FIELD: 1, SOME_CHAR: 'original' }
    ]

    expect(classifyRow(dataRow, dataSourceUnchanged, headerPks)).toEqual('A')
  })

  it('is M when a matching row exists but a field differs', () => {
    const dataRow = { PRIMARY_KEY_FIELD: 1, SOME_CHAR: 'changed' }
    const dataSourceUnchanged = [
      { PRIMARY_KEY_FIELD: 1, SOME_CHAR: 'original' }
    ]

    expect(classifyRow(dataRow, dataSourceUnchanged, headerPks)).toEqual('M')
  })

  it('is U when a matching row exists and nothing differs', () => {
    const dataRow = { PRIMARY_KEY_FIELD: 1, SOME_CHAR: 'original' }
    const dataSourceUnchanged = [
      { PRIMARY_KEY_FIELD: 1, SOME_CHAR: 'original' }
    ]

    expect(classifyRow(dataRow, dataSourceUnchanged, headerPks)).toEqual('U')
  })

  it('matches on all primary key columns for a composite key', () => {
    const headerPksComposite = ['LIB', 'ID']
    const dataRow = { LIB: 'WORK', ID: 1, SOME_CHAR: 'original' }
    const dataSourceUnchanged = [
      { LIB: 'OTHER', ID: 1, SOME_CHAR: 'original' },
      { LIB: 'WORK', ID: 1, SOME_CHAR: 'original' }
    ]

    // Same ID=1 exists under a different LIB - must not match that one.
    expect(
      classifyRow(dataRow, dataSourceUnchanged, headerPksComposite)
    ).toEqual('U')
  })
})
