"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.register = void 0;
const vscode = require("vscode-languageserver-protocol");
const rename_1 = require("./rename");
function register(context) {
    return async (item, token = vscode.CancellationToken.None) => {
        const data = item.data;
        if (data?.type === 'plugin') {
            const plugin = context.plugins[data.pluginId];
            if (!plugin.resolveCodeAction)
                return item;
            Object.assign(item, data.original);
            item = await plugin.resolveCodeAction(item, token);
            if (item.edit) {
                item.edit = (0, rename_1.embeddedEditToSourceEdit)(item.edit, context.documents, 'codeAction', { [data.uri]: data.version });
            }
        }
        if (data?.type === 'rule') {
            const fixes = context.ruleFixes?.[data.documentUri]?.[data.ruleId]?.[data.ruleFixIndex];
            const fix = fixes?.[1][data.index];
            if (fix) {
                let edit = await fix.getWorkspaceEdit?.(fixes[0]) ?? undefined;
                if (!edit) {
                    const edits = await fix.getEdits?.(fixes[0]);
                    if (edits) {
                        edit = {
                            documentChanges: [{
                                    textDocument: {
                                        uri: data.documentUri,
                                        version: null
                                    },
                                    edits,
                                }],
                        };
                    }
                }
                if (edit) {
                    item.edit = (0, rename_1.embeddedEditToSourceEdit)(edit, context.documents, data.isFormat ? 'format' : 'codeAction', { [data.uri]: data.version });
                }
            }
        }
        return item;
    };
}
exports.register = register;
//# sourceMappingURL=codeActionResolve.js.map