import GraphCommand from '../../../base/GraphCommand.js';
import commands from '../../commands.js';
import { z } from 'zod';
import { globalOptionsZod } from '../../../../Command.js';
import { roleDefinition } from '../../../../utils/roleDefinition.js';
import { validation } from '../../../../utils/validation.js';
import request from '../../../../request.js';
import { cli } from '../../../../cli/cli.js';
export const options = z.strictObject({
    ...globalOptionsZod.shape,
    id: z.uuid().optional().alias('i'),
    displayName: z.string().optional().alias('n'),
    force: z.boolean().optional().alias('f')
});
class EntraRoleDefinitionRemoveCommand extends GraphCommand {
    get name() {
        return commands.ROLEDEFINITION_REMOVE;
    }
    get description() {
        return 'Removes a specific Microsoft Entra ID role definition';
    }
    get schema() {
        return options;
    }
    getRefinedSchema(schema) {
        return schema
            .refine(options => !options.id !== !options.displayName, {
            error: 'Specify either id or displayName, but not both',
            params: {
                customCode: 'optionSet',
                options: ['id', 'displayName']
            }
        })
            .refine(options => options.id || options.displayName, {
            error: 'Specify either id or displayName',
            params: {
                customCode: 'optionSet',
                options: ['id', 'displayName']
            }
        })
            .refine(options => (!options.id && !options.displayName) || options.displayName || (options.id && validation.isValidGuid(options.id)), {
            error: e => `The '${e.input}' must be a valid GUID`,
            path: ['id']
        });
    }
    async commandAction(logger, args) {
        const removeRoleDefinition = async () => {
            try {
                let roleDefinitionId = args.options.id;
                if (args.options.displayName) {
                    roleDefinitionId = (await roleDefinition.getRoleDefinitionByDisplayName(args.options.displayName, 'id')).id;
                }
                if (args.options.verbose) {
                    await logger.logToStderr(`Removing role definition with ID ${roleDefinitionId}...`);
                }
                const requestOptions = {
                    url: `${this.resource}/v1.0/roleManagement/directory/roleDefinitions/${roleDefinitionId}`,
                    headers: {
                        accept: 'application/json;odata.metadata=none'
                    }
                };
                await request.delete(requestOptions);
            }
            catch (err) {
                this.handleRejectedODataJsonPromise(err);
            }
        };
        if (args.options.force) {
            await removeRoleDefinition();
        }
        else {
            const result = await cli.promptForConfirmation({ message: `Are you sure you want to remove role definition '${args.options.id || args.options.displayName}'?` });
            if (result) {
                await removeRoleDefinition();
            }
        }
    }
}
export default new EntraRoleDefinitionRemoveCommand();
//# sourceMappingURL=roledefinition-remove.js.map