import { z } from 'zod';
import { globalOptionsZod } from '../../../../Command.js';
import GraphCommand from '../../../base/GraphCommand.js';
import commands from '../../commands.js';
import request from '../../../../request.js';
import { spe } from '../../../../utils/spe.js';
export const options = z.strictObject({
    ...globalOptionsZod.shape,
    id: z.string().alias('i').optional(),
    name: z.string().alias('n').optional(),
    containerTypeId: z.uuid().optional(),
    containerTypeName: z.string().optional()
});
class SpeContainerGetCommand extends GraphCommand {
    get name() {
        return commands.CONTAINER_GET;
    }
    get description() {
        return 'Gets a container of a specific container type';
    }
    get schema() {
        return options;
    }
    getRefinedSchema(schema) {
        return schema
            .refine((opts) => [opts.id, opts.name].filter(value => value !== undefined).length === 1, {
            message: 'Specify either id or name, but not both.'
        })
            .refine((options) => !options.name || [options.containerTypeId, options.containerTypeName].filter(o => o !== undefined).length === 1, {
            error: 'Use one of the following options when specifying the container name: containerTypeId or containerTypeName.'
        })
            .refine((options) => options.name || [options.containerTypeId, options.containerTypeName].filter(o => o !== undefined).length === 0, {
            error: 'Options containerTypeId and containerTypeName are only required when retrieving a container by name.'
        });
    }
    async commandAction(logger, args) {
        try {
            const containerId = await this.resolveContainerId(args.options, logger);
            if (this.verbose) {
                await logger.logToStderr(`Getting a container with id '${containerId}'...`);
            }
            const requestOptions = {
                url: `${this.resource}/v1.0/storage/fileStorage/containers/${containerId}`,
                headers: {
                    accept: 'application/json;odata.metadata=none'
                },
                responseType: 'json'
            };
            const res = await request.get(requestOptions);
            await logger.log(res);
        }
        catch (err) {
            this.handleRejectedODataJsonPromise(err);
        }
    }
    async resolveContainerId(options, logger) {
        if (options.id) {
            return options.id;
        }
        if (this.verbose) {
            await logger.logToStderr(`Resolving container id from name '${options.name}'...`);
        }
        const containerTypeId = await this.getContainerTypeId(options, logger);
        return spe.getContainerIdByName(containerTypeId, options.name);
    }
    async getContainerTypeId(options, logger) {
        if (options.containerTypeId) {
            return options.containerTypeId;
        }
        if (this.verbose) {
            await logger.logToStderr(`Getting container type with name '${options.containerTypeName}'...`);
        }
        return spe.getContainerTypeIdByName(options.containerTypeName);
    }
}
export default new SpeContainerGetCommand();
//# sourceMappingURL=container-get.js.map