import * as hubspot from "@hubspot/api-client";
import { K as WEBAPP_URL_FOR_OAUTH } from "./index-Cw1zbw54.js";
import { g as getAppKeysFromSlug } from "./getAppKeysFromSlug-S62aYXii.js";
import { e as encodeOAuthState } from "./encodeOAuthState-ADe35aHB.js";
import { g as getSafeRedirectUrl } from "./getSafeRedirectUrl-hxmyPhEi.js";
import { g as getInstalledAppPath } from "./getInstalledAppPath-6P3H-VDB.js";
import { c as createOAuthAppCredential } from "./createOAuthAppCredential-UOwy2cXA.js";
import { d as decodeOAuthState } from "./decodeOAuthState-KhwGAxqw.js";
import { m as metadata } from "./_metadata-iu_l30mh.js";
import HubspotCalendarService from "./CrmService-8iwMxP0M.js";
const scopes = ["crm.objects.contacts.read", "crm.objects.contacts.write"];
const hubspotClient$1 = new hubspot.Client();
async function handler$1(req, res) {
  if (req.method !== "GET")
    return res.status(405).json({ message: "Method not allowed" });
  const appKeys = await 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 = `${WEBAPP_URL_FOR_OAUTH}/api/integrations/hubspot/callback`;
  const url = hubspotClient$1.oauth.getAuthorizationUrl(
    clientId,
    redirectUri,
    scopes.join(" "),
    void 0,
    encodeOAuthState(req)
  );
  res.status(200).json({ url });
}
const hubspotClient = new hubspot.Client();
async function handler(req, res) {
  const { code } = req.query;
  const state = 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("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,
    `${WEBAPP_URL_FOR_OAUTH}/api/integrations/hubspot/callback`,
    clientId,
    clientSecret
  );
  hubspotToken.expiryDate = Math.round(Date.now() + hubspotToken.expiresIn * 1e3);
  await createOAuthAppCredential({ appId: metadata.slug, type: metadata.type }, hubspotToken, req);
  res.redirect(
    getSafeRedirectUrl(state?.returnTo) ?? 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: HubspotCalendarService
}, Symbol.toStringTag, { value: "Module" }));
export {
  index$1 as api,
  index as lib,
  metadata
};
