import { globalOptionsZod } from '../../../../Command.js';
import request from '../../../../request.js';
import { formatting } from '../../../../utils/formatting.js';
import commands from '../../commands.js';
import { Outlook } from '../../Outlook.js';
import { cli } from '../../../../cli/cli.js';
import DelegatedGraphCommand from '../../../base/GraphDelegatedCommand.js';
import { z } from 'zod';
export const options = z.strictObject({
    ...globalOptionsZod.shape,
    id: z.string(),
    sourceFolderId: z.string().optional(),
    sourceFolderName: z.string().optional(),
    targetFolderId: z.string().optional(),
    targetFolderName: z.string().optional()
});
class OutlookMessageMoveCommand extends DelegatedGraphCommand {
    get name() {
        return commands.MESSAGE_MOVE;
    }
    get description() {
        return 'Moves message to the specified folder';
    }
    get schema() {
        return options;
    }
    getRefinedSchema(schema) {
        return schema
            .refine(options => options.sourceFolderId || options.sourceFolderName, {
            error: 'Specify either sourceFolderId or sourceFolderName',
            params: {
                customCode: 'optionSet',
                options: ['sourceFolderId', 'sourceFolderName']
            }
        })
            .refine(options => !(options.sourceFolderId && options.sourceFolderName), {
            error: 'Specify either sourceFolderId or sourceFolderName, but not both',
            params: {
                customCode: 'optionSet',
                options: ['sourceFolderId', 'sourceFolderName']
            }
        })
            .refine(options => options.targetFolderId || options.targetFolderName, {
            error: 'Specify either targetFolderId or targetFolderName',
            params: {
                customCode: 'optionSet',
                options: ['targetFolderId', 'targetFolderName']
            }
        })
            .refine(options => !(options.targetFolderId && options.targetFolderName), {
            error: 'Specify either targetFolderId or targetFolderName, but not both',
            params: {
                customCode: 'optionSet',
                options: ['targetFolderId', 'targetFolderName']
            }
        });
    }
    async commandAction(logger, args) {
        let sourceFolder;
        let targetFolder;
        try {
            sourceFolder = await this.getFolderId(args.options.sourceFolderId, args.options.sourceFolderName);
            targetFolder = await this.getFolderId(args.options.targetFolderId, args.options.targetFolderName);
            const messageUrl = `mailFolders/${sourceFolder}/messages/${args.options.id}`;
            const requestOptions = {
                url: `${this.resource}/v1.0/me/${messageUrl}/move`,
                headers: {
                    accept: 'application/json;odata.metadata=none'
                },
                data: {
                    destinationId: targetFolder
                },
                responseType: 'json'
            };
            await request.post(requestOptions);
        }
        catch (err) {
            this.handleRejectedODataJsonPromise(err);
        }
    }
    async getFolderId(folderId, folderName) {
        if (folderId) {
            return folderId;
        }
        if (Outlook.wellKnownFolderNames.indexOf(folderName) > -1) {
            return folderName;
        }
        const requestOptions = {
            url: `${this.resource}/v1.0/me/mailFolders?$filter=displayName eq '${formatting.encodeQueryParameter(folderName)}'&$select=id`,
            headers: {
                accept: 'application/json;odata.metadata=none'
            },
            responseType: 'json'
        };
        const response = await request.get(requestOptions);
        if (response.value.length === 0) {
            throw `Folder with name '${folderName}' not found`;
        }
        if (response.value.length > 1) {
            const resultAsKeyValuePair = formatting.convertArrayToHashTable('id', response.value);
            const result = await cli.handleMultipleResultsFound(`Multiple folders with name '${folderName}' found.`, resultAsKeyValuePair);
            return result.id;
        }
        return response.value[0].id;
    }
}
export default new OutlookMessageMoveCommand();
//# sourceMappingURL=message-move.js.map