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(),
    newName: z.string().optional(),
    orderHint: z.string().optional()
});
class PlannerBucketSetCommand extends GraphCommand {
    get name() {
        return commands.BUCKET_SET;
    }
    get description() {
        return 'Updates a Microsoft Planner bucket';
    }
    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.ownerGroupId && !opts.ownerGroupName && !opts.rosterId), {
            message: `Don't specify planId, planTitle, ownerGroupId, ownerGroupName or rosterId 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`
        })
            .refine(opts => opts.newName !== undefined || opts.orderHint !== undefined, {
            message: 'Specify either newName or orderHint'
        });
    }
    async commandAction(logger, args) {
        if (this.verbose) {
            await logger.logToStderr(`Updating bucket...`);
        }
        try {
            const bucket = await this.getBucket(args);
            const requestOptions = {
                url: `${this.resource}/v1.0/planner/buckets/${bucket.id}`,
                headers: {
                    accept: 'application/json;odata.metadata=none',
                    'if-match': bucket['@odata.etag']
                },
                responseType: 'json',
                data: {
                    name: args.options.newName,
                    orderHint: args.options.orderHint
                }
            };
            await request.patch(requestOptions);
        }
        catch (err) {
            this.handleRejectedODataJsonPromise(err);
        }
    }
    async getBucket(args) {
        if (args.options.id) {
            const requestOptions = {
                url: `${this.resource}/v1.0/planner/buckets/${args.options.id}`,
                headers: {
                    accept: 'application/json;odata.metadata=minimal'
                },
                responseType: 'json'
            };
            return await request.get(requestOptions);
        }
        const planId = await this.getPlanId(args);
        return planner.getBucketByTitle(args.options.name, planId, 'minimal');
    }
    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 getGroupId(args) {
        const { ownerGroupId, ownerGroupName } = args.options;
        if (ownerGroupId) {
            return ownerGroupId;
        }
        return entraGroup.getGroupIdByDisplayName(ownerGroupName);
    }
}
export default new PlannerBucketSetCommand();
//# sourceMappingURL=bucket-set.js.map