

const require_CredoError = require('../../../error/CredoError.js');
require('../../../error/index.js');
const require_replaceError = require('../../../logger/replaceError.js');
const require_TypedArrayEncoder = require('../../../utils/TypedArrayEncoder.js');
const require_JsonEncoder = require('../../../utils/JsonEncoder.js');
require('../../../utils/index.js');
const require_JwtPayload = require('./JwtPayload.js');

//#region src/crypto/jose/jwt/Jwt.ts
var Jwt = class Jwt {
	constructor(options) {
		this.serializedJwt = options.serializedJwt;
		this.payload = options.payload;
		this.header = options.header;
		this.signature = options.signature;
	}
	static fromSerializedJwt(serializedJwt) {
		if (typeof serializedJwt !== "string" || !Jwt.format.test(serializedJwt)) throw new require_CredoError.CredoError(`Invalid JWT. '${serializedJwt}' does not match JWT regex`);
		const [header, payload, signature] = serializedJwt.split(".");
		try {
			return new Jwt({
				header: require_JsonEncoder.JsonEncoder.fromBase64(header),
				payload: require_JwtPayload.JwtPayload.fromJson(require_JsonEncoder.JsonEncoder.fromBase64(payload)),
				signature: require_TypedArrayEncoder.TypedArrayEncoder.fromBase64(signature),
				serializedJwt
			});
		} catch (error) {
			throw new require_CredoError.CredoError(`Invalid JWT. ${error instanceof Error ? error.message : JSON.stringify(error, require_replaceError.replaceError)}`);
		}
	}
};
Jwt.format = /^[A-Za-z0-9-_=]+\.[A-Za-z0-9-_=]+\.?[A-Za-z0-9-_.+/=]*$/;

//#endregion
exports.Jwt = Jwt;