import { z } from 'zod';
import { globalOptionsZod } from '../../../../Command.js';
import request from '../../../../request.js';
import { entraUser } from '../../../../utils/entraUser.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,
    environmentName: z.string().alias('e'),
    appName: z.string()
        .refine(val => validation.isValidGuid(val), {
        message: 'The value is not a valid GUID for appName.'
    }),
    userId: z.string()
        .refine(val => validation.isValidGuid(val), {
        message: 'The value is not a valid GUID for userId.'
    })
        .optional(),
    userName: z.string()
        .refine(val => validation.isValidUserPrincipalName(val), {
        message: 'The value is not a valid UPN for userName.'
    })
        .optional(),
    roleForOldAppOwner: z.enum(['CanView', 'CanEdit']).optional()
});
class PaAppOwnerSetCommand extends PowerAppsCommand {
    get name() {
        return commands.APP_OWNER_SET;
    }
    get description() {
        return 'Sets a new owner for a Power Apps app';
    }
    get schema() {
        return options;
    }
    getRefinedSchema(schema) {
        return schema
            .refine(opts => [opts.userId, opts.userName].filter(x => x !== undefined).length === 1, {
            error: `Specify either 'userId' or 'userName', but not both.`,
            params: {
                customCode: 'optionSet',
                options: ['userId', 'userName']
            }
        });
    }
    async commandAction(logger, args) {
        if (this.verbose) {
            await logger.logToStderr(`Setting new owner ${args.options.userId || args.options.userName} for Power Apps app ${args.options.appName}...`);
        }
        try {
            const userId = await this.getUserId(args.options);
            const requestOptions = {
                url: `${this.resource}/providers/Microsoft.PowerApps/scopes/admin/environments/${args.options.environmentName}/apps/${args.options.appName}/modifyAppOwner?api-version=2022-11-01`,
                headers: {
                    accept: 'application/json',
                    'Content-Type': 'application/json'
                },
                responseType: 'json',
                data: {
                    roleForOldAppOwner: args.options.roleForOldAppOwner,
                    newAppOwner: userId
                }
            };
            await request.post(requestOptions);
        }
        catch (err) {
            this.handleRejectedODataJsonPromise(err);
        }
    }
    async getUserId(options) {
        if (options.userId) {
            return options.userId;
        }
        return entraUser.getUserIdByUpn(options.userName);
    }
}
export default new PaAppOwnerSetCommand();
//# sourceMappingURL=app-owner-set.js.map