import { H as HttpError } from "./http-error-RJXCPZV2.js";
import { p as prisma } from "./index-Cw1zbw54.js";
import { d as decodeOAuthState } from "./decodeOAuthState-KhwGAxqw.js";
import { t as throwIfNotHaveAdminAccessToTeam } from "./throwIfNotHaveAdminAccessToTeam-pjWipYSy.js";
const createOAuthAppCredential = async (appData, key, req) => {
  const userId = req.session?.user.id;
  if (!userId) {
    throw new HttpError({ statusCode: 401, message: "You must be logged in to do this" });
  }
  const state = decodeOAuthState(req);
  if (state?.teamId) {
    await throwIfNotHaveAdminAccessToTeam({ teamId: state?.teamId ?? null, userId });
    return await prisma.credential.create({
      data: {
        type: appData.type,
        key: key || {},
        teamId: state.teamId,
        appId: appData.appId
      }
    });
  }
  return await prisma.credential.create({
    data: {
      type: appData.type,
      key: key || {},
      userId,
      appId: appData.appId
    }
  });
};
const createOAuthAppCredential$1 = createOAuthAppCredential;
export {
  createOAuthAppCredential$1 as c
};
