import { z } from 'zod';
import { CommandError, globalOptionsZod } from '../../../../Command.js';
import { cli } from '../../../../cli/cli.js';
import request from '../../../../request.js';
import { formatting } from '../../../../utils/formatting.js';
import { validation } from '../../../../utils/validation.js';
import PowerAppsCommand from '../../../base/PowerAppsCommand.js';
import commands from '../../commands.js';
export const options = z.strictObject({
    ...globalOptionsZod.shape,
    name: z.string()
        .refine(val => validation.isValidGuid(val), {
        message: 'The value is not a valid GUID.'
    })
        .alias('n'),
    force: z.boolean().optional().alias('f'),
    asAdmin: z.boolean().optional(),
    environmentName: z.string().optional().alias('e')
});
class PaAppRemoveCommand extends PowerAppsCommand {
    get name() {
        return commands.APP_REMOVE;
    }
    get description() {
        return 'Removes the specified Microsoft Power App';
    }
    get schema() {
        return options;
    }
    getRefinedSchema(schema) {
        return schema
            .refine(opts => !opts.asAdmin || opts.environmentName, {
            message: 'When specifying the asAdmin option, the environment option is required as well.'
        })
            .refine(opts => !opts.environmentName || opts.asAdmin, {
            message: 'When specifying the environment option, the asAdmin option is required as well.'
        });
    }
    async commandAction(logger, args) {
        if (this.verbose) {
            await logger.logToStderr(`Removing Microsoft Power App ${args.options.name}...`);
        }
        const removePaApp = async () => {
            let endpoint = `${this.resource}/providers/Microsoft.PowerApps`;
            if (args.options.asAdmin) {
                endpoint += `/scopes/admin/environments/${formatting.encodeQueryParameter(args.options.environmentName)}`;
            }
            endpoint += `/apps/${formatting.encodeQueryParameter(args.options.name)}?api-version=2017-08-01`;
            const requestOptions = {
                url: endpoint,
                fullResponse: true,
                headers: {
                    accept: 'application/json'
                },
                responseType: 'json'
            };
            try {
                await request.delete(requestOptions);
            }
            catch (err) {
                if (err.response && err.response.status === 403) {
                    throw new CommandError(`App '${args.options.name}' does not exist`);
                }
                else {
                    this.handleRejectedODataJsonPromise(err);
                }
            }
        };
        if (args.options.force) {
            await removePaApp();
        }
        else {
            const result = await cli.promptForConfirmation({ message: `Are you sure you want to remove the Microsoft Power App ${args.options.name}?` });
            if (result) {
                await removePaApp();
            }
        }
    }
}
export default new PaAppRemoveCommand();
//# sourceMappingURL=app-remove.js.map