import { bufferToBase64, strToBuffer } from '@pawel-up/data-mock/Utils.js';
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';
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 data = this.lorem.paragraph();
        const result = {
            data,
        };
        if (opts.binary) {
            result.data = strToBuffer(data);
        }
        if (!opts.noPassphrase) {
            result.passphrase = this.lorem.word();
        }
        return result;
    }
    /**
     * Generates a Client Certificate index object.
     * @param opts Create options
     */
    certificateIndex(opts = {}) {
        const type = opts.type ? opts.type : this.random.pickOne(['p12', 'pem']);
        const result = {
            type,
            name: this.lorem.word(),
        };
        if (!opts.noCreated) {
            result.created = this.types.datetime().getTime();
        }
        return result;
    }
    /**
     * @param opts Create options
     */
    requestCertificate(opts = {}) {
        const type = opts.type ? opts.type : this.random.pickOne(['p12', 'pem']);
        const cert = this.certificate(opts);
        const name = this.lorem.word();
        const result = {
            type,
            name,
            cert,
        };
        if (!opts.noKey) {
            result.key = this.certificate(opts);
        }
        return result;
    }
    /**
     * Creates a ClientCertificate object that is used to create a new certificate.
     * @param opts Create options
     */
    clientCertificate(opts = {}) {
        const index = this.certificateIndex(opts);
        const data = this.requestCertificate(opts);
        const result = {
            ...index,
            cert: data.cert,
        };
        if (data.key) {
            result.key = data.key;
        }
        return result;
    }
    /**
     * 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
     */
    clientCertificates(size = 15, opts = {}) {
        const result = [];
        for (let i = 0; i < size; i++) {
            result[result.length] = this.clientCertificate(opts);
        }
        return result;
    }
    /**
     * Creates a ClientCertificate transformed to the export object.
     */
    exportClientCertificate(opts = {}) {
        const item = this.clientCertificate(opts);
        if (item.key) {
            item.pKey = item.key;
        }
        item.key = this.types.uuid();
        item.kind = 'ARC#ClientCertificate';
        return item;
    }
    /**
     * Creates a list of ClientCertificates transformed for the export object.
     */
    exportClientCertificates(size = 15, opts = {}) {
        const result = [];
        for (let i = 0; i < size; i++) {
            result[result.length] = this.exportClientCertificate(opts);
        }
        return result;
    }
    /**
     * @param cert Certificate definition. See class description.
     */
    toStore(cert) {
        if (Array.isArray(cert)) {
            return cert.map((info) => this.toStore(info));
        }
        if (typeof cert.data === 'string') {
            return cert;
        }
        const data = bufferToBase64(cert.data);
        const copy = { ...cert, type: 'buffer', data };
        return copy;
    }
}
//# sourceMappingURL=Certificates.js.map