export function parseAgeMonths(input) {
  if (input == null) return null;
  var s = String(input).toLowerCase().trim();
  if (!s) return null;
  // Plain number with no unit: keep as months; user can add a unit if they meant years.
  if (/^[\d.]+$/.test(s)) {
    var n = parseFloat(s);
    if (isNaN(n)) return null;
    return n;
  }
  var total = 0;
  var matched = false;
  var my = s.match(/([\d.]+)\s*(?:years|year|yrs|yr|y)(?![a-z])/);
  if (my) { total += parseFloat(my[1]) * 12; matched = true; }
  var mm = s.match(/([\d.]+)\s*(?:months|month|mos|mo|m)(?![a-z])/);
  if (mm) { total += parseFloat(mm[1]); matched = true; }
  var mw = s.match(/([\d.]+)\s*(?:weeks|week|wks|wk|w)(?![a-z])/);
  if (mw) { total += parseFloat(mw[1]) * 7 / 30.4375; matched = true; }
  var md = s.match(/([\d.]+)\s*(?:days|day|d)(?![a-z])/);
  if (md) { total += parseFloat(md[1]) / 30.4375; matched = true; }
  return matched ? total : null;
}

export function formatParsedAge(months) {
  if (months < 1) {
    var d = Math.round(months * 30.4375);
    return '= ' + d + ' day' + (d === 1 ? '' : 's') + ' (' + months.toFixed(2) + ' mo)';
  }
  if (months < 24) {
    return '= ' + (Math.round(months * 10) / 10) + ' months';
  }
  var yr = Math.floor(months / 12);
  var mo = Math.round(months - yr * 12);
  if (mo === 12) { yr += 1; mo = 0; }
  return '= ' + yr + ' yr' + (mo ? ' ' + mo + ' mo' : '') + ' (' + Math.round(months) + ' mo total)';
}
