import jwt from 'jsonwebtoken';
import { AKAPIException } from '../../exceptions/api_exception.js';
export var AKJwtTokenType;
(function (AKJwtTokenType) {
    AKJwtTokenType["access"] = "access";
    AKJwtTokenType["refresh"] = "refresh";
    AKJwtTokenType["setPassword"] = "setPassword";
})(AKJwtTokenType || (AKJwtTokenType = {}));
export class AKJwtService {
    #options;
    constructor(options) {
        this.#options = options;
    }
    static tokenHasType(payload, type) {
        if (typeof type === 'string') {
            return payload.type === type;
        }
        else if (Array.isArray(type)) {
            return type.includes(payload.type);
        }
        throw AKAPIException.auth.token.typeUnknown;
    }
    async #createToken(user, tokenType, expiresIn, extraData = {}) {
        return await jwtAsyncSign({
            ...extraData,
            sub: user.id,
            type: tokenType,
        }, this.#options.secret, { expiresIn });
    }
    async verify(token) {
        return await jwtAsyncVerify(token, this.#options.secret);
    }
    async accessToken(user) {
        return await this.#createToken(user, AKJwtTokenType.access, '1d');
    }
    async refreshToken(user) {
        return await this.#createToken(user, AKJwtTokenType.refresh, '1y');
    }
    async createSetPasswordToken(user) {
        return await this.#createToken(user, AKJwtTokenType.setPassword, '15m');
    }
}
const jwtAsyncSign = (payload, secretOrPrivateKey, options) => new Promise((resolve, reject) => jwt.sign(payload, secretOrPrivateKey, options, (error, token) => error || !token ? reject(error) : resolve(token)));
const jwtAsyncVerify = (token, secretOrPublicKey, options) => new Promise((resolve, reject) => jwt.verify(token, secretOrPublicKey, options, (error, token) => error || !token ? reject(error) : resolve(token)));
//# sourceMappingURL=jwt_service.js.map