import {
  FT_CORRECT_SCORE_TEMPLATE,
  HT_CORRECT_SCORE_TEMPLATE,
} from '@thebet365/shared';

const SNAPSHOT_1X2: Record<string, string> = {
  主胜: 'HOME',
  客胜: 'AWAY',
  和局: 'DRAW',
  平局: 'DRAW',
};

/** 盘口 code 缺失时，从下单快照名推断标准 selectionCode */
export function resolveSelectionCode(
  selectionCode: string | null | undefined,
  nameSnapshot: string,
): string {
  if (selectionCode?.trim()) return selectionCode.trim();

  const name = nameSnapshot.trim();
  if (SNAPSHOT_1X2[name]) return SNAPSHOT_1X2[name];
  if (name.startsWith('大')) return 'OVER';
  if (name.startsWith('小')) return 'UNDER';
  if (name.startsWith('主')) return 'HOME';
  if (name.startsWith('客')) return 'AWAY';

  if (name.includes('-')) {
    return `SCORE_${name.replace('-', '_')}`;
  }
  return name;
}

export function templateScoresForMarket(marketType: string): string[] {
  if (marketType === 'FT_CORRECT_SCORE') return [...FT_CORRECT_SCORE_TEMPLATE];
  if (marketType.includes('CORRECT_SCORE')) return [...HT_CORRECT_SCORE_TEMPLATE];
  return [];
}
