export type KbxTransactionMode = 'new' | 'edit' | 'view'

export interface KbxTransactionContext<THeader, TLine> {
  header: THeader
  lines: TLine[]
  mode: KbxTransactionMode
  status: string
  dirty: boolean
  version?: number
}


export type KbxRecordEditability = 'editable' | 'restricted' | 'readonly'

export interface KbxRecordStatePolicy {
  status: string
  editability: KbxRecordEditability
  editableFields?: string[]
  message?: string
}

export function resolveKbxRecordStatePolicy(policies: KbxRecordStatePolicy[], status: string): KbxRecordStatePolicy {
  return policies.find(x => x.status === status) ?? { status, editability: 'readonly', message: '현재 상태에서는 수정할 수 없습니다.' }
}

export function kbxFieldReadonly(policy: KbxRecordStatePolicy, field: string): boolean {
  if (policy.editability === 'readonly') return true
  if (policy.editability === 'editable') return false
  return !(policy.editableFields ?? []).includes(field)
}
