import type { PostImportRow } from '@/lib/postImportTypes'

export type PostImportDisplayStatus =
    'ready'
    | 'error'
    | 'skipped'
    | 'warning'
    | 'created'
    | 'failed'

export type PostImportBadgeValue = PostImportDisplayStatus

const hasWarnings = (row: PostImportRow): boolean =>
    Object.values (row.fieldWarnings ?? { }).some (messages => messages.length > 0)
    || row.baseWarnings.length > 0

export const displayPostImportStatus = (
    row: PostImportRow,
): PostImportDisplayStatus | null =>
    row.status === 'pending'
    ? null
    : (row.importStatus === 'failed')
    ? 'failed'
    : (row.skipReason != null || row.importStatus === 'skipped')
    ? 'skipped'
    : (row.importStatus === 'created')
      ? 'created'
      : (row.status === 'error')
      ? 'error'
      : (Object.values (row.validationErrors ?? { }).some (messages => messages.length > 0))
      ? 'error'
      : ((hasWarnings (row) || row.status === 'warning')
	 ? 'warning'
	 : 'ready')
