import { z } from 'zod';
import { globalOptionsZod } from '../../../../Command.js';
import request from '../../../../request.js';
import commands from '../../commands.js';
import GraphCommand from '../../../base/GraphCommand.js';
import { entraApp } from '../../../../utils/entraApp.js';
import { cli } from '../../../../cli/cli.js';
import { directoryExtension } from '../../../../utils/directoryExtension.js';
export const options = z.strictObject({
    ...globalOptionsZod.shape,
    id: z.uuid().optional().alias('i'),
    name: z.string().optional().alias('n'),
    appId: z.uuid().optional(),
    appObjectId: z.uuid().optional(),
    appName: z.string().optional(),
    force: z.boolean().optional().alias('f')
});
class GraphDirectoryExtensionRemoveCommand extends GraphCommand {
    get name() {
        return commands.DIRECTORYEXTENSION_REMOVE;
    }
    get description() {
        return 'Removes a directory extension';
    }
    get schema() {
        return options;
    }
    getRefinedSchema(schema) {
        return schema
            .refine(options => !options.id !== !options.name, {
            error: 'Specify either id or name, but not both',
            params: {
                customCode: 'optionSet',
                options: ['id', 'name']
            }
        })
            .refine(options => options.id || options.name, {
            error: 'Specify either id or name',
            params: {
                customCode: 'optionSet',
                options: ['id', 'name']
            }
        })
            .refine(options => Object.values([options.appId, options.appObjectId, options.appName]).filter(v => typeof v !== 'undefined').length === 1, {
            error: 'Specify either appId, appObjectId or appName, but not multiple',
            params: {
                customCode: 'optionSet',
                options: ['appId', 'appObjectId', 'appName']
            }
        });
    }
    async commandAction(logger, args) {
        const removeDirectoryExtension = async () => {
            try {
                const appObjectId = await this.getAppObjectId(args.options);
                let schemeExtensionId = args.options.id;
                if (args.options.name) {
                    schemeExtensionId = (await directoryExtension.getDirectoryExtensionByName(args.options.name, appObjectId, ['id'])).id;
                }
                if (args.options.verbose) {
                    await logger.logToStderr(`Removing schema extension with ID ${schemeExtensionId} from application with ID ${appObjectId}...`);
                }
                const requestOptions = {
                    url: `${this.resource}/v1.0/applications/${appObjectId}/extensionProperties/${schemeExtensionId}`,
                    headers: {
                        accept: 'application/json;odata.metadata=none'
                    }
                };
                await request.delete(requestOptions);
            }
            catch (err) {
                this.handleRejectedODataJsonPromise(err);
            }
        };
        if (args.options.force) {
            await removeDirectoryExtension();
        }
        else {
            const result = await cli.promptForConfirmation({ message: `Are you sure you want to remove directory extension '${args.options.id || args.options.name}' from application '${args.options.appObjectId || args.options.appId || args.options.appName}'?` });
            if (result) {
                await removeDirectoryExtension();
            }
        }
    }
    async getAppObjectId(options) {
        if (options.appObjectId) {
            return options.appObjectId;
        }
        if (options.appId) {
            return (await entraApp.getAppRegistrationByAppId(options.appId, ["id"])).id;
        }
        return (await entraApp.getAppRegistrationByAppName(options.appName, ["id"])).id;
    }
}
export default new GraphDirectoryExtensionRemoveCommand();
//# sourceMappingURL=directoryextension-remove.js.map