import { z } from 'zod';
import { globalOptionsZod } from '../../../../Command.js';
import { formatting } from '../../../../utils/formatting.js';
import { odata } from '../../../../utils/odata.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,
    userName: z.string().refine(name => validation.isValidUserPrincipalName(name), {
        error: e => `'${e.input}' is not a valid userName.`
    }).optional().alias('n'),
    userId: z.uuid().optional(),
    appDisplayName: z.string().optional(),
    appId: z.uuid().optional()
});
class EntraUserSigninListCommand extends GraphCommand {
    get name() {
        return commands.USER_SIGNIN_LIST;
    }
    get description() {
        return 'Retrieves the Entra ID user sign-ins for the tenant';
    }
    get schema() {
        return options;
    }
    getRefinedSchema(schema) {
        return schema
            .refine(options => !(options.userId && options.userName), {
            error: `Specify either 'userId' or 'userName', but not both.`
        })
            .refine(options => !(options.appId && options.appDisplayName), {
            error: `Specify either 'appId' or 'appDisplayName', but not both.`
        });
    }
    defaultProperties() {
        return ['id', 'userPrincipalName', 'appId', 'appDisplayName', 'createdDateTime'];
    }
    async commandAction(logger, args) {
        try {
            let endpoint = `${this.resource}/v1.0/auditLogs/signIns`;
            let filter = "";
            if (args.options.userName || args.options.userId) {
                filter = args.options.userId ?
                    `?$filter=userId eq '${formatting.encodeQueryParameter(args.options.userId)}'` :
                    `?$filter=userPrincipalName eq '${formatting.encodeQueryParameter(args.options.userName)}'`;
            }
            if (args.options.appId || args.options.appDisplayName) {
                filter += filter ? " and " : "?$filter=";
                filter += args.options.appId ?
                    `appId eq '${formatting.encodeQueryParameter(args.options.appId)}'` :
                    `appDisplayName eq '${formatting.encodeQueryParameter(args.options.appDisplayName)}'`;
            }
            endpoint += filter;
            const signins = await odata.getAllItems(endpoint);
            await logger.log(signins);
        }
        catch (err) {
            this.handleRejectedODataJsonPromise(err);
        }
    }
}
export default new EntraUserSigninListCommand();
//# sourceMappingURL=user-signin-list.js.map