import { z } from 'zod';
import { globalOptionsZod } from '../../../../Command.js';
import { cli } from '../../../../cli/cli.js';
import request from '../../../../request.js';
import { entraGroup } from '../../../../utils/entraGroup.js';
import { planner } from '../../../../utils/planner.js';
import { validation } from '../../../../utils/validation.js';
import GraphCommand from '../../../base/GraphCommand.js';
import commands from '../../commands.js';
export const options = z.strictObject({
    ...globalOptionsZod.shape,
    id: z.string().optional().alias('i'),
    title: z.string().optional().alias('t'),
    ownerGroupId: z.string()
        .refine(val => validation.isValidGuid(val), {
        message: 'The value is not a valid GUID.'
    })
        .optional(),
    ownerGroupName: z.string().optional(),
    force: z.boolean().optional().alias('f')
});
class PlannerPlanRemoveCommand extends GraphCommand {
    get name() {
        return commands.PLAN_REMOVE;
    }
    get description() {
        return 'Removes the Microsoft Planner plan';
    }
    get schema() {
        return options;
    }
    getRefinedSchema(schema) {
        return schema
            .refine(opts => [opts.id, opts.title].filter(x => x !== undefined).length === 1, {
            message: `Specify exactly one of the following options: 'id' or 'title'.`,
            params: {
                customCode: 'optionSet',
                options: ['id', 'title']
            }
        })
            .refine(opts => !opts.title || [opts.ownerGroupId, opts.ownerGroupName].filter(x => x !== undefined).length === 1, {
            message: `Specify either ownerGroupId or ownerGroupName when using title.`,
            params: {
                customCode: 'optionSet',
                options: ['ownerGroupId', 'ownerGroupName']
            }
        })
            .refine(opts => !opts.id || (!opts.ownerGroupId && !opts.ownerGroupName), {
            message: `Don't specify ownerGroupId or ownerGroupName when using id`
        });
    }
    async commandAction(logger, args) {
        const removePlan = async () => {
            try {
                const plan = await this.getPlan(args);
                if (this.verbose) {
                    await logger.logToStderr(`Removing plan '${plan.title}' ...`);
                }
                const requestOptions = {
                    url: `${this.resource}/v1.0/planner/plans/${plan.id}`,
                    headers: {
                        accept: 'application/json;odata.metadata=none',
                        'if-match': plan['@odata.etag']
                    },
                    responseType: 'json'
                };
                await request.delete(requestOptions);
            }
            catch (err) {
                this.handleRejectedODataJsonPromise(err);
            }
        };
        if (args.options.force) {
            await removePlan();
        }
        else {
            const result = await cli.promptForConfirmation({ message: `Are you sure you want to remove the plan ${args.options.id || args.options.title}?` });
            if (result) {
                await removePlan();
            }
        }
    }
    async getPlan(args) {
        const { id, title } = args.options;
        if (id) {
            return planner.getPlanById(id, 'minimal');
        }
        const groupId = await this.getGroupId(args);
        return planner.getPlanByTitle(title, groupId, 'minimal');
    }
    async getGroupId(args) {
        const { ownerGroupId, ownerGroupName } = args.options;
        if (ownerGroupId) {
            return ownerGroupId;
        }
        return entraGroup.getGroupIdByDisplayName(ownerGroupName);
    }
}
export default new PlannerPlanRemoveCommand();
//# sourceMappingURL=plan-remove.js.map