import { z } from 'zod';
import { cli } from '../../../../cli/cli.js';
import { globalOptionsZod } from '../../../../Command.js';
import request from '../../../../request.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()
});
class EntraEnterpriseAppGetCommand extends GraphCommand {
    get name() {
        return commands.ENTERPRISEAPP_GET;
    }
    get description() {
        return 'Gets information about an Enterprise Application';
    }
    alias() {
        return [commands.SP_GET];
    }
    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 getSpId(args) {
        if (args.options.objectId) {
            return args.options.objectId;
        }
        let spMatchQuery = '';
        if (args.options.displayName) {
            spMatchQuery = `displayName eq '${formatting.encodeQueryParameter(args.options.displayName)}'`;
        }
        else if (args.options.id) {
            spMatchQuery = `appId eq '${formatting.encodeQueryParameter(args.options.id)}'`;
        }
        const idRequestOptions = {
            url: `${this.resource}/v1.0/servicePrincipals?$filter=${spMatchQuery}`,
            headers: {
                accept: 'application/json;odata.metadata=none'
            },
            responseType: 'json'
        };
        const response = await request.get(idRequestOptions);
        const spItem = response.value[0];
        if (!spItem) {
            throw `The specified Entra app does not exist`;
        }
        if (response.value.length > 1) {
            const resultAsKeyValuePair = formatting.convertArrayToHashTable('id', response.value);
            const result = await cli.handleMultipleResultsFound(`Multiple Entra apps with name '${args.options.displayName}' found.`, resultAsKeyValuePair);
            return result.id;
        }
        return spItem.id;
    }
    async commandAction(logger, args) {
        if (this.verbose) {
            await logger.logToStderr(`Retrieving enterprise application information...`);
        }
        try {
            const id = await this.getSpId(args);
            const requestOptions = {
                url: `${this.resource}/v1.0/servicePrincipals/${id}`,
                headers: {
                    accept: 'application/json;odata.metadata=none',
                    'content-type': '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 EntraEnterpriseAppGetCommand();
//# sourceMappingURL=enterpriseapp-get.js.map