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}`);
}
/**
 * Lists members of an MRT organization.
 *
 * @param options - List options including organization slug and optional pagination/filtering parameters
 * @param auth - Authentication strategy (ApiKeyStrategy or other AuthStrategy implementation)
 * @returns Promise resolving to paginated list of organization members with count, next, previous, and members array
 * @throws Error if the request fails
 *
 * @example
 * ```typescript
 * import { ApiKeyStrategy } from '@salesforce/b2c-tooling-sdk/auth';
 * import { listOrgMembers } from '@salesforce/b2c-tooling-sdk/operations/mrt';
 *
 * const auth = new ApiKeyStrategy(process.env.MRT_API_KEY!, 'Authorization');
 *
 * const result = await listOrgMembers({
 *   organizationSlug: 'my-org'
 * }, auth);
 * ```
 */
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 ?? [],
    };
}
/**
 * Adds a member to an MRT organization.
 *
 * @param options - Add member options
 * @param options.organizationSlug - Organization slug identifier
 * @param options.email - Email of the member to add
 * @param options.role - Role value (0 = Owner, 1 = Member)
 * @param options.canViewAllProjects - Optional: grant permission to view all projects
 * @param options.customDomainCertPermission - Optional: custom domain certificate permission (1 = Disabled, 2 = Enabled)
 * @param options.origin - Optional: MRT API origin endpoint
 * @param auth - Authentication strategy (e.g., ApiKeyStrategy)
 * @returns The created organization member
 * @throws Error if the request fails
 *
 * @example
 * ```typescript
 * import { ApiKeyStrategy } from '@salesforce/b2c-tooling-sdk/auth';
 * import { addOrgMember } from '@salesforce/b2c-tooling-sdk/operations/mrt';
 *
 * const auth = new ApiKeyStrategy(process.env.MRT_API_KEY!, 'Authorization');
 *
 * const member = await addOrgMember({
 *   organizationSlug: 'my-org',
 *   email: 'user@example.com',
 *   role: 1  // Member
 * }, auth);
 * ```
 */
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);
}
/**
 * Gets an organization member by email.
 *
 * @param options - Get options including organization slug and email
 * @param auth - Authentication strategy (ApiKeyStrategy)
 * @returns The organization member details
 * @throws Error if the request fails or member is not found
 *
 * @example
 * ```typescript
 * import { ApiKeyStrategy } from '@salesforce/b2c-tooling-sdk/auth';
 * import { getOrgMember } from '@salesforce/b2c-tooling-sdk/operations/mrt';
 *
 * const auth = new ApiKeyStrategy(process.env.MRT_API_KEY!, 'Authorization');
 *
 * const member = await getOrgMember({
 *   organizationSlug: 'my-org',
 *   email: 'user@example.com'
 * }, auth);
 *
 * console.log(`Member role: ${member.role}`);
 * ```
 */
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;
}
/**
 * Updates an organization member's permissions and settings.
 *
 * @param options - Update options with organizationSlug, email, and optional canViewAllProjects and customDomainCertPermission
 * @param auth - Authentication strategy (required for API authorization)
 * @returns Promise resolving to the updated organization member with full member details
 * @throws Error if the API request fails
 */
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);
}
/**
 * Removes a member from an MRT organization.
 *
 * @param options - Remove options
 * @param options.organizationSlug - Slug of the organization
 * @param options.email - Email address of the member to remove
 * @param options.origin - Optional MRT API origin (defaults to DEFAULT_MRT_ORIGIN)
 * @param auth - Authentication strategy
 * @throws {Error} If the API request fails
 */
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