"use strict";
/*
 * Copyright OpenSearch Contributors
 * SPDX-License-Identifier: Apache-2.0
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.pplLintCodeActionProvider = void 0;
const diagnostic_to_marker_1 = require("./diagnostic_to_marker");
const fix_registry_1 = require("./fix_registry");
// Code-action provider that surfaces quick-fixes for PPL markers on two
// channels: lint diagnostics (`ppl-lint`, owner PPL_LINT) and syntax errors
// (`ppl-syntax`, owner PPL_WORKER — e.g. the command-typo suggestion). For each
// marker with an associated fix it returns a quick-fix code action with a
// workspace edit. Markers from any other source are ignored.
//
// The fix payload is NOT read off the marker: Monaco's MarkerService rebuilds
// each marker from a fixed field list when `setModelMarkers` is called, dropping
// any custom property, so a fix hung off the marker never survives to here.
// Instead each lifecycle records fixes in a side table keyed by the marker
// fields the service preserves (position + message); we re-associate them here,
// reading the table that matches the marker's source.
exports.pplLintCodeActionProvider = {
    provideCodeActions(model, _range, context) {
        const actions = [];
        for (const marker of context.markers) {
            const key = (0, fix_registry_1.markerFixKey)(marker);
            let fix;
            if (marker.source === diagnostic_to_marker_1.LINT_MARKER_SOURCE) {
                fix = (0, fix_registry_1.getModelFix)(model, key);
            }
            else if (marker.source === diagnostic_to_marker_1.SYNTAX_MARKER_SOURCE) {
                fix = (0, fix_registry_1.getModelSyntaxFix)(model, key);
            }
            else {
                continue;
            }
            if (!fix) {
                continue;
            }
            // Use the fix's own range when it targets a span different from the
            // squiggle (e.g. deleting one character before the underlined name);
            // otherwise replace the marker's range.
            const editRange = fix.range ?? {
                startLineNumber: marker.startLineNumber,
                startColumn: marker.startColumn,
                endLineNumber: marker.endLineNumber,
                endColumn: marker.endColumn,
            };
            const textEdit = {
                resource: model.uri,
                textEdit: {
                    range: editRange,
                    text: fix.text,
                },
                // Intentionally omit versionId. Monaco's bulk-edit service rejects an
                // edit whose captured versionId no longer matches the model ("model
                // changed in the meantime"), and in the live editor the version advances
                // between the moment the code action is computed and the moment the user
                // clicks it (debounced re-lint, re-tokenize, autocomplete all bump it) —
                // so a captured versionId makes the quick-fix silently do nothing. The
                // fix range is absolute, so applying it without the version guard is safe.
                versionId: undefined,
            };
            actions.push({
                title: fix.title,
                diagnostics: [marker],
                kind: 'quickfix',
                edit: {
                    edits: [textEdit],
                },
            });
        }
        return {
            actions,
            dispose: () => { },
        };
    },
};
//# sourceMappingURL=code_action_provider.js.map