import { z } from 'zod';
import { globalOptionsZod } from '../../../../Command.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'),
    name: z.string().optional().alias('n'),
    planId: z.string().optional(),
    planTitle: z.string().optional(),
    rosterId: z.string().optional(),
    ownerGroupId: z.string()
        .refine(val => validation.isValidGuid(val), {
        message: 'The value is not a valid GUID.'
    })
        .optional(),
    ownerGroupName: z.string().optional()
});
class PlannerBucketGetCommand extends GraphCommand {
    get name() {
        return commands.BUCKET_GET;
    }
    get description() {
        return 'Gets the Microsoft Planner bucket in a plan';
    }
    get schema() {
        return options;
    }
    getRefinedSchema(schema) {
        return schema
            .refine(opts => [opts.id, opts.name].filter(x => x !== undefined).length === 1, {
            message: `Specify exactly one of the following options: 'id' or 'name'.`,
            params: {
                customCode: 'optionSet',
                options: ['id', 'name']
            }
        })
            .refine(opts => !opts.name || [opts.planId, opts.planTitle, opts.rosterId].filter(x => x !== undefined).length === 1, {
            message: `Specify exactly one of the following options: 'planId', 'planTitle' or 'rosterId'.`,
            params: {
                customCode: 'optionSet',
                options: ['planId', 'planTitle', 'rosterId']
            }
        })
            .refine(opts => !opts.planTitle || [opts.ownerGroupId, opts.ownerGroupName].filter(x => x !== undefined).length === 1, {
            message: `Specify exactly one of the following options: 'ownerGroupId' or 'ownerGroupName'.`,
            params: {
                customCode: 'optionSet',
                options: ['ownerGroupId', 'ownerGroupName']
            }
        })
            .refine(opts => !opts.id || (!opts.planId && !opts.planTitle && !opts.rosterId && !opts.ownerGroupId && !opts.ownerGroupName), {
            message: `Don't specify planId, planTitle, rosterId, ownerGroupId or ownerGroupName when using id`
        })
            .refine(opts => !opts.name || !opts.planId || (!opts.ownerGroupId && !opts.ownerGroupName), {
            message: `Don't specify ownerGroupId or ownerGroupName when using planId`
        })
            .refine(opts => !opts.name || !opts.rosterId || (!opts.ownerGroupId && !opts.ownerGroupName), {
            message: `Don't specify ownerGroupId or ownerGroupName when using rosterId`
        });
    }
    async commandAction(logger, args) {
        try {
            const bucket = await this.getBucket(args);
            await logger.log(bucket);
        }
        catch (err) {
            this.handleRejectedODataJsonPromise(err);
        }
    }
    async getBucket(args) {
        const { id, name } = args.options;
        if (id) {
            return this.getBucketById(id);
        }
        const planId = await this.getPlanId(args);
        return planner.getBucketByTitle(name, planId);
    }
    async getPlanId(args) {
        const { planId, planTitle, rosterId } = args.options;
        if (planId) {
            return planId;
        }
        if (planTitle) {
            const groupId = await this.getGroupId(args);
            return planner.getPlanIdByTitle(planTitle, groupId);
        }
        return planner.getPlanIdByRosterId(rosterId);
    }
    async getBucketById(id) {
        const requestOptions = {
            url: `${this.resource}/v1.0/planner/buckets/${id}`,
            headers: {
                accept: 'application/json;odata.metadata=none'
            },
            responseType: 'json'
        };
        return request.get(requestOptions);
    }
    async getGroupId(args) {
        const { ownerGroupId, ownerGroupName } = args.options;
        if (ownerGroupId) {
            return ownerGroupId;
        }
        return entraGroup.getGroupIdByDisplayName(ownerGroupName);
    }
}
export default new PlannerBucketGetCommand();
//# sourceMappingURL=bucket-get.js.map