"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const jsforce = require("@jsforce/jsforce-node");
const index$2 = require("./user-v6mfWQ2d.cjs");
const getAppKeysFromSlug = require("./getAppKeysFromSlug-W8EkKxxV.cjs");
const encodeOAuthState = require("./encodeOAuthState-VBfelnxQ.cjs");
const getSafeRedirectUrl = require("./getSafeRedirectUrl-ErDpjTNV.cjs");
const getInstalledAppPath = require("./getInstalledAppPath-3c7mqtrk.cjs");
const createOAuthAppCredential = require("./createOAuthAppCredential-ccvXpWYY.cjs");
const decodeOAuthState = require("./decodeOAuthState--7fljpCf.cjs");
const config = require("./config-ZA_TYsE8.cjs");
const CrmService = require("./CrmService-QLsdyzfi.cjs");
async function handler$1(req, res) {
  if (req.method !== "GET")
    return res.status(405).json({ message: "Method not allowed" });
  let consumerKey = "";
  const appKeys = await getAppKeysFromSlug.getAppKeysFromSlug("salesforce");
  if (typeof appKeys.consumer_key === "string")
    consumerKey = appKeys.consumer_key;
  if (!consumerKey)
    return res.status(400).json({ message: "Salesforce client id missing." });
  const salesforceClient = new jsforce.Connection({
    oauth2: {
      clientId: consumerKey,
      redirectUri: `${index$2.WEBAPP_URL_FOR_OAUTH}/api/integrations/salesforce/callback`
    }
  });
  const state = encodeOAuthState.encodeOAuthState(req);
  const url = salesforceClient.oauth2.getAuthorizationUrl({
    scope: "refresh_token full",
    ...state && { state }
  });
  res.status(200).json({ url });
}
async function handler(req, res) {
  const { code } = req.query;
  const state = decodeOAuthState.decodeOAuthState(req);
  if (code === void 0 && typeof code !== "string") {
    res.status(400).json({ message: "`code` must be a string" });
    return;
  }
  if (!req.session?.user?.id) {
    return res.status(401).json({ message: "You must be logged in to do this" });
  }
  let consumerKey = "";
  let consumerSecret = "";
  const appKeys = await getAppKeysFromSlug.getAppKeysFromSlug("salesforce");
  if (typeof appKeys.consumer_key === "string")
    consumerKey = appKeys.consumer_key;
  if (typeof appKeys.consumer_secret === "string")
    consumerSecret = appKeys.consumer_secret;
  if (!consumerKey)
    return res.status(400).json({ message: "Salesforce consumer key missing." });
  if (!consumerSecret)
    return res.status(400).json({ message: "Salesforce consumer secret missing." });
  const conn = new jsforce.Connection({
    oauth2: {
      clientId: consumerKey,
      clientSecret: consumerSecret,
      redirectUri: `${index$2.WEBAPP_URL_FOR_OAUTH}/api/integrations/salesforce/callback`
    }
  });
  const salesforceTokenInfo = await conn.oauth2.requestToken(code);
  await createOAuthAppCredential.createOAuthAppCredential({ appId: config.appConfig.slug, type: config.appConfig.type }, salesforceTokenInfo, req);
  res.redirect(
    getSafeRedirectUrl.getSafeRedirectUrl(state?.returnTo) ?? getInstalledAppPath.getInstalledAppPath({ variant: "other", slug: "salesforce" })
  );
}
const index$1 = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
  __proto__: null,
  add: handler$1,
  callback: handler
}, Symbol.toStringTag, { value: "Module" }));
const index = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
  __proto__: null,
  CrmService: CrmService.default
}, Symbol.toStringTag, { value: "Module" }));
exports.api = index$1;
exports.lib = index;
