import { z } from 'zod';
import { cli } from '../../../../cli/cli.js';
import { globalOptionsZod } from '../../../../Command.js';
import request from '../../../../request.js';
import { odata } from '../../../../utils/odata.js';
import { formatting } from '../../../../utils/formatting.js';
import GraphCommand from '../../../base/GraphCommand.js';
import commands from '../../commands.js';
export const options = z.strictObject({
    ...globalOptionsZod.shape,
    id: z.uuid().optional().alias('i'),
    displayName: z.string().optional().alias('n'),
    objectId: z.uuid().optional(),
    force: z.boolean().optional().alias('f')
});
class EntraEnterpriseAppRemoveCommand extends GraphCommand {
    get name() {
        return commands.ENTERPRISEAPP_REMOVE;
    }
    get description() {
        return 'Deletes an enterprise application (or service principal)';
    }
    alias() {
        return [commands.SP_REMOVE];
    }
    get schema() {
        return options;
    }
    getRefinedSchema(schema) {
        return schema
            .refine(options => [options.id, options.displayName, options.objectId].filter(o => o !== undefined).length === 1, {
            error: 'Specify either id, displayName, or objectId',
            params: {
                customCode: 'optionSet',
                options: ['id', 'displayName', 'objectId']
            }
        });
    }
    async commandAction(logger, args) {
        const removeEnterpriseApplication = async () => {
            if (this.verbose) {
                await logger.logToStderr(`Removing enterprise application ${args.options.id || args.options.displayName || args.options.objectId}...`);
            }
            try {
                let url = `${this.resource}/v1.0`;
                if (args.options.id) {
                    url += `/servicePrincipals(appId='${args.options.id}')`;
                }
                else {
                    const id = await this.getSpId(args.options);
                    url += `/servicePrincipals/${id}`;
                }
                const requestOptions = {
                    url: url,
                    headers: {
                        accept: 'application/json;odata.metadata=none'
                    },
                    responseType: 'json'
                };
                await request.delete(requestOptions);
            }
            catch (err) {
                this.handleRejectedODataJsonPromise(err);
            }
        };
        if (args.options.force) {
            await removeEnterpriseApplication();
        }
        else {
            const result = await cli.promptForConfirmation({ message: `Are you sure you want to remove enterprise application '${args.options.id || args.options.displayName || args.options.objectId}'?` });
            if (result) {
                await removeEnterpriseApplication();
            }
        }
    }
    async getSpId(options) {
        if (options.objectId) {
            return options.objectId;
        }
        const spItemsResponse = await odata.getAllItems(`${this.resource}/v1.0/servicePrincipals?$filter=displayName eq '${formatting.encodeQueryParameter(options.displayName)}'&$select=id`);
        if (spItemsResponse.length === 0) {
            throw `The specified enterprise application does not exist.`;
        }
        if (spItemsResponse.length > 1) {
            const resultAsKeyValuePair = formatting.convertArrayToHashTable('id', spItemsResponse);
            const result = await cli.handleMultipleResultsFound(`Multiple enterprise applications with name '${options.displayName}' found.`, resultAsKeyValuePair);
            return result.id;
        }
        const spItem = spItemsResponse[0];
        return spItem.id;
    }
}
export default new EntraEnterpriseAppRemoveCommand();
//# sourceMappingURL=enterpriseapp-remove.js.map