import { startOfYear, endOfYear, startOfMonth, endOfMonth, addYears } from 'date-fns' const MONTHS: Record = { JAN: 0, FEB: 1, MAR: 2, APR: 3, MAY: 4, JUN: 5, JUL: 6, AUG: 7, SEP: 8, OCT: 9, NOV: 10, DEC: 11, } export function parseDateRangeFromTitle( title: string, referenceYear: number = new Date().getFullYear() ): { start: Date; end: Date } | null { const match = title.match(/^\[([^\]]+)\]/) if (!match) return null const tag = match[1].toUpperCase() const baseDate = new Date(referenceYear, 0, 1) const yearMatch = tag.match(/^(\d+)YR$/) if (yearMatch) { const years = parseInt(yearMatch[1]) return { start: startOfYear(baseDate), end: endOfYear(addYears(baseDate, years - 1)) } } if (tag === 'H1') return { start: new Date(referenceYear, 0, 1), end: new Date(referenceYear, 5, 30) } if (tag === 'H2') return { start: new Date(referenceYear, 6, 1), end: new Date(referenceYear, 11, 31) } const quarterMatch = tag.match(/^Q([1-4])$/) if (quarterMatch) { const q = parseInt(quarterMatch[1]) const startMonth = (q - 1) * 3 return { start: new Date(referenceYear, startMonth, 1), end: endOfMonth(new Date(referenceYear, startMonth + 2, 1)) } } if (tag in MONTHS) { const month = MONTHS[tag] return { start: startOfMonth(new Date(referenceYear, month, 1)), end: endOfMonth(new Date(referenceYear, month, 1)) } } const weekMatch = tag.match(/^W(\d+)$/) if (weekMatch) { const week = parseInt(weekMatch[1]) const start = new Date(referenceYear, 0, 1 + (week - 1) * 7) const end = new Date(start) end.setDate(end.getDate() + 6) return { start, end } } return null } export function getHierarchyLevel(title: string): number { const match = title.match(/^\[([^\]]+)\]/) if (!match) return 0 const tag = match[1].toUpperCase() if (/^\d+YR$/.test(tag)) return 100 + parseInt(tag) if (/^H[12]$/.test(tag)) return 50 if (/^Q[1-4]$/.test(tag)) return 40 if (tag in MONTHS) return 30 if (/^W\d+$/.test(tag)) return 20 return 10 }