export const TX_KEY_MAP: Record<string, string> = {
  MANUAL_DEPOSIT: 'wallet.tx_deposit',
  ADMIN_DEPOSIT: 'wallet.tx_admin_deposit',
  AGENT_DEPOSIT: 'wallet.tx_agent_deposit',
  INITIAL_DEPOSIT: 'wallet.tx_admin_deposit',
  MANUAL_WITHDRAW: 'wallet.tx_withdraw',
  ADMIN_WITHDRAW: 'wallet.tx_admin_withdraw',
  AGENT_WITHDRAW: 'wallet.tx_agent_withdraw',
  MANUAL_ADJUST: 'wallet.tx_adjust',
  BET_FREEZE: 'wallet.tx_bet_freeze',
  BET_DEDUCT: 'wallet.tx_bet_deduct',
  BET_SETTLE_WIN: 'wallet.tx_bet_win',
  BET_SETTLE_LOSE: 'wallet.tx_bet_lose',
  BET_SETTLE_PUSH: 'wallet.tx_bet_push',
  BET_WIN: 'wallet.tx_bet_win',
  BET_REFUND: 'wallet.tx_bet_refund',
  BET_VOID: 'wallet.tx_bet_void',
  BET_VOID_REFUND: 'wallet.tx_bet_void',
  CASHBACK: 'wallet.tx_cashback',
  CASHBACK_DEPOSIT: 'wallet.tx_cashback',
  RESETTLE_REVERSE: 'wallet.tx_resettle',
  DEPOSIT: 'wallet.tx_deposit',
  WITHDRAW: 'wallet.tx_withdraw',
  PLAYER_DEPOSIT: 'wallet.tx_player_deposit',
  PLAYER_DEPOSIT_REVERSAL: 'wallet.tx_player_deposit_reversal',
};

export function txTypeKey(type: string): string {
  return TX_KEY_MAP[type.toUpperCase()] ?? '';
}

export function txDisplayType(tx: { displayType?: string; transactionType: string }): string {
  return tx.displayType?.trim() || tx.transactionType;
}

export function txSummaryLabel(
  tx: {
    summary?: string | null;
    summaryKind?: 'opening_bonus' | null;
    referenceType?: string | null;
  },
  t: (key: string, params?: Record<string, unknown>) => string,
): string {
  if (tx.summaryKind === 'opening_bonus') {
    return t('wallet.summary_opening_bonus');
  }
  const summary = tx.summary?.trim();
  if (!summary) return '';
  if (tx.referenceType === 'BET') {
    return t('wallet.summary_bet', { betNo: summary });
  }
  return summary;
}

export function isDepositType(type: string): boolean {
  const t = type.toUpperCase();
  return (t.includes('DEPOSIT') || t === 'CASHBACK_DEPOSIT') && !isCashbackType(type) && t !== 'PLAYER_DEPOSIT_REVERSAL';
}

export function isDepositReversalType(type: string): boolean {
  return type.toUpperCase() === 'PLAYER_DEPOSIT_REVERSAL';
}

export function txRemarkLabel(
  tx: { transactionType: string; remark?: string | null; referenceId?: string | null },
  t: (key: string, params?: Record<string, unknown>) => string,
): string {
  const type = tx.transactionType.toUpperCase();
  const remark = tx.remark?.trim() ?? '';
  if (type === 'PLAYER_DEPOSIT_REVERSAL') {
    return t('wallet.remark_deposit_revoke_generic');
  }
  return remark;
}

export function isWithdrawType(type: string): boolean {
  const t = type.toUpperCase();
  if (t === 'PLAYER_DEPOSIT_REVERSAL') return false;
  return t.includes('WITHDRAW');
}

export function isBetType(type: string): boolean {
  return type.toUpperCase().startsWith('BET_') || type.toUpperCase() === 'RESETTLE_REVERSE';
}

export function isCashbackType(type: string): boolean {
  const t = type.toUpperCase();
  return t === 'CASHBACK' || t === 'CASHBACK_DEPOSIT';
}
