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","sourcesContent":["import type {\n  JsonRpcEngineEndCallback,\n  MethodHandler,\n} from '@metamask/json-rpc-engine';\nimport type { Messenger } from '@metamask/messenger';\nimport type { PermissionControllerHasPermissionAction } from '@metamask/permission-controller';\nimport { providerErrors, rpcErrors } from '@metamask/rpc-errors';\nimport {\n  selectiveUnion,\n  type ScheduleBackgroundEventParams,\n  type ScheduleBackgroundEventResult,\n  type SnapId,\n} from '@metamask/snaps-sdk';\nimport type { InferMatching } from '@metamask/snaps-utils';\nimport {\n  CronjobRpcRequestStruct,\n  ISO8601DateStruct,\n  ISO8601DurationStruct,\n} from '@metamask/snaps-utils';\nimport { StructError, create, object } from '@metamask/superstruct';\nimport { hasProperty, type PendingJsonRpcResponse } from '@metamask/utils';\n\nimport { SnapEndowments } from '../endowments';\nimport type {\n  CronjobControllerScheduleAction,\n  JsonRpcRequestWithOrigin,\n} from '../types';\n\nexport type ScheduleBackgroundEventMethodActions =\n  | PermissionControllerHasPermissionAction\n  | CronjobControllerScheduleAction;\n\n/**\n * Schedule a background event for a Snap. The background event will trigger a\n * JSON-RPC request to the Snap at the scheduled time, handled by the\n * `onCronjob` entry point in the Snap.\n *\n * The schedule can be defined using either an ISO 8601 date or duration string.\n * For example:\n *\n * - Using a date: `2026-12-31T23:59:59Z`\n * - Using a duration: `P1DT2H` (which represents a duration of 1 day and 2\n * hours)\n *\n * @example\n * ```ts\n * const id = await wallet.request({\n *   method: 'snap_scheduleBackgroundEvent',\n *   params: {\n *     date: '2026-12-31T23:59:59Z',\n *     request: {\n *       method: 'mySnapMethod',\n *       params: { foo: 'bar' },\n *     },\n *   },\n * });\n * ```\n */\nexport const scheduleBackgroundEventHandler = {\n  implementation: getScheduleBackgroundEventImplementation,\n  actionNames: [\n    'PermissionController:hasPermission',\n    'CronjobController:schedule',\n  ],\n} satisfies MethodHandler<\n  never,\n  ScheduleBackgroundEventMethodActions,\n  ScheduleBackgroundEventParameters,\n  ScheduleBackgroundEventResult,\n  { origin: SnapId }\n>;\n\nconst ScheduleBackgroundEventParametersWithDateStruct = object({\n  date: ISO8601DateStruct,\n  request: CronjobRpcRequestStruct,\n});\n\nconst ScheduleBackgroundEventParametersWithDurationStruct = object({\n  duration: ISO8601DurationStruct,\n  request: CronjobRpcRequestStruct,\n});\n\nconst ScheduleBackgroundEventParametersStruct = selectiveUnion((val) => {\n  if (hasProperty(val, 'date')) {\n    return ScheduleBackgroundEventParametersWithDateStruct;\n  }\n  return ScheduleBackgroundEventParametersWithDurationStruct;\n});\n\nexport type ScheduleBackgroundEventParameters = InferMatching<\n  typeof ScheduleBackgroundEventParametersStruct,\n  ScheduleBackgroundEventParams\n>;\n\n/**\n * Get the schedule for a background event based on the provided parameters.\n *\n * @param params - The parameters for the background event.\n * @returns The schedule parameters for the background event.\n */\nfunction getSchedule(params: ScheduleBackgroundEventParameters): string {\n  if (hasProperty(params, 'date')) {\n    // TODO: Check why `params.date` is not a string.\n    return params.date as string;\n  }\n\n  return params.duration;\n}\n\n/**\n * The `snap_scheduleBackgroundEvent` method implementation.\n *\n * @param req - The JSON-RPC request object.\n * @param res - The JSON-RPC response object.\n * @param _next - The `json-rpc-engine` \"next\" callback. Not used by this\n * function.\n * @param end - The `json-rpc-engine` \"end\" callback.\n * @param _hooks - The RPC method hooks. Not used by this function.\n * @param messenger - The messenger used to call controller actions.\n * @returns An id representing the background event.\n */\nasync function getScheduleBackgroundEventImplementation(\n  req: JsonRpcRequestWithOrigin<ScheduleBackgroundEventParameters>,\n  res: PendingJsonRpcResponse<ScheduleBackgroundEventResult>,\n  _next: unknown,\n  end: JsonRpcEngineEndCallback,\n  _hooks: never,\n  messenger: Messenger<string, ScheduleBackgroundEventMethodActions>,\n): Promise<void> {\n  const { params, origin } = req;\n\n  if (\n    !messenger.call(\n      'PermissionController:hasPermission',\n      origin,\n      SnapEndowments.Cronjob,\n    )\n  ) {\n    return end(providerErrors.unauthorized());\n  }\n\n  try {\n    const validatedParams = getValidatedParams(params);\n    const { request } = validatedParams;\n    const schedule = getSchedule(validatedParams);\n\n    const id = messenger.call('CronjobController:schedule', {\n      snapId: origin,\n      schedule,\n      request,\n    });\n\n    res.result = id;\n  } catch (error) {\n    return end(error);\n  }\n\n  return end();\n}\n\n/**\n * Validate the scheduleBackgroundEvent method `params` and returns them cast to the correct\n * type. Throws if validation fails.\n *\n * @param params - The unvalidated params object from the method request.\n * @returns The validated resolveInterface method parameter object.\n */\nfunction getValidatedParams(\n  params: unknown,\n): ScheduleBackgroundEventParameters {\n  try {\n    return create(params, ScheduleBackgroundEventParametersStruct);\n  } catch (error) {\n    if (error instanceof StructError) {\n      throw rpcErrors.invalidParams({\n        message: `Invalid params: ${error.message}.`,\n      });\n    }\n    /* istanbul ignore next */\n    throw rpcErrors.internal();\n  }\n}\n"]}