import { createMrtClient, DEFAULT_MRT_ORIGIN } from '../../clients/mrt.js';
import { getLogger } from '../../logging/logger.js';
export const ORG_ROLES = {
    0: 'Owner',
    1: 'Member',
};
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 listOrgMembers(options, auth) {
    const logger = getLogger();
    const { organizationSlug, limit, offset, ordering, search, origin } = options;
    logger.debug({ organizationSlug }, '[MRT] Listing organization members');
    const client = createMrtClient({ origin: origin || DEFAULT_MRT_ORIGIN }, auth);
    const { data, error } = await client.GET('/api/organizations/{organization_slug}/members/', {
        params: {
            path: { organization_slug: organizationSlug },
            query: { limit, offset, ordering, search },
        },
    });
    if (error)
        throw describeError(error, 'list organization members');
    return {
        count: data.count ?? 0,
        next: data.next ?? null,
        previous: data.previous ?? null,
        members: data.results ?? [],
    };
}
export async function addOrgMember(options, auth) {
    const logger = getLogger();
    const { organizationSlug, email, role, canViewAllProjects, customDomainCertPermission, origin } = options;
    logger.debug({ organizationSlug, email, role }, '[MRT] Adding organization member');
    const client = createMrtClient({ origin: origin || DEFAULT_MRT_ORIGIN }, auth);
    const body = { user: email, role };
    if (canViewAllProjects !== undefined)
        body.can_view_all_projects = canViewAllProjects;
    if (customDomainCertPermission !== undefined)
        body.custom_domain_cert_permission = customDomainCertPermission;
    const { error } = await client.POST('/api/organizations/{organization_slug}/members/', {
        params: { path: { organization_slug: organizationSlug } },
        body,
    });
    if (error)
        throw describeError(error, 'add organization member');
    // Re-fetch to get the full member shape (POST response omits email/first_name/last_name).
    return getOrgMember({ organizationSlug, email, origin }, auth);
}
export async function getOrgMember(options, auth) {
    const { organizationSlug, email, origin } = options;
    const client = createMrtClient({ origin: origin || DEFAULT_MRT_ORIGIN }, auth);
    const { data, error } = await client.GET('/api/organizations/{organization_slug}/members/{email}/', {
        params: { path: { organization_slug: organizationSlug, email } },
    });
    if (error)
        throw describeError(error, 'get organization member');
    return data;
}
export async function updateOrgMember(options, auth) {
    const { organizationSlug, email, canViewAllProjects, customDomainCertPermission, origin } = options;
    const client = createMrtClient({ origin: origin || DEFAULT_MRT_ORIGIN }, auth);
    const body = {};
    if (canViewAllProjects !== undefined)
        body.can_view_all_projects = canViewAllProjects;
    if (customDomainCertPermission !== undefined)
        body.custom_domain_cert_permission = customDomainCertPermission;
    const { error } = await client.PATCH('/api/organizations/{organization_slug}/members/{email}/', {
        params: { path: { organization_slug: organizationSlug, email } },
        body,
    });
    if (error)
        throw describeError(error, 'update organization member');
    // Re-fetch to get the full member shape (PATCH response omits email/first_name/last_name).
    return getOrgMember({ organizationSlug, email, origin }, auth);
}
export async function removeOrgMember(options, auth) {
    const { organizationSlug, email, origin } = options;
    const client = createMrtClient({ origin: origin || DEFAULT_MRT_ORIGIN }, auth);
    const { error } = await client.DELETE('/api/organizations/{organization_slug}/members/{email}/', {
        params: { path: { organization_slug: organizationSlug, email } },
    });
    if (error)
        throw describeError(error, 'remove organization member');
}
//# sourceMappingURL=organization-member.js.map