import { z } from 'zod';
import { globalOptionsZod } from '../../../../Command.js';
import GraphCommand from '../../../base/GraphCommand.js';
import commands from '../../commands.js';
import { vivaEngage } from '../../../../utils/vivaEngage.js';
import { entraUser } from '../../../../utils/entraUser.js';
import request from '../../../../request.js';
import { validation } from '../../../../utils/validation.js';
export const options = z.strictObject({
    ...globalOptionsZod.shape,
    roleId: z.uuid().optional().refine(id => validation.isValidGuid(id), {
        error: e => `'${e.input}' is not a valid GUID.`
    }),
    roleName: z.string().optional(),
    userId: z.uuid().optional().refine(id => validation.isValidGuid(id), {
        error: e => `'${e.input}' is not a valid GUID.`
    }),
    userName: z.string().refine(upn => validation.isValidUserPrincipalName(upn), {
        error: e => `'${e.input}' is not a valid UPN.`
    }).optional()
});
class VivaEngageRoleMemberAddCommand extends GraphCommand {
    get name() {
        return commands.ENGAGE_ROLE_MEMBER_ADD;
    }
    get description() {
        return 'Assigns a Viva Engage role to a user';
    }
    get schema() {
        return options;
    }
    getRefinedSchema(schema) {
        return schema
            .refine(options => [options.roleId, options.roleName].filter(x => x !== undefined).length === 1, {
            error: 'Specify either roleId, or roleName, but not both.'
        })
            .refine(options => [options.userId, options.userName].filter(x => x !== undefined).length === 1, {
            error: 'Specify either userId, or userName, but not both.'
        });
    }
    async commandAction(logger, args) {
        try {
            let roleId = args.options.roleId;
            let userId = args.options.userId;
            if (args.options.roleName) {
                if (this.verbose) {
                    await logger.logToStderr(`Retrieving Viva Engage role ID for role name '${args.options.roleName}'...`);
                }
                roleId = await vivaEngage.getRoleIdByName(args.options.roleName);
            }
            if (args.options.userName) {
                if (this.verbose) {
                    await logger.logToStderr(`Retrieving Viva Engage user ID for user name '${args.options.userName}'...`);
                }
                userId = await entraUser.getUserIdByUpn(args.options.userName);
            }
            if (this.verbose) {
                await logger.logToStderr(`Assigning user ${userId} to a Viva Engage role ${roleId}...`);
            }
            const requestOptions = {
                url: `${this.resource}/beta/employeeExperience/roles/${roleId}/members`,
                headers: {
                    'content-type': 'application/json;odata=nometadata',
                    'accept': 'application/json;odata.metadata=none'
                },
                responseType: 'json',
                data: {
                    'user@odata.bind': `${this.resource}/beta/users('${userId}')`
                }
            };
            await request.post(requestOptions);
        }
        catch (err) {
            this.handleRejectedODataJsonPromise(err);
        }
    }
}
export default new VivaEngageRoleMemberAddCommand();
//# sourceMappingURL=engage-role-member-add.js.map