import { createMrtClient, DEFAULT_MRT_ORIGIN } from '../../clients/mrt.js';
import { getLogger } from '../../logging/logger.js';
/**
 * Lists organizations accessible to the authenticated user.
 *
 * @param options - List options including pagination
 * @param auth - Authentication strategy (ApiKeyStrategy)
 * @returns Paginated list of organizations
 * @throws Error if request fails
 *
 * @example
 * ```typescript
 * import { ApiKeyStrategy } from '@salesforce/b2c-tooling-sdk/auth';
 * import { listOrganizations } from '@salesforce/b2c-tooling-sdk/operations/mrt';
 *
 * const auth = new ApiKeyStrategy(process.env.MRT_API_KEY!, 'Authorization');
 *
 * const result = await listOrganizations({}, auth);
 * console.log(`Found ${result.count} organizations`);
 *
 * for (const org of result.organizations) {
 *   console.log(`- ${org.name} (${org.slug})`);
 * }
 * ```
 */
export async function listOrganizations(options, auth) {
    const logger = getLogger();
    const { limit, offset, origin } = options;
    logger.debug({ limit, offset }, '[MRT] Listing organizations');
    const client = createMrtClient({ origin: origin || DEFAULT_MRT_ORIGIN }, auth);
    const { data, error } = await client.GET('/api/organizations/', {
        params: {
            query: {
                limit,
                offset,
            },
        },
    });
    if (error) {
        const errorMessage = typeof error === 'object' && error !== null && 'message' in error
            ? String(error.message)
            : JSON.stringify(error);
        throw new Error(`Failed to list organizations: ${errorMessage}`);
    }
    logger.debug({ count: data.count }, '[MRT] Organizations listed');
    return {
        count: data.count ?? 0,
        next: data.next ?? null,
        previous: data.previous ?? null,
        organizations: data.results ?? [],
    };
}
//# sourceMappingURL=organization.js.map