import { z } from 'zod';
import { globalOptionsZod } from '../../../../Command.js';
import GraphCommand from '../../../base/GraphCommand.js';
import commands from '../../commands.js';
import { odata } from '../../../../utils/odata.js';
import { entraUser } from '../../../../utils/entraUser.js';
import { validation } from '../../../../utils/validation.js';
import { formatting } from '../../../../utils/formatting.js';
const authenticationMethodValues = ['push', 'oath', 'voiceMobile', 'voiceAlternateMobile', 'voiceOffice', 'sms', 'none'];
const methodsRegisteredValues = ['mobilePhone', 'email', 'fido2', 'microsoftAuthenticatorPush', 'softwareOneTimePasscode'];
export const options = z.strictObject({
    ...globalOptionsZod.shape,
    isAdmin: z.boolean().optional(),
    userType: z.enum(['member', 'guest']).optional(),
    userPreferredMethodForSecondaryAuthentication: z.string().refine(val => {
        const methods = val.split(',').map(m => m.trim());
        return methods.every(m => authenticationMethodValues.includes(m));
    }, {
        error: e => `'${e.input}' is not a valid userPreferredMethodForSecondaryAuthentication value. Allowed values ${authenticationMethodValues.join(', ')}.`
    }).optional(),
    systemPreferredAuthenticationMethods: z.string().refine(val => {
        const methods = val.split(',').map(m => m.trim());
        return methods.every(m => authenticationMethodValues.includes(m));
    }, {
        error: e => `'${e.input}' is not a valid systemPreferredAuthenticationMethods value. Allowed values ${authenticationMethodValues.join(', ')}.`
    }).optional(),
    isSelfServicePasswordResetRegistered: z.boolean().optional(),
    isSelfServicePasswordResetEnabled: z.boolean().optional(),
    isSelfServicePasswordResetCapable: z.boolean().optional(),
    isMfaRegistered: z.boolean().optional(),
    isMfaCapable: z.boolean().optional(),
    isPasswordlessCapable: z.boolean().optional(),
    isSystemPreferredAuthenticationMethodEnabled: z.boolean().optional(),
    methodsRegistered: z.string().refine(val => {
        const methods = val.split(',').map(m => m.trim());
        return methods.every(m => methodsRegisteredValues.includes(m));
    }, {
        error: e => `'${e.input}' is not a valid methodsRegistered value. Allowed values ${methodsRegisteredValues.join(', ')}.`
    }).optional(),
    userIds: z.string().refine(val => validation.isValidGuidArray(val) === true, {
        error: e => `The following GUIDs are invalid for the option 'userIds': ${validation.isValidGuidArray(e.input)}.`
    }).optional(),
    userPrincipalNames: z.string().refine(val => validation.isValidUserPrincipalNameArray(val) === true, {
        error: e => `The following user principal names are invalid for the option 'userPrincipalNames': ${validation.isValidUserPrincipalNameArray(e.input)}.`
    }).optional(),
    properties: z.string().optional().alias('p')
});
class EntraUserRegistrationDetailsListCommand extends GraphCommand {
    get name() {
        return commands.USER_REGISTRATIONDETAILS_LIST;
    }
    get description() {
        return 'Retrieves a list of the authentication methods registered for users';
    }
    defaultProperties() {
        return ['userPrincipalName', 'methodsRegistered', 'lastUpdatedDateTime'];
    }
    get schema() {
        return options;
    }
    async commandAction(logger, args) {
        try {
            let userUpns = [];
            if (args.options.userIds) {
                const ids = args.options.userIds.split(',').map(m => m.trim());
                userUpns = await Promise.all(ids.map(id => entraUser.getUpnByUserId(id)));
            }
            const requestUrl = this.getRequestUrl(args.options, userUpns);
            const result = await odata.getAllItems(requestUrl);
            await logger.log(result);
        }
        catch (err) {
            this.handleRejectedODataJsonPromise(err);
        }
    }
    getRequestUrl(options, userUpns) {
        const queryParameters = [];
        if (options.properties) {
            queryParameters.push(`$select=${options.properties}`);
        }
        const filters = [];
        if (options.isAdmin !== undefined) {
            filters.push(`isAdmin eq ${options.isAdmin}`);
        }
        if (options.isMfaCapable !== undefined) {
            filters.push(`isMfaCapable eq ${options.isMfaCapable}`);
        }
        if (options.isMfaRegistered !== undefined) {
            filters.push(`isMfaRegistered eq ${options.isMfaRegistered}`);
        }
        if (options.isPasswordlessCapable !== undefined) {
            filters.push(`isPasswordlessCapable eq ${options.isPasswordlessCapable}`);
        }
        if (options.isSelfServicePasswordResetCapable !== undefined) {
            filters.push(`isSelfServicePasswordResetCapable eq ${options.isSelfServicePasswordResetCapable}`);
        }
        if (options.isSelfServicePasswordResetEnabled !== undefined) {
            filters.push(`isSelfServicePasswordResetEnabled eq ${options.isSelfServicePasswordResetEnabled}`);
        }
        if (options.isSelfServicePasswordResetRegistered !== undefined) {
            filters.push(`isSelfServicePasswordResetRegistered eq ${options.isSelfServicePasswordResetRegistered}`);
        }
        if (options.isSystemPreferredAuthenticationMethodEnabled !== undefined) {
            filters.push(`isSystemPreferredAuthenticationMethodEnabled eq ${options.isSystemPreferredAuthenticationMethodEnabled}`);
        }
        const methodsRegistered = options.methodsRegistered?.split(',').map(method => `methodsRegistered/any(m:m eq '${method.trim()}')`);
        const methodsRegisteredFilter = methodsRegistered?.join(' or ');
        if (methodsRegisteredFilter) {
            filters.push(`(${methodsRegisteredFilter})`);
        }
        const systemPreferredAuthenticationMethods = options.systemPreferredAuthenticationMethods?.split(',').map(method => `systemPreferredAuthenticationMethods/any(m:m eq '${method.trim()}')`);
        const systemPreferredAuthenticationMethodsFilter = systemPreferredAuthenticationMethods?.join(' or ');
        if (systemPreferredAuthenticationMethodsFilter) {
            filters.push(`(${systemPreferredAuthenticationMethodsFilter})`);
        }
        const userUPNs = [];
        if (userUpns.length > 0) {
            userUpns.forEach(upn => {
                userUPNs.push(`userPrincipalName eq '${formatting.encodeQueryParameter(upn)}'`);
            });
        }
        if (options.userPrincipalNames) {
            const upns = options.userPrincipalNames.split(',').map(m => m.trim());
            upns.forEach(upn => {
                userUPNs.push(`userPrincipalName eq '${formatting.encodeQueryParameter(upn)}'`);
            });
        }
        if (userUPNs.length > 0) {
            filters.push(`(${userUPNs.join(' or ')})`);
        }
        const userPreferredMethodForSecondaryAuthentication = options.userPreferredMethodForSecondaryAuthentication?.split(',').map(method => `userPreferredMethodForSecondaryAuthentication eq '${method.trim()}'`);
        const userPreferredMethodForSecondaryAuthenticationFilter = userPreferredMethodForSecondaryAuthentication?.join(' or ');
        if (userPreferredMethodForSecondaryAuthenticationFilter) {
            filters.push(`(${userPreferredMethodForSecondaryAuthenticationFilter})`);
        }
        if (options.userType) {
            filters.push(`userType eq '${options.userType}'`);
        }
        if (filters.length > 0) {
            queryParameters.push(`$filter=${filters.join(' and ')}`);
        }
        const queryString = queryParameters.length > 0
            ? `?${queryParameters.join('&')}`
            : '';
        return `${this.resource}/v1.0/reports/authenticationMethods/userRegistrationDetails${queryString}`;
    }
}
export default new EntraUserRegistrationDetailsListCommand();
//# sourceMappingURL=user-registrationdetails-list.js.map