"use strict";
const httpError = require("./http-error-mkY8Ke7V.cjs");
const index = require("./user-v6mfWQ2d.cjs");
const decodeOAuthState = require("./decodeOAuthState--7fljpCf.cjs");
const throwIfNotHaveAdminAccessToTeam = require("./throwIfNotHaveAdminAccessToTeam-OTNj--da.cjs");
const createOAuthAppCredential = async (appData, key, req) => {
  const userId = req.session?.user.id;
  if (!userId) {
    throw new httpError.HttpError({ statusCode: 401, message: "You must be logged in to do this" });
  }
  const state = decodeOAuthState.decodeOAuthState(req);
  if (state?.teamId) {
    await throwIfNotHaveAdminAccessToTeam.throwIfNotHaveAdminAccessToTeam({ teamId: state?.teamId ?? null, userId });
    return await index.prisma.credential.create({
      data: {
        type: appData.type,
        key: key || {},
        teamId: state.teamId,
        appId: appData.appId
      }
    });
  }
  return await index.prisma.credential.create({
    data: {
      type: appData.type,
      key: key || {},
      userId,
      appId: appData.appId
    }
  });
};
const createOAuthAppCredential$1 = createOAuthAppCredential;
exports.createOAuthAppCredential = createOAuthAppCredential$1;
