"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateJWT = void 0;
const crypto_1 = require("crypto");
const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
/**
 * Generates a JWT for Epic's FHIR integration.
 * @param clientId - The client ID for the app.
 * @param tokenEndpoint - The FHIR authorization server's token endpoint URL.
 * @param privateKey - The private key to sign the JWT.
 * @returns The signed JWT.
 */
const generateJWT = ({ clientId, tokenEndpoint, privateKey, kid, jku, }) => {
    const nowInSeconds = Math.floor(Date.now() / 1000);
    const jwtHeader = {
        alg: 'RS384',
        typ: 'JWT',
        kid,
        jku,
    };
    const jwtPayload = {
        iss: clientId,
        sub: clientId,
        aud: tokenEndpoint,
        jti: (0, crypto_1.randomUUID)(),
        exp: nowInSeconds + 300, // 5 minutes in the future
        nbf: nowInSeconds,
        iat: nowInSeconds,
    };
    const token = jsonwebtoken_1.default.sign(jwtPayload, privateKey, {
        algorithm: 'RS384',
        header: jwtHeader,
    });
    return token;
};
exports.generateJWT = generateJWT;
//# sourceMappingURL=generateJWT.js.map