import { z } from 'zod';
import { globalOptionsZod } from '../../../../Command.js';
import { formatting } from '../../../../utils/formatting.js';
import { odata } from '../../../../utils/odata.js';
import PowerAppsCommand from '../../../base/PowerAppsCommand.js';
import commands from '../../commands.js';
export const options = z.strictObject({
    ...globalOptionsZod.shape,
    environmentName: z.string().optional().alias('e'),
    asAdmin: z.boolean().optional()
});
class PaAppListCommand extends PowerAppsCommand {
    get name() {
        return commands.APP_LIST;
    }
    get description() {
        return 'Lists all Power Apps apps';
    }
    defaultProperties() {
        return ['name', 'displayName'];
    }
    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) {
        const url = `${this.resource}/providers/Microsoft.PowerApps${args.options.asAdmin ? '/scopes/admin' : ''}${args.options.environmentName ? '/environments/' + formatting.encodeQueryParameter(args.options.environmentName) : ''}/apps?api-version=2017-08-01`;
        try {
            const apps = await odata.getAllItems(url);
            if (apps.length > 0) {
                apps.forEach(a => {
                    a.displayName = a.properties.displayName;
                });
            }
            await logger.log(apps);
        }
        catch (err) {
            this.handleRejectedODataJsonPromise(err);
        }
    }
}
export default new PaAppListCommand();
//# sourceMappingURL=app-list.js.map