import auth from '../../../../Auth.js';
import { globalOptionsZod } from '../../../../Command.js';
import request from '../../../../request.js';
import { accessToken } from '../../../../utils/accessToken.js';
import GraphCommand from '../../../base/GraphCommand.js';
import commands from '../../commands.js';
import { z } from 'zod';
import { validation } from '../../../../utils/validation.js';
export const options = z.strictObject({
    ...globalOptionsZod.shape,
    id: z.string().alias('i'),
    userId: z.string()
        .refine(userId => validation.isValidGuid(userId), {
        error: e => `'${e.input}' is not a valid GUID.`
    }).optional(),
    userName: z.string()
        .refine(userName => validation.isValidUserPrincipalName(userName), {
        error: e => `'${e.input}' is not a valid UPN.`
    }).optional()
});
class OutlookMessageGetCommand extends GraphCommand {
    get name() {
        return commands.MESSAGE_GET;
    }
    get description() {
        return 'Retrieves specified message';
    }
    get schema() {
        return options;
    }
    getRefinedSchema(schema) {
        return schema
            .refine(options => !(options.userId && options.userName), {
            error: 'Specify either userId or userName, but not both',
            params: {
                customCode: 'optionSet',
                options: ['userId', 'userName']
            }
        });
    }
    async commandAction(logger, args) {
        try {
            const isAppOnlyAccessToken = accessToken.isAppOnlyAccessToken(auth.connection.accessTokens[this.resource].accessToken);
            if (this.verbose) {
                await logger.logToStderr(`Retrieving message with id ${args.options.id} using ${isAppOnlyAccessToken ? 'app only permissions' : 'delegated permissions'}`);
            }
            let requestUrl = '';
            if (isAppOnlyAccessToken) {
                if (!args.options.userId && !args.options.userName) {
                    throw `The option 'userId' or 'userName' is required when retrieving an email using app only credentials`;
                }
                if (args.options.userId && args.options.userName) {
                    throw `Both options 'userId' and 'userName' cannot be set when retrieving an email using app only credentials`;
                }
                requestUrl += `users/${args.options.userId ? args.options.userId : args.options.userName}`;
            }
            else {
                if (args.options.userId && args.options.userName) {
                    throw `Both options 'userId' and 'userName' cannot be set when retrieving an email using delegated credentials`;
                }
                if (args.options.userId || args.options.userName) {
                    requestUrl += `users/${args.options.userId ? args.options.userId : args.options.userName}`;
                }
                else {
                    requestUrl += 'me';
                }
            }
            requestUrl += `/messages/${args.options.id}`;
            const requestOptions = {
                url: `${this.resource}/v1.0/${requestUrl}`,
                headers: {
                    accept: 'application/json;odata.metadata=none'
                },
                responseType: 'json'
            };
            const res = await request.get(requestOptions);
            await logger.log(res);
        }
        catch (err) {
            this.handleRejectedODataJsonPromise(err);
        }
    }
}
export default new OutlookMessageGetCommand();
//# sourceMappingURL=message-get.js.map