import type { Composer } from 'vue-i18n';
import type { SlipItem, SlipMode } from '../stores/betSlip';

export interface BetPlaceConfirmContext {
  mode: SlipMode;
  items: SlipItem[];
  totalStake: number;
  totalReturn: number;
  totalOdds?: number;
  formatMoney: (amount: number) => string;
  getStake: (item: SlipItem) => number;
  getOdds: (item: SlipItem) => number;
}

export function buildBetPlaceConfirmMessage(
  t: Composer['t'],
  ctx: BetPlaceConfirmContext,
): string {
  if (ctx.mode === 'parlay') {
    const odds =
      ctx.totalOdds != null
        ? String(ctx.totalOdds)
            .replace(/0+$/, '')
            .replace(/\.$/, '')
        : '';
    return t('bet.place_confirm_parlay', {
      count: ctx.items.length,
      stake: ctx.formatMoney(ctx.totalStake),
      odds,
      return: ctx.formatMoney(ctx.totalReturn),
    });
  }

  if (ctx.items.length === 1) {
    const item = ctx.items[0];
    return t('bet.place_confirm_single', {
      pick: item.selectionName,
      odds: ctx.getOdds(item).toFixed(2),
      stake: ctx.formatMoney(ctx.getStake(item)),
      return: ctx.formatMoney(ctx.totalReturn),
    });
  }

  return t('bet.place_confirm_singles', {
    count: ctx.items.length,
    stake: ctx.formatMoney(ctx.totalStake),
    return: ctx.formatMoney(ctx.totalReturn),
  });
}
