import { __decorate, __param } from '../../../../../../../external/tslib/tslib.es6.js';
import { $ } from '../../../../base/browser/dom.js';
import { toErrorMessage } from '../../../../base/common/errorMessage.js';
import { revive } from '../../../../base/common/marshalling.js';
import { URI } from '../../../../base/common/uri.js';
import { IKeybindingService } from '../../../../platform/keybinding/common/keybinding.js';
import { ILabelService } from '../../../../platform/label/common/label.js';
import { ILogService } from '../../../../platform/log/common/log.js';
import { ChatRequestTextPart, ChatRequestDynamicVariablePart, ChatRequestAgentPart } from '../common/chatParserTypes.js';
import { contentRefUrl } from '../common/annotations.js';
const variableRefUrl = 'http://_vscodedecoration_';
let ChatMarkdownDecorationsRenderer = class ChatMarkdownDecorationsRenderer {
    constructor(keybindingService, labelService, logService) {
        this.keybindingService = keybindingService;
        this.labelService = labelService;
        this.logService = logService;
    }
    convertParsedRequestToMarkdown(parsedRequest) {
        let result = '';
        for (const part of parsedRequest.parts) {
            if (part instanceof ChatRequestTextPart) {
                result += part.text;
            }
            else {
                const uri = part instanceof ChatRequestDynamicVariablePart && ( part.data.map(d => d.value)).find((d) => d instanceof URI)
                    || undefined;
                const title = uri ? encodeURIComponent(this.labelService.getUriLabel(uri, { relative: true })) :
                    part instanceof ChatRequestAgentPart ? part.agent.id :
                        '';
                result += `[${part.text}](${variableRefUrl}?${title})`;
            }
        }
        return result;
    }
    walkTreeAndAnnotateReferenceLinks(element) {
        element.querySelectorAll('a').forEach(a => {
            const href = a.getAttribute('data-href');
            if (href) {
                if (href.startsWith(variableRefUrl)) {
                    const title = decodeURIComponent(href.slice(variableRefUrl.length + 1));
                    a.parentElement.replaceChild(this.renderResourceWidget(a.textContent, title), a);
                }
                else if (href.startsWith(contentRefUrl)) {
                    this.renderFileWidget(href, a);
                }
                else if (href.startsWith('command:')) {
                    this.injectKeybindingHint(a, href, this.keybindingService);
                }
            }
        });
    }
    renderFileWidget(href, a) {
        const fullUri = ( URI.parse(href));
        let location;
        try {
            location = revive(JSON.parse(fullUri.fragment));
        }
        catch (err) {
            this.logService.error('Invalid chat widget render data JSON', toErrorMessage(err));
            return;
        }
        if (!location.uri || !URI.isUri(location.uri)) {
            this.logService.error(`Invalid chat widget render data: ${fullUri.fragment}`);
            return;
        }
        const fragment = location.range ? `${location.range.startLineNumber}-${location.range.endLineNumber}` : '';
        a.setAttribute('data-href', ( location.uri.with({ fragment }).toString()));
        const label = this.labelService.getUriLabel(location.uri, { relative: true });
        a.title = location.range ?
            `${label}#${location.range.startLineNumber}-${location.range.endLineNumber}` :
            label;
    }
    renderResourceWidget(name, title) {
        const container = $('span.chat-resource-widget');
        const alias = $('span', undefined, name);
        alias.title = title;
        container.appendChild(alias);
        return container;
    }
    injectKeybindingHint(a, href, keybindingService) {
        const command = href.match(/command:([^\)]+)/)?.[1];
        if (command) {
            const kb = keybindingService.lookupKeybinding(command);
            if (kb) {
                const keybinding = kb.getLabel();
                if (keybinding) {
                    a.textContent = `${a.textContent} (${keybinding})`;
                }
            }
        }
    }
};
ChatMarkdownDecorationsRenderer = ( __decorate([
    ( __param(0, IKeybindingService)),
    ( __param(1, ILabelService)),
    ( __param(2, ILogService))
], ChatMarkdownDecorationsRenderer));
export { ChatMarkdownDecorationsRenderer };
