/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
import * as l10n from '@vscode/l10n';
import * as lsp from 'vscode-languageserver-protocol';
import { rangeIntersects } from '../../types/range.js';
import { getDocUri } from '../../types/textDocument.js';
import { WorkspaceEditBuilder } from '../../util/editBuilder.js';
import { DiagnosticCode } from '../diagnostics.js';
import { codeActionKindContains } from './util.js';
export class MdRemoveLinkDefinitionCodeActionProvider {
    static #removeUnusedDefTitle = l10n.t('Remove unused link definition');
    static #removeDuplicateDefTitle = l10n.t('Remove duplicate link definition');
    *getActions(doc, range, context) {
        if (!this.#isEnabled(context)) {
            return;
        }
        const unusedDiagnosticLines = new Set();
        for (const diag of context.diagnostics) {
            if (diag.code === DiagnosticCode.link_unusedDefinition && diag.data && rangeIntersects(diag.range, range)) {
                const link = diag.data;
                yield this.#getRemoveDefinitionAction(doc, link, MdRemoveLinkDefinitionCodeActionProvider.#removeUnusedDefTitle);
                unusedDiagnosticLines.add(link.source.range.start.line);
            }
        }
        for (const diag of context.diagnostics) {
            if (diag.code === DiagnosticCode.link_duplicateDefinition && diag.data && rangeIntersects(diag.range, range)) {
                const link = diag.data;
                if (!unusedDiagnosticLines.has(link.source.range.start.line)) {
                    yield this.#getRemoveDefinitionAction(doc, link, MdRemoveLinkDefinitionCodeActionProvider.#removeDuplicateDefTitle);
                }
            }
        }
    }
    #isEnabled(context) {
        if (typeof context.only === 'undefined') {
            return true;
        }
        return context.only.some(kind => codeActionKindContains(lsp.CodeActionKind.QuickFix, kind));
    }
    #getRemoveDefinitionAction(doc, definition, title) {
        const builder = new WorkspaceEditBuilder();
        const range = definition.source.range;
        builder.replace(getDocUri(doc), lsp.Range.create(range.start.line, 0, range.start.line + 1, 0), '');
        return { title, kind: lsp.CodeActionKind.QuickFix, edit: builder.getEdit() };
    }
}
//# sourceMappingURL=removeLinkDefinition.js.map