{"version":3,"file":"utils.mjs","sources":["../../../../../src/components/DateTimePickers/RelativeTimeRangePicker/utils.ts"],"sourcesContent":["import { RelativeTimeRange, TimeOption } from '@grafana/data';\n\nconst regex = /^now$|^now(\\-|\\+)(\\d{1,10})([wdhms])$/;\n\nexport const mapOptionToRelativeTimeRange = (option: TimeOption): RelativeTimeRange | undefined => {\n  return {\n    from: relativeToSeconds(option.from),\n    to: relativeToSeconds(option.to),\n  };\n};\n\nexport const mapRelativeTimeRangeToOption = (range: RelativeTimeRange): TimeOption => {\n  const from = secondsToRelativeFormat(range.from);\n  const to = secondsToRelativeFormat(range.to);\n\n  return { from, to, display: `${from} to ${to}` };\n};\n\nexport type RangeValidation = {\n  isValid: boolean;\n  errorMessage?: string;\n};\n\nexport const isRangeValid = (relative: string, now = Date.now()): RangeValidation => {\n  if (!isRelativeFormat(relative)) {\n    return {\n      isValid: false,\n      errorMessage: 'Value not in relative time format.',\n    };\n  }\n\n  const seconds = relativeToSeconds(relative);\n\n  if (seconds > Math.ceil(now / 1000)) {\n    return {\n      isValid: false,\n      errorMessage: 'Can not enter value prior to January 1, 1970.',\n    };\n  }\n\n  return { isValid: true };\n};\n\nexport const isRelativeFormat = (format: string): boolean => {\n  return regex.test(format);\n};\n\nconst relativeToSeconds = (relative: string): number => {\n  const match = regex.exec(relative);\n\n  if (!match || match.length !== 4) {\n    return 0;\n  }\n\n  const [, sign, value, unit] = match;\n  const parsed = parseInt(value, 10);\n\n  if (isNaN(parsed)) {\n    return 0;\n  }\n\n  const seconds = parsed * units[unit];\n  return sign === '+' ? seconds * -1 : seconds;\n};\n\nconst units: Record<string, number> = {\n  w: 604800,\n  d: 86400,\n  h: 3600,\n  m: 60,\n  s: 1,\n};\n\nconst secondsToRelativeFormat = (seconds: number): string => {\n  if (seconds === 0) {\n    return 'now';\n  }\n\n  const absoluteSeconds = Math.abs(seconds);\n  if (seconds < 0) {\n    return `now+${formatDuration(absoluteSeconds)}`;\n  }\n\n  return `now-${formatDuration(absoluteSeconds)}`;\n};\n\n/**\n * Formats the given duration in seconds into a human-readable string representation.\n *\n * @param seconds - The duration in seconds.\n * @returns The formatted duration string.\n */\nfunction formatDuration(seconds: number): string {\n  const units = [\n    { unit: 'w', value: 7 * 24 * 60 * 60 },\n    { unit: 'd', value: 24 * 60 * 60 },\n    { unit: 'h', value: 60 * 60 },\n    { unit: 'm', value: 60 },\n    { unit: 's', value: 1 },\n  ];\n\n  for (const { unit, value } of units) {\n    if (seconds % value === 0) {\n      const quotient = seconds / value;\n      return `${quotient}${unit}`;\n    }\n  }\n\n  // If no perfect division, use the least significant unit\n  const leastSignificant = units[units.length - 1];\n  return `${seconds}${leastSignificant.unit}`;\n}\n"],"names":[],"mappings":"AAEA,MAAM,KAAQ,GAAA,uCAAA;AAyCD,MAAA,gBAAA,GAAmB,CAAC,MAA4B,KAAA;AAC3D,EAAO,OAAA,KAAA,CAAM,KAAK,MAAM,CAAA;AAC1B;;;;"}