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 SpoFileUnarchiveCommand extends SpoCommand {
    get name() {
        return commands.FILE_UNARCHIVE;
    }
    get description() {
        return 'Unarchives a file';
    }
    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, verbose } = args.options;
        if (!force) {
            const result = await cli.promptForConfirmation({ message: `Reactivation could take up to 24 hours. Files that are reactivated cannot be archived again for 30 days. Are you sure you would like to unarchive this item?` });
            if (!result) {
                return;
            }
        }
        try {
            if (verbose) {
                await logger.logToStderr(`Unarchiving file '${url || id}' at site '${webUrl}'...`);
            }
            let requestUrl = `${webUrl}/_api/web`;
            if (id) {
                requestUrl += `/GetFileById('${formatting.encodeQueryParameter(id)}')`;
            }
            else if (url) {
                const serverRelativePath = urlUtil.getServerRelativePath(webUrl, url);
                requestUrl += `/GetFileByServerRelativePath(DecodedUrl='${formatting.encodeQueryParameter(serverRelativePath)}')`;
            }
            requestUrl += '?$select=ListId,ListItemAllFields/Id&$expand=ListItemAllFields';
            const fileInfo = await request.get({
                url: requestUrl,
                headers: {
                    accept: 'application/json;odata=nometadata'
                },
                responseType: 'json'
            });
            const requestOptions = {
                url: `${webUrl}/_api/Lists(guid'${fileInfo.ListId}')/items(${fileInfo.ListItemAllFields.Id})/UnArchive`,
                headers: {
                    accept: 'application/json;odata=nometadata'
                },
                responseType: 'json'
            };
            await request.post(requestOptions);
        }
        catch (err) {
            this.handleRejectedODataJsonPromise(err);
        }
    }
}
export default new SpoFileUnarchiveCommand();
//# sourceMappingURL=file-unarchive.js.map