import { Runtime } from 'aws-cdk-lib/aws-lambda'
import { BundlingOptions, OutputFormat } from 'aws-cdk-lib/aws-lambda-nodejs'

export const LAMBDA_RUNTIME = Runtime.NODEJS_24_X

/**
 * Datadog Lambda layer versions, updated by Renovate (see renovate.json customManagers).
 * Node layer version = minor of the datadog-lambda-js release (v12.133.0 -> 133),
 * Extension layer version = datadog-lambda-extension release tag (v87 -> 87).
 */
// renovate: datasource=github-releases depName=DataDog/datadog-lambda-js extractVersion=^v\d+\.(?<version>\d+)\.\d+$
export const DATADOG_NODE_LAYER_VERSION = 133
// renovate: datasource=github-releases depName=DataDog/datadog-lambda-extension extractVersion=^v(?<version>\d+)$
export const DATADOG_EXTENSION_LAYER_VERSION = 87

/**
 * Basic bundling options for bundling Lambda code in ESM format.
 * The AWS SDK is not bundled in the built asset, will default
 * to the SDK version available in the Lambda runtime
 */
export const LAMBDA_BUNDLING_OPTS_NODEJS_ESM: BundlingOptions = {
  format: OutputFormat.ESM,
  minify: false,
  sourceMap: true,
  mainFields: ['module', 'main'], // Prefer ESM implementation over CommonJS if module ships with both
  esbuildArgs: {
    '--out-extension:.js': '.mjs',
  },
  externalModules: ['@aws-sdk'],
  // Make CommonJS require work in ESM, used by wrapped CJS code from dependencies that only ship in CJS
  banner: `import { createRequire } from 'module'; const require = createRequire(import.meta.url);`,
}

/**
 * Basic bundling options for bundling Lambda code in ESM format.
 * Current AWS SDK version is bundled in the Lambda asset
 */
export const LAMBDA_BUNDLING_OPTS_NODEJS_ESM_BUNDLE_SDK: BundlingOptions = {
  format: OutputFormat.ESM,
  bundleAwsSDK: true,
  minify: false,
  sourceMap: true,
  mainFields: ['module', 'main'], // Prefer ESM implementation over CommonJS if module ships with both
  esbuildArgs: {
    '--out-extension:.js': '.mjs',
  },
  // Make CommonJS require work in ESM, used by wrapped CJS code from dependencies that only ship in CJS
  banner: `import { createRequire } from 'module'; const require = createRequire(import.meta.url);`,
}
