import { Lorem } from '@pawel-up/data-mock/Lorem.js';
import { Random } from '@pawel-up/data-mock/Random.js';
import { Types } from '@pawel-up/data-mock/Types.js';
import { strToBuffer } from '@pawel-up/data-mock/Utils.js';
import { Certificate } from '../../models/ClientCertificate.js';
export class Certificates {
    lorem;
    types;
    random;
    constructor(init = {}) {
        this.lorem = new Lorem(init);
        this.types = new Types(init.seed);
        this.random = new Random(init.seed);
    }
    /**
     * Creates a certificate definition.
     */
    certificate(opts = {}) {
        const type = opts.type ? opts.type : this.random.pickOne(['p12', 'pem']);
        const data = this.lorem.paragraph();
        const cert = opts.binary ? strToBuffer(data) : data;
        let instance;
        if (type === 'p12') {
            instance = Certificate.fromP12(cert, this.lorem.word());
        }
        else if (type === 'pem') {
            instance = Certificate.fromPem(cert, cert, this.lorem.word());
        }
        else {
            throw new Error(`Invalid certificate type.`);
        }
        if (!opts.noPassphrase) {
            instance.cert.passphrase = this.lorem.word();
            if (type === 'pem') {
                const key = instance.certKey;
                key.passphrase = this.lorem.word();
            }
        }
        return instance.toJSON();
    }
    /**
     * Creates a list of ClientCertificate objects that are used to create a new certificates.
     *
     * @param size The number of certificates to generate.
     * @param opts Create options
     */
    certificates(size = 15, opts = {}) {
        const result = [];
        for (let i = 0; i < size; i++) {
            result[result.length] = this.certificate(opts);
        }
        return result;
    }
}
//# sourceMappingURL=Certificates.js.map