'use client'; import type { LeagueConfigFormModel } from '@gridpilot/racing/application'; import Input from '@/components/ui/Input'; import SegmentedControl from '@/components/ui/SegmentedControl'; interface LeagueDropSectionProps { form: LeagueConfigFormModel; onChange?: (form: LeagueConfigFormModel) => void; readOnly?: boolean; } export function LeagueDropSection({ form, onChange, readOnly, }: LeagueDropSectionProps) { const disabled = readOnly || !onChange; const dropPolicy = form.dropPolicy; const updateDropPolicy = ( patch: Partial, ) => { if (!onChange) return; onChange({ ...form, dropPolicy: { ...dropPolicy, ...patch, }, }); }; const handleStrategyChange = ( strategy: LeagueConfigFormModel['dropPolicy']['strategy'], ) => { if (strategy === 'none') { updateDropPolicy({ strategy: 'none', n: undefined }); } else if (strategy === 'bestNResults') { const n = dropPolicy.n ?? 6; updateDropPolicy({ strategy: 'bestNResults', n }); } else if (strategy === 'dropWorstN') { const n = dropPolicy.n ?? 2; updateDropPolicy({ strategy: 'dropWorstN', n }); } }; const handleNChange = (value: string) => { const parsed = parseInt(value, 10); updateDropPolicy({ n: Number.isNaN(parsed) || parsed <= 0 ? undefined : parsed, }); }; const computeSummary = () => { if (dropPolicy.strategy === 'none') { return 'All results will count towards the championship.'; } if (dropPolicy.strategy === 'bestNResults') { const n = dropPolicy.n; if (typeof n === 'number' && n > 0) { return `Best ${n} results will count; others are ignored.`; } return 'Best N results will count; others are ignored.'; } if (dropPolicy.strategy === 'dropWorstN') { const n = dropPolicy.n; if (typeof n === 'number' && n > 0) { return `Worst ${n} results will be dropped from the standings.`; } return 'Worst N results will be dropped from the standings.'; } return 'All results will count towards the championship.'; }; const currentStrategyValue = dropPolicy.strategy === 'none' ? 'all' : dropPolicy.strategy === 'bestNResults' ? 'bestN' : 'dropWorstN'; return (

Drop rule

Decide whether to count every round or ignore a few worst results.

{ if (disabled) return; if (value === 'all') { handleStrategyChange('none'); } else if (value === 'bestN') { handleStrategyChange('bestNResults'); } else if (value === 'dropWorstN') { handleStrategyChange('dropWorstN'); } }} /> {(dropPolicy.strategy === 'bestNResults' || dropPolicy.strategy === 'dropWorstN') && (
0 ? String(dropPolicy.n) : '' } onChange={(e) => handleNChange(e.target.value)} disabled={disabled} min={1} />

{dropPolicy.strategy === 'bestNResults' ? 'For example, best 6 of 10 rounds count.' : 'For example, drop the worst 2 results.'}

)}

{computeSummary()}

); }