export type RowEditStatus = 'M' | 'A' | 'D' | 'U'

/**
 * Classifies a single row as Modified/Added/Deleted/Unchanged against the
 * pre-edit snapshot (`dataSourceUnchanged`), PK-matched via `headerPks`.
 *
 * Extracted from editor.component.ts's getRowsSubmittingCount(), which used
 * this same logic inline, only at submit time. Kept here as a pure function
 * so it can run live (e.g. on every afterChange, for the EDIT_STATUS column)
 * without duplicating the diffing rules.
 */
export const classifyRow = (
  dataRow: any,
  dataSourceUnchanged: any[],
  headerPks: string[]
): RowEditStatus => {
  if (dataRow._____DELETE__THIS__RECORD_____ === 'Yes') return 'D'

  const dataRowUnchanged = dataSourceUnchanged.find((row: any) => {
    for (const pkCol of headerPks) {
      if (row[pkCol] !== dataRow[pkCol]) return false
    }

    return true
  })

  if (!dataRowUnchanged) return 'A'

  return JSON.stringify(dataRow) !== JSON.stringify(dataRowUnchanged)
    ? 'M'
    : 'U'
}
