import tls from 'tls';
/**
 * Adds client certificate to the request configuration options.
 */
export function addClientCertificate(certificate, options) {
    if (!certificate) {
        return;
    }
    const cert = { ...certificate };
    if (cert.type === 'p12') {
        if (!options.pfx) {
            options.pfx = [];
        }
        const struct = {
            buf: Buffer.from(cert.cert.data),
        };
        if (cert.cert.passphrase) {
            struct.passphrase = cert.cert.passphrase;
        }
        if (!Array.isArray(options.pfx)) {
            if (options.pfx) {
                options.pfx = [options.pfx];
            }
            else {
                options.pfx = [];
            }
        }
        options.pfx.push(struct);
    }
    else if (cert.type === 'pem') {
        const typed = cert;
        if (!options.cert) {
            options.cert = [];
        }
        if (!Array.isArray(options.cert)) {
            if (options.cert) {
                options.cert = [options.cert];
            }
            else {
                options.cert = [];
            }
        }
        const added = Buffer.from(typed.cert.data);
        options.cert.push(added);
        if (typed.certKey) {
            if (!Array.isArray(options.key)) {
                if (options.key) {
                    options.key = [options.key];
                }
                else {
                    options.key = [];
                }
            }
            const struct = {
                pem: Buffer.from(typed.certKey.data),
            };
            if (typed.certKey.passphrase) {
                struct.passphrase = typed.certKey.passphrase;
            }
            options.key.push(struct);
        }
    }
}
/**
 * Checks certificate identity using TLS api.
 */
export function checkServerIdentity(host, cert) {
    if (!cert) {
        return new Error('Certificate is required');
    }
    const err = tls.checkServerIdentity(host, cert);
    if (err) {
        return err;
    }
}
//# sourceMappingURL=CertificateManager.js.map