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,
    name: z.string().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(),
    orderHint: z.string().optional()
});
class PlannerBucketAddCommand extends GraphCommand {
    get name() {
        return commands.BUCKET_ADD;
    }
    get description() {
        return 'Adds a new Microsoft Planner bucket';
    }
    get schema() {
        return options;
    }
    getRefinedSchema(schema) {
        return schema
            .refine(opts => [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']
            }
        });
    }
    async commandAction(logger, args) {
        try {
            const planId = await this.getPlanId(args);
            const requestOptions = {
                url: `${this.resource}/v1.0/planner/buckets`,
                headers: {
                    accept: 'application/json;odata.metadata=none'
                },
                responseType: 'json',
                data: {
                    name: args.options.name,
                    planId: planId,
                    orderHint: args.options.orderHint
                }
            };
            const response = await request.post(requestOptions);
            await logger.log(response);
        }
        catch (err) {
            this.handleRejectedODataJsonPromise(err);
        }
    }
    async getPlanId(args) {
        if (args.options.planId) {
            return args.options.planId;
        }
        if (args.options.planTitle) {
            const groupId = await this.getGroupId(args);
            return planner.getPlanIdByTitle(args.options.planTitle, groupId);
        }
        return planner.getPlanIdByRosterId(args.options.rosterId);
    }
    async getGroupId(args) {
        if (args.options.ownerGroupId) {
            return args.options.ownerGroupId;
        }
        return entraGroup.getGroupIdByDisplayName(args.options.ownerGroupName);
    }
}
export default new PlannerBucketAddCommand();
//# sourceMappingURL=bucket-add.js.map