export { txDisplayAmount } from '@thebet365/shared';

export const TX_KEY_MAP: Record<string, string> = {
  MANUAL_DEPOSIT: 'finance.tx.deposit',
  ADMIN_DEPOSIT: 'finance.tx.admin_deposit',
  AGENT_DEPOSIT: 'finance.tx.agent_deposit',
  INITIAL_DEPOSIT: 'finance.tx.admin_deposit',
  MANUAL_WITHDRAW: 'finance.tx.withdraw',
  ADMIN_WITHDRAW: 'finance.tx.admin_withdraw',
  AGENT_WITHDRAW: 'finance.tx.agent_withdraw',
  MANUAL_ADJUST: 'finance.tx.adjust',
  BET_FREEZE: 'finance.tx.bet_freeze',
  BET_DEDUCT: 'finance.tx.bet_deduct',
  BET_SETTLE_WIN: 'finance.tx.bet_win',
  BET_SETTLE_LOSE: 'finance.tx.bet_lose',
  BET_SETTLE_PUSH: 'finance.tx.bet_push',
  BET_WIN: 'finance.tx.bet_win',
  BET_REFUND: 'finance.tx.bet_refund',
  BET_VOID: 'finance.tx.bet_void',
  BET_VOID_REFUND: 'finance.tx.bet_void',
  CASHBACK: 'finance.tx.cashback',
  CASHBACK_DEPOSIT: 'finance.tx.cashback',
  RESETTLE_REVERSE: 'finance.tx.resettle',
  DEPOSIT: 'finance.tx.deposit',
  WITHDRAW: 'finance.tx.withdraw',
  PLAYER_DEPOSIT: 'finance.tx.player_deposit',
};

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

export const DEPOSIT_RECHARGE_TYPES = new Set([
  'MANUAL_DEPOSIT',
  'ADMIN_DEPOSIT',
  'AGENT_DEPOSIT',
  'INITIAL_DEPOSIT',
  'DEPOSIT',
  'PLAYER_DEPOSIT',
]);

export const DEPOSIT_METHOD_KEY_MAP: Record<string, string> = {
  MANUAL_ADMIN: 'finance.deposit_method.manual_admin',
  MANUAL_AGENT: 'finance.deposit_method.manual_agent',
  MANUAL: 'finance.deposit_method.manual',
};

export function walletDepositMethodLabel(
  row: {
    transactionType: string;
    depositMethodKey?: string | null;
    depositMethodName?: string | null;
  },
  t: (key: string) => string,
): string {
  if (!DEPOSIT_RECHARGE_TYPES.has(row.transactionType.toUpperCase())) return '—';
  if (row.depositMethodName?.trim()) return row.depositMethodName.trim();
  if (row.depositMethodKey) {
    const key = DEPOSIT_METHOD_KEY_MAP[row.depositMethodKey];
    return key ? t(key) : row.depositMethodKey;
  }
  const type = row.transactionType.toUpperCase();
  if (type === 'ADMIN_DEPOSIT') return t('finance.tx.admin_deposit');
  if (type === 'AGENT_DEPOSIT') return t('finance.tx.agent_deposit');
  if (type === 'INITIAL_DEPOSIT') return t('finance.tx.admin_deposit');
  if (type === 'PLAYER_DEPOSIT') return t('finance.tx.player_deposit');
  return '—';
}

const WALLET_REMARK_EXACT: Record<string, string> = {
  'Agent deposit': 'finance.remark.agent_deposit',
  'Agent withdraw': 'finance.remark.agent_withdraw',
  '代理上分': 'finance.remark.agent_deposit',
  '代理下分': 'finance.remark.agent_withdraw',
  '管理员上分': 'finance.remark.admin_deposit',
  '管理员下分': 'finance.remark.admin_withdraw',
  '开户初始余额': 'finance.remark.initial_balance',
  'Resettlement adjustment': 'finance.tx.resettle',
};

/** 钱包流水备注：系统英文/中文模板按当前语言展示 */
export function walletRemarkLabel(
  remark: string | null | undefined,
  transactionType: string,
  t: (key: string, params?: Record<string, string | number>) => string,
): string {
  const raw = remark?.trim();
  if (!raw) {
    if (transactionType === 'MANUAL_DEPOSIT') return t('finance.remark.agent_deposit');
    if (transactionType === 'MANUAL_WITHDRAW') return t('finance.remark.agent_withdraw');
    return '—';
  }

  const exactKey = WALLET_REMARK_EXACT[raw];
  if (exactKey) return t(exactKey);

  const revokeEn = raw.match(/^Revoke approved deposit\s+([A-Z0-9]+)$/i);
  if (revokeEn) return t('finance.remark.revoke_deposit', { orderNo: revokeEn[1] });

  const revokeZh = raw.match(/^撤销已通过充值\s+([A-Z0-9]+)$/);
  if (revokeZh) return t('finance.remark.revoke_deposit', { orderNo: revokeZh[1] });

  const depositOrder = raw.match(/^Deposit order\s+([A-Z0-9]+)$/i);
  if (depositOrder) return t('finance.remark.deposit_order', { orderNo: depositOrder[1] });

  const cashbackBatch = raw.match(/^Cashback batch\s+(.+)$/i);
  if (cashbackBatch) return t('finance.remark.cashback_batch', { batchNo: cashbackBatch[1].trim() });

  return raw;
}
