import commands from '../../commands.js';
import SpoCommand from '../../../base/SpoCommand.js';
import { globalOptionsZod } from '../../../../Command.js';
import { z } from 'zod';
import { validation } from '../../../../utils/validation.js';
import { cli } from '../../../../cli/cli.js';
import { urlUtil } from '../../../../utils/urlUtil.js';
import request from '../../../../request.js';
import { formatting } from '../../../../utils/formatting.js';
export const options = z.strictObject({
    ...globalOptionsZod.shape,
    webUrl: z.string()
        .refine(url => validation.isValidSharePointUrl(url) === true, {
        error: e => `'${e.input}' is not a valid SharePoint Online site URL.`
    })
        .alias('u'),
    url: z.string().optional(),
    id: z.uuid().optional().alias('i'),
    force: z.boolean().optional().alias('f')
});
class SpoFolderArchiveCommand extends SpoCommand {
    get name() {
        return commands.FOLDER_ARCHIVE;
    }
    get description() {
        return 'Archives a folder';
    }
    get schema() {
        return options;
    }
    getRefinedSchema(schema) {
        return schema
            .refine(options => [options.url, options.id].filter(o => o !== undefined).length === 1, {
            error: `Specify 'url' or 'id', but not both.`
        });
    }
    getExcludedOptionsWithUrls() {
        return ['url'];
    }
    async commandAction(logger, args) {
        const { webUrl, url, id, force } = args.options;
        if (!force) {
            const result = await cli.promptForConfirmation({ message: `Are you sure you would like to archive this item? You will be able to reactivate it instantly for the first 7 days. After that, it will take up to 24 hours to reactivate.` });
            if (!result) {
                return;
            }
        }
        try {
            if (this.verbose) {
                await logger.logToStderr(`Archiving folder ${url || id} at site ${webUrl}...`);
            }
            let requestUrl = `${webUrl}/_api/web`;
            if (id) {
                requestUrl += `/GetFolderById('${formatting.encodeQueryParameter(id)}')`;
            }
            else if (url) {
                const serverRelativePath = urlUtil.getServerRelativePath(webUrl, url);
                requestUrl += `/GetFolderByServerRelativePath(DecodedUrl='${formatting.encodeQueryParameter(serverRelativePath)}')`;
            }
            requestUrl += '?$select=Exists,ListItemAllFields/Id,ListItemAllFields/ParentList/Id&$expand=ListItemAllFields,ListItemAllFields/ParentList';
            const folderInfo = await request.get({
                url: requestUrl,
                headers: {
                    accept: 'application/json;odata=nometadata'
                },
                responseType: 'json'
            });
            if (!folderInfo.Exists) {
                throw `The folder '${url || id}' does not exist.`;
            }
            if (!folderInfo.ListItemAllFields?.ParentList) {
                throw `The folder '${url || id}' is the root folder of a document library and cannot be archived. Archive a subfolder instead.`;
            }
            const requestOptions = {
                url: `${webUrl}/_api/Lists(guid'${folderInfo.ListItemAllFields.ParentList.Id}')/items(${folderInfo.ListItemAllFields.Id})/Archive`,
                headers: {
                    accept: 'application/json;odata=nometadata'
                },
                responseType: 'json'
            };
            await request.post(requestOptions);
        }
        catch (err) {
            this.handleRejectedODataJsonPromise(err);
        }
    }
}
export default new SpoFolderArchiveCommand();
//# sourceMappingURL=folder-archive.js.map