/** 与后台 settlement 模板顺序一致，用于列内排序 */
export const FT_CORRECT_SCORE_ORDER = [
  'SCORE_1_0', 'SCORE_2_0', 'SCORE_2_1', 'SCORE_3_0', 'SCORE_3_1', 'SCORE_3_2',
  'SCORE_4_0', 'SCORE_4_1', 'SCORE_4_2', 'SCORE_4_3', 'OTHER_HOME',
  'SCORE_0_0', 'SCORE_1_1', 'SCORE_2_2', 'SCORE_3_3', 'SCORE_4_4', 'OTHER_DRAW',
  'SCORE_0_1', 'SCORE_0_2', 'SCORE_1_2', 'SCORE_0_3', 'SCORE_1_3', 'SCORE_2_3',
  'SCORE_0_4', 'SCORE_1_4', 'SCORE_2_4', 'SCORE_3_4', 'OTHER_AWAY',
];

export const HT_CORRECT_SCORE_ORDER = [
  'SCORE_1_0', 'SCORE_2_0', 'SCORE_2_1', 'SCORE_3_0', 'OTHER_HOME',
  'SCORE_0_0', 'SCORE_1_1', 'SCORE_2_2', 'OTHER_DRAW',
  'SCORE_0_1', 'SCORE_0_2', 'SCORE_1_2', 'SCORE_0_3', 'OTHER_AWAY',
];

export type CsColumn = 'home' | 'draw' | 'away';

export interface CsSelection {
  id: string;
  selectionCode: string;
  selectionName: string;
  odds: string;
  oddsVersion: string;
  scoreDisplay: string;
}

export function isCorrectScoreMarket(marketType: string) {
  return marketType.includes('CORRECT_SCORE');
}

function orderForMarket(marketType: string) {
  if (marketType === 'FT_CORRECT_SCORE') return FT_CORRECT_SCORE_ORDER;
  return HT_CORRECT_SCORE_ORDER;
}

const OTHER_SCORE_FALLBACK: Record<string, string> = {
  OTHER_HOME: '主胜其它比分',
  OTHER_DRAW: '和局其它比分',
  OTHER_AWAY: '客胜其它比分',
};

export type OtherScoreCode = 'OTHER_HOME' | 'OTHER_DRAW' | 'OTHER_AWAY';

export function otherScoreDisplay(
  code: OtherScoreCode,
  t?: (key: string) => string,
): string {
  if (t) {
    const key = `bet.cs_${code.toLowerCase()}`;
    const v = t(key);
    if (v !== key) return v;
  }
  return OTHER_SCORE_FALLBACK[code];
}

export function parseScoreCode(
  code: string,
  t?: (key: string) => string,
): { display: string; column: CsColumn } | null {
  if (code === 'OTHER_HOME') {
    return { display: otherScoreDisplay('OTHER_HOME', t), column: 'home' };
  }
  if (code === 'OTHER_DRAW') {
    return { display: otherScoreDisplay('OTHER_DRAW', t), column: 'draw' };
  }
  if (code === 'OTHER_AWAY') {
    return { display: otherScoreDisplay('OTHER_AWAY', t), column: 'away' };
  }
  const m = code.match(/^SCORE_(\d+)_(\d+)$/);
  if (!m) return null;
  const h = Number(m[1]);
  const a = Number(m[2]);
  if (h > a) return { display: `${h}:${a}`, column: 'home' };
  if (h < a) return { display: `${h}:${a}`, column: 'away' };
  return { display: `${h}:${a}`, column: 'draw' };
}

function sortByTemplate(items: CsSelection[], template: string[]) {
  return [...items].sort((a, b) => {
    const ia = template.indexOf(a.selectionCode);
    const ib = template.indexOf(b.selectionCode);
    return (ia === -1 ? 999 : ia) - (ib === -1 ? 999 : ib);
  });
}

export function groupCorrectScoreSelections(
  selections: Array<{
    id: string;
    selectionCode: string;
    selectionName: string;
    odds: string;
    oddsVersion: string;
  }>,
  marketType: string,
  t?: (key: string) => string,
) {
  const template = orderForMarket(marketType);
  const home: CsSelection[] = [];
  const draw: CsSelection[] = [];
  const away: CsSelection[] = [];

  for (const sel of selections) {
    const parsed = parseScoreCode(sel.selectionCode, t);
    if (!parsed) continue;
    const row: CsSelection = { ...sel, scoreDisplay: parsed.display };
    if (parsed.column === 'home') home.push(row);
    else if (parsed.column === 'draw') draw.push(row);
    else away.push(row);
  }

  return {
    home: sortByTemplate(home, template),
    draw: sortByTemplate(draw, template),
    away: sortByTemplate(away, template),
  };
}
