import jsforce from "@jsforce/jsforce-node";
import { ar as WEBAPP_URL_FOR_OAUTH } from "./user--mpkKOwW.js";
import { g as getAppKeysFromSlug } from "./getAppKeysFromSlug-IDaQR-Xd.js";
import { e as encodeOAuthState } from "./encodeOAuthState-ADe35aHB.js";
import { g as getSafeRedirectUrl } from "./getSafeRedirectUrl-VGWYovUy.js";
import { g as getInstalledAppPath } from "./getInstalledAppPath-d831DYzR.js";
import { c as createOAuthAppCredential } from "./createOAuthAppCredential-S0A8nWSA.js";
import { d as decodeOAuthState } from "./decodeOAuthState-KhwGAxqw.js";
import { a as appConfig } from "./config-ashvHixX.js";
import SalesforceCRMService from "./CrmService-UuTiY7xM.js";
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("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: `${WEBAPP_URL_FOR_OAUTH}/api/integrations/salesforce/callback`
    }
  });
  const state = 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(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("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: `${WEBAPP_URL_FOR_OAUTH}/api/integrations/salesforce/callback`
    }
  });
  const salesforceTokenInfo = await conn.oauth2.requestToken(code);
  await createOAuthAppCredential({ appId: appConfig.slug, type: appConfig.type }, salesforceTokenInfo, req);
  res.redirect(
    getSafeRedirectUrl(state?.returnTo) ?? 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: SalesforceCRMService
}, Symbol.toStringTag, { value: "Module" }));
export {
  index$1 as api,
  index as lib
};
