import { createMrtClient, DEFAULT_MRT_ORIGIN } from '../../clients/mrt.js';
import { getLogger } from '../../logging/logger.js';
function describeError(error, action) {
    const message = typeof error === 'object' && error !== null && 'message' in error
        ? String(error.message)
        : JSON.stringify(error);
    return new Error(`Failed to ${action}: ${message}`);
}
export async function listCertificates(options, auth) {
    const logger = getLogger();
    const { organizationSlug, limit, offset, ordering, search, customOnly, origin } = options;
    logger.debug({ organizationSlug }, '[MRT] Listing certificates');
    const client = createMrtClient({ origin: origin || DEFAULT_MRT_ORIGIN }, auth);
    const { data, error } = await client.GET('/api/organizations/{organization_slug}/certificates/', {
        params: {
            path: { organization_slug: organizationSlug },
            query: { limit, offset, ordering, search, custom_only: customOnly },
        },
    });
    if (error)
        throw describeError(error, 'list certificates');
    return {
        count: data.count ?? 0,
        next: data.next ?? null,
        previous: data.previous ?? null,
        certificates: data.results ?? [],
    };
}
export async function getCertificate(options, auth) {
    const { organizationSlug, certId, origin } = options;
    const client = createMrtClient({ origin: origin || DEFAULT_MRT_ORIGIN }, auth);
    const { data, error } = await client.GET('/api/organizations/{organization_slug}/certificates/{cert_id}/', {
        params: { path: { organization_slug: organizationSlug, cert_id: String(certId) } },
    });
    if (error)
        throw describeError(error, 'get certificate');
    return data;
}
export async function createCertificate(options, auth) {
    const logger = getLogger();
    const { organizationSlug, domainName, origin } = options;
    logger.debug({ organizationSlug, domainName }, '[MRT] Creating certificate');
    const client = createMrtClient({ origin: origin || DEFAULT_MRT_ORIGIN }, auth);
    const { data, error } = await client.POST('/api/organizations/{organization_slug}/certificates/', {
        params: { path: { organization_slug: organizationSlug } },
        body: { domain_name: domainName },
    });
    if (error)
        throw describeError(error, 'create certificate');
    return data;
}
export async function deleteCertificate(options, auth) {
    const { organizationSlug, certId, origin } = options;
    const client = createMrtClient({ origin: origin || DEFAULT_MRT_ORIGIN }, auth);
    const { error } = await client.DELETE('/api/organizations/{organization_slug}/certificates/{cert_id}/', {
        params: { path: { organization_slug: organizationSlug, cert_id: String(certId) } },
    });
    if (error)
        throw describeError(error, 'delete certificate');
}
/**
 * Restarts validation for a certificate. Only works for certificates that have
 * not yet been validated.
 */
export async function restartCertificateValidation(options, auth) {
    const { organizationSlug, certId, origin } = options;
    const client = createMrtClient({ origin: origin || DEFAULT_MRT_ORIGIN }, auth);
    const { data, error } = await client.PATCH('/api/organizations/{organization_slug}/certificates/{cert_id}/restart-validation/', {
        params: { path: { organization_slug: organizationSlug, cert_id: String(certId) } },
        body: {},
    });
    if (error)
        throw describeError(error, 'restart certificate validation');
    return data;
}
//# sourceMappingURL=certificate.js.map