Object.defineProperty(exports, '__esModule', { value: true });

const core = require('@sentry/core');
const utils = require('@sentry/utils');
const async = require('./async.js');
const client = require('./client.js');
const wintercgFetch = require('./integrations/wintercg-fetch.js');
const index = require('./transports/index.js');
const vercel = require('./utils/vercel.js');

const nodeStackParser = utils.createStackParser(utils.nodeStackLineParser());

/** @deprecated Use `getDefaultIntegrations(options)` instead. */
const defaultIntegrations = [
  core.inboundFiltersIntegration(),
  core.functionToStringIntegration(),
  core.linkedErrorsIntegration(),
  wintercgFetch.winterCGFetchIntegration(),
];

/** Get the default integrations for the browser SDK. */
function getDefaultIntegrations(options) {
  return [
    // eslint-disable-next-line deprecation/deprecation
    ...defaultIntegrations,
    ...(options.sendDefaultPii ? [core.requestDataIntegration()] : []),
  ];
}

/** Inits the Sentry NextJS SDK on the Edge Runtime. */
function init(options = {}) {
  async.setAsyncLocalStorageAsyncContextStrategy();

  if (options.defaultIntegrations === undefined) {
    options.defaultIntegrations = getDefaultIntegrations(options);
  }

  if (options.dsn === undefined && process.env.SENTRY_DSN) {
    options.dsn = process.env.SENTRY_DSN;
  }

  if (options.tracesSampleRate === undefined && process.env.SENTRY_TRACES_SAMPLE_RATE) {
    const tracesSampleRate = parseFloat(process.env.SENTRY_TRACES_SAMPLE_RATE);
    if (isFinite(tracesSampleRate)) {
      options.tracesSampleRate = tracesSampleRate;
    }
  }

  if (options.release === undefined) {
    const detectedRelease = getSentryRelease();
    if (detectedRelease !== undefined) {
      options.release = detectedRelease;
    } else {
      // If release is not provided, then we should disable autoSessionTracking
      options.autoSessionTracking = false;
    }
  }

  options.environment =
    options.environment || process.env.SENTRY_ENVIRONMENT || vercel.getVercelEnv(false) || process.env.NODE_ENV;

  if (options.autoSessionTracking === undefined && options.dsn !== undefined) {
    options.autoSessionTracking = true;
  }

  if (options.instrumenter === undefined) {
    options.instrumenter = 'sentry';
  }

  const clientOptions = {
    ...options,
    stackParser: utils.stackParserFromStackParserOptions(options.stackParser || nodeStackParser),
    integrations: core.getIntegrationsToSetup(options),
    transport: options.transport || index.makeEdgeTransport,
  };

  core.initAndBind(client.VercelEdgeClient, clientOptions);
}

/**
 * Returns a release dynamically from environment variables.
 */
function getSentryRelease(fallback) {
  // Always read first as Sentry takes this as precedence
  if (process.env.SENTRY_RELEASE) {
    return process.env.SENTRY_RELEASE;
  }

  // This supports the variable that sentry-webpack-plugin injects
  if (utils.GLOBAL_OBJ.SENTRY_RELEASE && utils.GLOBAL_OBJ.SENTRY_RELEASE.id) {
    return utils.GLOBAL_OBJ.SENTRY_RELEASE.id;
  }

  return (
    // GitHub Actions - https://help.github.com/en/actions/configuring-and-managing-workflows/using-environment-variables#default-environment-variables
    process.env.GITHUB_SHA ||
    // Vercel - https://vercel.com/docs/v2/build-step#system-environment-variables
    process.env.VERCEL_GIT_COMMIT_SHA ||
    process.env.VERCEL_GITHUB_COMMIT_SHA ||
    process.env.VERCEL_GITLAB_COMMIT_SHA ||
    process.env.VERCEL_BITBUCKET_COMMIT_SHA ||
    // Zeit (now known as Vercel)
    process.env.ZEIT_GITHUB_COMMIT_SHA ||
    process.env.ZEIT_GITLAB_COMMIT_SHA ||
    process.env.ZEIT_BITBUCKET_COMMIT_SHA ||
    fallback
  );
}

exports.defaultIntegrations = defaultIntegrations;
exports.getDefaultIntegrations = getDefaultIntegrations;
exports.getSentryRelease = getSentryRelease;
exports.init = init;
//# sourceMappingURL=sdk.js.map