import { z } from 'zod';
import { globalOptionsZod } from '../../../../Command.js';
import request from '../../../../request.js';
import { entraUser } from '../../../../utils/entraUser.js';
import { validation } from '../../../../utils/validation.js';
import GraphCommand from '../../../base/GraphCommand.js';
import commands from '../../commands.js';
export const options = z.strictObject({
    ...globalOptionsZod.shape,
    userId: z.uuid().optional().alias('i'),
    userName: z.string().refine(name => validation.isValidUserPrincipalName(name), {
        error: e => `'${e.input}' is not a valid user principal name.`
    }).optional().alias('n'),
    userEmail: z.string().refine(email => validation.isValidUserPrincipalName(email), {
        error: e => `'${e.input}' is not a valid user email.`
    }).optional().alias('e'),
    securityEnabledOnly: z.boolean().optional()
});
class EntraUserGroupmembershipListCommand extends GraphCommand {
    get name() {
        return commands.USER_GROUPMEMBERSHIP_LIST;
    }
    get description() {
        return 'Retrieves all groups where the user is a member of';
    }
    get schema() {
        return options;
    }
    getRefinedSchema(schema) {
        return schema
            .refine(options => [options.userId, options.userName, options.userEmail].filter(o => o !== undefined).length === 1, {
            error: `Specify either 'userId', 'userName', or 'userEmail'.`,
            params: {
                customCode: 'optionSet',
                options: ['userId', 'userName', 'userEmail']
            }
        });
    }
    async commandAction(logger, args) {
        let userId = args.options.userId;
        try {
            if (args.options.userName) {
                userId = await entraUser.getUserIdByUpn(args.options.userName);
            }
            else if (args.options.userEmail) {
                userId = await entraUser.getUserIdByEmail(args.options.userEmail);
            }
            const requestOptions = {
                url: `${this.resource}/v1.0/users/${userId}/getMemberGroups`,
                headers: {
                    accept: 'application/json;odata.metadata=none'
                },
                responseType: 'json',
                data: {
                    securityEnabledOnly: !!args.options.securityEnabledOnly
                }
            };
            const groups = [];
            const results = await request.post(requestOptions);
            results.value.forEach(x => groups.push({ groupId: x }));
            await logger.log(groups);
        }
        catch (err) {
            this.handleRejectedODataJsonPromise(err);
        }
    }
}
export default new EntraUserGroupmembershipListCommand();
//# sourceMappingURL=user-groupmembership-list.js.map