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

export type CashFlowDirection = 'inflow' | 'outflow' | 'neutral';
export type CashFlowContext = 'player' | 'team';

export interface CashFlowPresentation {
  direction: CashFlowDirection;
  amount: number;
  sign: '+' | '−' | '';
}

export function presentCashFlow(
  amount: number,
  type: TransactionTypeLike,
  context: CashFlowContext,
): CashFlowPresentation {
  const typeId = typeof type === 'number' ? type : typeof type === 'object' ? type?.id : undefined;
  const typeName = (
    typeof type === 'string' ? type : typeof type === 'object' ? type?.name : undefined
  )
    ?.trim()
    .toLowerCase();

  const direction =
    context === 'player' && (typeId === 0 || typeName === 'payment')
      ? amount < 0
        ? 'outflow'
        : 'inflow'
      : context === 'team' && (typeId === 1 || typeName === 'credit')
        ? 'inflow'
        : context === 'team' && (typeId === 14 || typeName === 'expense')
          ? 'outflow'
          : 'neutral';

  return {
    direction,
    amount: Math.abs(amount),
    sign: direction === 'inflow' ? '+' : direction === 'outflow' ? '−' : '',
  };
}
