"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
exports.getServerSession = getServerSession;
exports.unstable_getServerSession = unstable_getServerSession;

var _core = require("../core");

var _detectHost = require("../utils/detect-host");

var _utils = require("./utils");

async function NextAuthHandler(req, res, options) {
  var _ref, _options$secret, _options$jwt, _ref2, _handler$status, _handler$cookies, _handler$headers;

  const {
    nextauth,
    ...query
  } = req.query;
  options.secret = (_ref = (_options$secret = options.secret) !== null && _options$secret !== void 0 ? _options$secret : (_options$jwt = options.jwt) === null || _options$jwt === void 0 ? void 0 : _options$jwt.secret) !== null && _ref !== void 0 ? _ref : process.env.NEXTAUTH_SECRET;
  const handler = await (0, _core.AuthHandler)({
    req: {
      host: (0, _detectHost.detectHost)(req.headers["x-forwarded-host"]),
      body: req.body,
      query,
      cookies: req.cookies,
      headers: req.headers,
      method: req.method,
      action: nextauth === null || nextauth === void 0 ? void 0 : nextauth[0],
      providerId: nextauth === null || nextauth === void 0 ? void 0 : nextauth[1],
      error: (_ref2 = req.query.error) !== null && _ref2 !== void 0 ? _ref2 : nextauth === null || nextauth === void 0 ? void 0 : nextauth[1]
    },
    options
  });
  res.status((_handler$status = handler.status) !== null && _handler$status !== void 0 ? _handler$status : 200);
  (_handler$cookies = handler.cookies) === null || _handler$cookies === void 0 ? void 0 : _handler$cookies.forEach(cookie => (0, _utils.setCookie)(res, cookie));
  (_handler$headers = handler.headers) === null || _handler$headers === void 0 ? void 0 : _handler$headers.forEach(h => res.setHeader(h.key, h.value));

  if (handler.redirect) {
    var _req$body;

    if (((_req$body = req.body) === null || _req$body === void 0 ? void 0 : _req$body.json) !== "true") {
      res.status(302).setHeader("Location", handler.redirect);
      return res.end();
    }

    return res.json({
      url: handler.redirect
    });
  }

  return res.send(handler.body);
}

function NextAuth(...args) {
  if (args.length === 1) {
    return async (req, res) => await NextAuthHandler(req, res, args[0]);
  }

  return NextAuthHandler(args[0], args[1], args[2]);
}

var _default = NextAuth;
exports.default = _default;
let experimentalRSCWarningShown = false;

async function getServerSession(...args) {
  var _options$secret2;

  const isRSC = args.length === 0 || args.length === 1;

  if (!experimentalRSCWarningShown && isRSC && process.env.NODE_ENV !== "production") {
    console.warn("[next-auth][warn][EXPERIMENTAL_API]", "\n`getServerSession` is used in a React Server Component.", `\nhttps://next-auth.js.org/configuration/nextjs#getServerSession}`, `\nhttps://next-auth.js.org/warnings#EXPERIMENTAL_API`);
    experimentalRSCWarningShown = true;
  }

  let req, res, options;

  if (isRSC) {
    options = Object.assign({}, args[0], {
      providers: []
    });

    const {
      headers,
      cookies
    } = require("next/headers");

    req = {
      headers: Object.fromEntries(headers()),
      cookies: Object.fromEntries(cookies().getAll().map(c => [c.name, c.value]))
    };
    res = {
      getHeader() {},

      setCookie() {},

      setHeader() {}

    };
  } else {
    req = args[0];
    res = args[1];
    options = Object.assign({}, args[2], {
      providers: []
    });
  }

  options.secret = (_options$secret2 = options.secret) !== null && _options$secret2 !== void 0 ? _options$secret2 : process.env.NEXTAUTH_SECRET;
  const session = await (0, _core.AuthHandler)({
    options,
    req: {
      host: (0, _detectHost.detectHost)(req.headers["x-forwarded-host"]),
      action: "session",
      method: "GET",
      cookies: req.cookies,
      headers: req.headers
    }
  });
  const {
    body,
    cookies,
    status = 200
  } = session;
  cookies === null || cookies === void 0 ? void 0 : cookies.forEach(cookie => (0, _utils.setCookie)(res, cookie));

  if (body && typeof body !== "string" && Object.keys(body).length) {
    if (status === 200) {
      if (isRSC) delete body.expires;
      return body;
    }

    throw new Error(body.message);
  }

  return null;
}

let deprecatedWarningShown = false;

async function unstable_getServerSession(...args) {
  if (!deprecatedWarningShown && process.env.NODE_ENV !== "production") {
    console.warn("`unstable_getServerSession` has been renamed to `getServerSession`.");
    deprecatedWarningShown = true;
  }

  return await getServerSession(...args);
}