"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const hubspot = require("@hubspot/api-client");
const index$2 = require("./index-QSf6WJj0.cjs");
const getAppKeysFromSlug = require("./getAppKeysFromSlug-duun9y1Q.cjs");
const encodeOAuthState = require("./encodeOAuthState-VBfelnxQ.cjs");
const getSafeRedirectUrl = require("./getSafeRedirectUrl-MkOChEjJ.cjs");
const getInstalledAppPath = require("./getInstalledAppPath-2GQAo7UC.cjs");
const createOAuthAppCredential = require("./createOAuthAppCredential--aER13Ig.cjs");
const decodeOAuthState = require("./decodeOAuthState--7fljpCf.cjs");
const _metadata = require("./_metadata-QkSH3hVJ.cjs");
const CrmService = require("./CrmService-ByJAWzsi.cjs");
function _interopNamespaceDefault(e) {
  const n = Object.create(null, { [Symbol.toStringTag]: { value: "Module" } });
  if (e) {
    for (const k in e) {
      if (k !== "default") {
        const d = Object.getOwnPropertyDescriptor(e, k);
        Object.defineProperty(n, k, d.get ? d : {
          enumerable: true,
          get: () => e[k]
        });
      }
    }
  }
  n.default = e;
  return Object.freeze(n);
}
const hubspot__namespace = /* @__PURE__ */ _interopNamespaceDefault(hubspot);
const scopes = ["crm.objects.contacts.read", "crm.objects.contacts.write"];
const hubspotClient$1 = new hubspot__namespace.Client();
async function handler$1(req, res) {
  if (req.method !== "GET")
    return res.status(405).json({ message: "Method not allowed" });
  const appKeys = await getAppKeysFromSlug.getAppKeysFromSlug("hubspot");
  let clientId = "";
  if (typeof appKeys.client_id === "string")
    clientId = appKeys.client_id;
  if (!clientId)
    return res.status(400).json({ message: "HubSpot client id missing." });
  const redirectUri = `${index$2.WEBAPP_URL_FOR_OAUTH}/api/integrations/hubspot/callback`;
  const url = hubspotClient$1.oauth.getAuthorizationUrl(
    clientId,
    redirectUri,
    scopes.join(" "),
    void 0,
    encodeOAuthState.encodeOAuthState(req)
  );
  res.status(200).json({ url });
}
const hubspotClient = new hubspot__namespace.Client();
async function handler(req, res) {
  const { code } = req.query;
  const state = decodeOAuthState.decodeOAuthState(req);
  if (code && 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 clientId = "";
  let clientSecret = "";
  const appKeys = await getAppKeysFromSlug.getAppKeysFromSlug("hubspot");
  if (typeof appKeys.client_id === "string")
    clientId = appKeys.client_id;
  if (typeof appKeys.client_secret === "string")
    clientSecret = appKeys.client_secret;
  if (!clientId)
    return res.status(400).json({ message: "HubSpot client id missing." });
  if (!clientSecret)
    return res.status(400).json({ message: "HubSpot client secret missing." });
  const hubspotToken = await hubspotClient.oauth.tokensApi.createToken(
    "authorization_code",
    code,
    `${index$2.WEBAPP_URL_FOR_OAUTH}/api/integrations/hubspot/callback`,
    clientId,
    clientSecret
  );
  hubspotToken.expiryDate = Math.round(Date.now() + hubspotToken.expiresIn * 1e3);
  await createOAuthAppCredential.createOAuthAppCredential({ appId: _metadata.metadata.slug, type: _metadata.metadata.type }, hubspotToken, req);
  res.redirect(
    getSafeRedirectUrl.getSafeRedirectUrl(state?.returnTo) ?? getInstalledAppPath.getInstalledAppPath({ variant: "other", slug: "hubspot" })
  );
}
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.metadata = _metadata.metadata;
exports.api = index$1;
exports.lib = index;
