import { Encoder } from '../../../utils/encoder.js';
export class GeneralJwsBuilder {
    jws;
    constructor(jws) {
        this.jws = jws;
    }
    static async create(payload, signers = []) {
        const jws = {
            payload: Encoder.bytesToBase64Url(payload),
            signatures: []
        };
        const builder = new GeneralJwsBuilder(jws);
        for (const signer of signers) {
            await builder.addSignature(signer);
        }
        return builder;
    }
    async addSignature(signer) {
        const protectedHeader = {
            kid: signer.keyId,
            alg: signer.algorithm
        };
        const protectedHeaderString = JSON.stringify(protectedHeader);
        const protectedHeaderBase64UrlString = Encoder.stringToBase64Url(protectedHeaderString);
        const signingInputString = `${protectedHeaderBase64UrlString}.${this.jws.payload}`;
        const signingInputBytes = Encoder.stringToBytes(signingInputString);
        const signatureBytes = await signer.sign(signingInputBytes);
        const signature = Encoder.bytesToBase64Url(signatureBytes);
        this.jws.signatures.push({ protected: protectedHeaderBase64UrlString, signature });
    }
    getJws() {
        return this.jws;
    }
}
//# sourceMappingURL=builder.js.map