import type { ReactElement } from 'react'
import type { DebtLoad } from '../stakes/storage'
import type { SettledDebt } from '../stakes/types'
import { OutstandingDebtRow } from './OutstandingDebtRow'
import { taskLabel } from './taskLabel'
interface DebtHistoryPanelProps {
debtLoad: DebtLoad
onLogDebtProgress: (debtId: string, units: number) => void
}
const reasonLabel = (reason: SettledDebt['reason']): string => (reason === 'bust' ? 'bust' : 'cashed out')
/** Cleared debts are read-only history — there is no control to un-pay one. */
const ClearedDebtRow = ({ debt }: { debt: SettledDebt }): ReactElement => (
{taskLabel(debt.taskTypeId)}: {debt.owedUnits} owed ({reasonLabel(debt.reason)}) — cleared
)
/**
* The durable ledger, separate from any single session: every `SettledDebt` ever recorded, newest
* first. Outstanding entries carry the same log-progress control `BuyInScreen`'s gate shows (the
* shared `OutstandingDebtRow`), so progress can be logged from either place with identical rules.
*/
export const DebtHistoryPanel = ({ debtLoad, onLogDebtProgress }: DebtHistoryPanelProps): ReactElement => {
if (debtLoad.status === 'unreadable') {
return (
Debt history
Your debt history could not be read.
)
}
if (debtLoad.debts.length === 0) {
return (
Debt history
No debts settled yet.
)
}
const newestFirst = [...debtLoad.debts].sort((a, b) => b.settledAt - a.settledAt)
return (
Debt history
{newestFirst.map((debt) =>
debt.paidUnits < debt.owedUnits ? (
) : (
),
)}
)
}