import { l as logger, p as prisma, A as APP_CREDENTIAL_SHARING_ENABLED, F as CREDENTIAL_SYNC_ENDPOINT, C as CREDENTIAL_SYNC_SECRET, o as CREDENTIAL_SYNC_SECRET_HEADER_NAME } from "./i18n-49-r55rr.js";
import { g as getTokenObjectFromCredential, m as markTokenAsExpired, i as invalidateCredential } from "./markTokenAsExpired-H8gTrqCa.js";
import { e as isInMemoryDelegationCredential } from "./user-QM9GuUuS.js";
import { C as CredentialRepository } from "./credential-aTtsoHE4.js";
const log$1 = logger.getSubLogger({
  prefix: ["getCurrentTokenObject"]
});
function buildDummyTokenObjectForDelegationUserCredential() {
  return {
    access_token: "TOKEN_PLACEHOLDER_FOR_DELEGATION_CREDENTIAL"
  };
}
async function getCurrentTokenObject(credential) {
  let inDbCredential;
  if (credential.delegatedToId && isInMemoryDelegationCredential({ credentialId: credential.id })) {
    if (!credential.userId) {
      log$1.error("DelegationCredential: No user id found for delegation credential");
    } else {
      log$1.debug("Getting current token object for delegation credential");
      const delegationUserCredentialInDb = await CredentialRepository.findUniqueByUserIdAndDelegationCredentialId({
        userId: credential.userId,
        delegationCredentialId: credential.delegatedToId
      });
      inDbCredential = delegationUserCredentialInDb;
      if (!inDbCredential) {
        log$1.error("getCurrentTokenObject: No delegation user credential found in db");
        return buildDummyTokenObjectForDelegationUserCredential();
      }
    }
  } else {
    log$1.debug("Getting current token object for non delegation credential");
    inDbCredential = credential;
  }
  if (!inDbCredential) {
    throw new Error("getCurrentTokenObject: No delegation user credential found in db");
  }
  const currentTokenObject = getTokenObjectFromCredential(inDbCredential);
  return currentTokenObject;
}
const log = logger.getSubLogger({ prefix: ["_utils", "oauth", "updateTokenObject"] });
const updateTokenObject = async ({
  tokenObject,
  credentialId
}) => {
  await prisma.credential.update({
    where: {
      id: credentialId
    },
    data: {
      key: tokenObject
    }
  });
};
const updateTokenObjectInDb = async (args) => {
  const { tokenObject } = args;
  if (args.authStrategy === "jwt") {
    const { userId, delegatedToId, credentialType, appId } = args;
    if (!userId) {
      log.error("Cannot update token object in DB for Delegation as userId is not present");
      return;
    }
    if (!delegatedToId) {
      log.error("Cannot update token object in DB for Delegation as delegatedToId is not present");
      return;
    }
    const updated = await CredentialRepository.updateWhereUserIdAndDelegationCredentialId({
      userId,
      delegationCredentialId: delegatedToId,
      data: {
        key: tokenObject
      }
    });
    if (updated.count === 0) {
      log.debug("No delegation-credential found. Creating one");
      await CredentialRepository.createDelegationCredential({
        userId,
        delegationCredentialId: delegatedToId,
        type: credentialType,
        key: tokenObject,
        appId
      });
    }
  } else {
    const { credentialId } = args;
    await CredentialRepository.updateWhereId({
      id: credentialId,
      data: {
        key: tokenObject
      }
    });
  }
};
const credentialSyncVariables = {
  APP_CREDENTIAL_SHARING_ENABLED,
  CREDENTIAL_SYNC_ENDPOINT,
  CREDENTIAL_SYNC_SECRET,
  CREDENTIAL_SYNC_SECRET_HEADER_NAME
};
const oAuthManagerHelper = {
  updateTokenObject,
  markTokenAsExpired,
  invalidateCredential,
  getTokenObjectFromCredential,
  credentialSyncVariables,
  updateTokenObjectInDb,
  getCurrentTokenObject
};
export {
  oAuthManagerHelper as o
};
