import { ratioToPercentUi } from "@/lib/admin-rate-percent";
import { PRIZE_SCOPE_ORDER } from "@/modules/config/doc/prize-scopes";
import type { AdminPlayTypeRow, OddsItemRow } from "@/types/api/admin-config";

/** 赔率/回水配置页展示用：小数比例 → 百分比 UI 字符串 */
export const rateToPercentUi = ratioToPercentUi;

export function inferRebatePercentFromDimension(
  dim: 2 | 3 | 4,
  rows: OddsItemRow[],
  typeList: AdminPlayTypeRow[],
): string {
  const codes = typeList
    .filter((t) => (t.dimension ?? 2) === dim)
    .map((t) => t.play_code)
    .sort((a, b) => a.localeCompare(b));
  const scope = PRIZE_SCOPE_ORDER[0];
  for (const code of codes) {
    const hit = rows.find((r) => r.play_code === code && r.prize_scope === scope);
    if (hit) {
      return rateToPercentUi(String(hit.rebate_rate));
    }
  }
  return "0.00";
}
