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AQ,CAAC,CAAC;IAC5C,IAAI,QAAQ,CAAC,OAAO,EAAE,CAAC;QACrB,kDAAkD;QAClD,MAAM,iBAAiB,GAAG,WAAW,CAAC,QAAQ,CAAC,CAAC;QAChD,OAAO,gBAAQ,CAAC,GAAG,EAAE,CAAC,KAAK,EAAE,CAAC,IAAI,CAAC,iBAAiB,CAAC,CAAC,KAAK,EAAE,CAAC;IAChE,CAAC;IAED,IAAI,CAAC;QACH,MAAM,MAAM,GAAG,IAAA,6BAAe,EAAC,QAAQ,EAAE,EAAE,GAAG,EAAE,IAAI,EAAE,CAAC,CAAC;QACxD,MAAM,IAAI,GAAG,MAAM,CAAC,IAAI,EAAE,CAAC;QAC3B,MAAM,QAAQ,GAAG,gBAAQ,CAAC,UAAU,CAAC,IAAI,CAAC,MAAM,EAAE,CAAC,CAAC;QACpD,IAAA,cAAM,EAAC,QAAQ,CAAC,OAAO,CAAC,CAAC;QAEzB,OAAO,QAAQ,CAAC,KAAK,EAAE,CAAC,KAAK,EAAE,CAAC;IAClC,CAAC;IAAC,MAAM,CAAC;QACP,MAAM,IAAI,KAAK,CACb,oBAAoB,QAAQ,4DAA4D,CACzF,CAAC;IACJ,CAAC;AACH,CAAC;AAnCD,4CAmCC","sourcesContent":["import type { CronjobSpecification } from '@metamask/snaps-utils';\nimport { assert, hasProperty } from '@metamask/utils';\nimport { parseExpression } from 'cron-parser';\nimport { DateTime, Duration } from 'luxon';\n\n/**\n * Get the schedule from a cronjob specification.\n *\n * This function assumes the cronjob specification is valid and contains\n * either a `duration` or an `expression` property.\n *\n * @param specification - The cronjob specification to extract the schedule\n * from.\n * @returns The schedule of the cronjob, which can be either an ISO 8601\n * duration or a cron expression.\n */\nexport function getCronjobSpecificationSchedule(\n  specification: CronjobSpecification,\n) {\n  if (hasProperty(specification, 'duration')) {\n    return specification.duration as string;\n  }\n\n  return specification.expression;\n}\n\n/**\n * Get a duration with a minimum of 1 second. This function assumes the provided\n * duration is valid.\n *\n * @param duration - The duration to validate.\n * @returns The validated duration.\n */\nfunction getDuration(duration: Duration): Duration<true> {\n  if (duration.as('seconds') < 1) {\n    return Duration.fromObject({ seconds: 1 });\n  }\n\n  return duration;\n}\n\n/**\n * Get the next execution date from a schedule, which should be either:\n *\n * - An ISO 8601 date string, or\n * - An ISO 8601 duration string, or\n * - A cron expression.\n *\n * @param schedule - The schedule of the event.\n * @returns The parsed ISO 8601 date at which the event should be executed.\n */\nexport function getExecutionDate(schedule: string) {\n  const date = DateTime.fromISO(schedule, { setZone: true });\n  if (date.isValid) {\n    const now = Date.now();\n\n    // We round to the nearest second to avoid milliseconds in the output.\n    const roundedDate = date.toUTC().startOf('second');\n    if (roundedDate.toMillis() < now) {\n      throw new Error('Cannot schedule an event in the past.');\n    }\n\n    return roundedDate.toISO({\n      suppressMilliseconds: true,\n    });\n  }\n\n  const duration = Duration.fromISO(schedule);\n  if (duration.isValid) {\n    // This ensures the duration is at least 1 second.\n    const validatedDuration = getDuration(duration);\n    return DateTime.now().toUTC().plus(validatedDuration).toISO();\n  }\n\n  try {\n    const parsed = parseExpression(schedule, { utc: true });\n    const next = parsed.next();\n    const nextDate = DateTime.fromJSDate(next.toDate());\n    assert(nextDate.isValid);\n\n    return nextDate.toUTC().toISO();\n  } catch {\n    throw new Error(\n      `Unable to parse \"${schedule}\" as ISO 8601 date, ISO 8601 duration, or cron expression.`,\n    );\n  }\n}\n"]}