export type TransactionTypeLike =
  string | number | { id?: number; name?: string } | null | undefined;

const debitTypeNames = new Set(['fine', 'levy', 'fee', 'expense']);

export function signedTransactionAmount(amount: number, type: TransactionTypeLike): number {
  const typeId = typeof type === 'number' ? type : typeof type === 'object' ? type?.id : undefined;
  const typeName =
    typeof type === 'string' ? type : typeof type === 'object' ? type?.name : undefined;
  const isDebit = (typeId ?? 0) > 10 || debitTypeNames.has(typeName ?? '');
  return isDebit ? -Math.abs(amount) : amount;
}
