"use strict";
const index = require("./i18n-Bs8xJzoY.cjs");
const markTokenAsExpired = require("./markTokenAsExpired-JTzOdd4i.cjs");
const user = require("./user-7XDeDEMd.cjs");
const credential = require("./credential-yvBSPLqv.cjs");
const log$1 = index.logger.getSubLogger({
  prefix: ["getCurrentTokenObject"]
});
function buildDummyTokenObjectForDelegationUserCredential() {
  return {
    access_token: "TOKEN_PLACEHOLDER_FOR_DELEGATION_CREDENTIAL"
  };
}
async function getCurrentTokenObject(credential$1) {
  let inDbCredential;
  if (credential$1.delegatedToId && user.isInMemoryDelegationCredential({ credentialId: credential$1.id })) {
    if (!credential$1.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 credential.CredentialRepository.findUniqueByUserIdAndDelegationCredentialId({
        userId: credential$1.userId,
        delegationCredentialId: credential$1.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$1;
  }
  if (!inDbCredential) {
    throw new Error("getCurrentTokenObject: No delegation user credential found in db");
  }
  const currentTokenObject = markTokenAsExpired.getTokenObjectFromCredential(inDbCredential);
  return currentTokenObject;
}
const log = index.logger.getSubLogger({ prefix: ["_utils", "oauth", "updateTokenObject"] });
const updateTokenObject = async ({
  tokenObject,
  credentialId
}) => {
  await index.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 credential.CredentialRepository.updateWhereUserIdAndDelegationCredentialId({
      userId,
      delegationCredentialId: delegatedToId,
      data: {
        key: tokenObject
      }
    });
    if (updated.count === 0) {
      log.debug("No delegation-credential found. Creating one");
      await credential.CredentialRepository.createDelegationCredential({
        userId,
        delegationCredentialId: delegatedToId,
        type: credentialType,
        key: tokenObject,
        appId
      });
    }
  } else {
    const { credentialId } = args;
    await credential.CredentialRepository.updateWhereId({
      id: credentialId,
      data: {
        key: tokenObject
      }
    });
  }
};
const credentialSyncVariables = {
  APP_CREDENTIAL_SHARING_ENABLED: index.APP_CREDENTIAL_SHARING_ENABLED,
  CREDENTIAL_SYNC_ENDPOINT: index.CREDENTIAL_SYNC_ENDPOINT,
  CREDENTIAL_SYNC_SECRET: index.CREDENTIAL_SYNC_SECRET,
  CREDENTIAL_SYNC_SECRET_HEADER_NAME: index.CREDENTIAL_SYNC_SECRET_HEADER_NAME
};
const oAuthManagerHelper = {
  updateTokenObject,
  markTokenAsExpired: markTokenAsExpired.markTokenAsExpired,
  invalidateCredential: markTokenAsExpired.invalidateCredential,
  getTokenObjectFromCredential: markTokenAsExpired.getTokenObjectFromCredential,
  credentialSyncVariables,
  updateTokenObjectInDb,
  getCurrentTokenObject
};
exports.oAuthManagerHelper = oAuthManagerHelper;
