import type { OddsItemRow } from "@/types/api/admin-config";

function oddsItemFingerprint(row: OddsItemRow): string {
  return [
    row.play_code,
    row.prize_scope,
    row.odds_value,
    row.rebate_rate,
    row.commission_rate,
    row.currency_code,
  ].join("|");
}

/** 草稿行是否与已保存版本存在差异。 */
export function oddsDraftIsDirty(draftRows: OddsItemRow[], savedRows: OddsItemRow[]): boolean {
  if (draftRows.length !== savedRows.length) {
    return true;
  }

  const saved = new Map(savedRows.map((row) => [`${row.play_code}|${row.prize_scope}`, row]));

  for (const draft of draftRows) {
    const key = `${draft.play_code}|${draft.prize_scope}`;
    const baseline = saved.get(key);
    if (!baseline) {
      return true;
    }
    if (oddsItemFingerprint(draft) !== oddsItemFingerprint(baseline)) {
      return true;
    }
  }

  return false;
}
