import { __decorate, __param } from '../../../../../../../external/tslib/tslib.es6.js';
import { h, $, append } from '../../../../base/browser/dom.js';
import { renderIcon } from '../../../../base/browser/ui/iconLabel/iconLabels.js';
import { CancellationTokenSource } from '../../../../base/common/cancellation.js';
import { Codicon } from '../../../../base/common/codicons.js';
import { Emitter } from '../../../../base/common/event.js';
import { Disposable } from '../../../../base/common/lifecycle.js';
import { FileAccess } from '../../../../base/common/network.js';
import { ThemeIcon } from '../../../../base/common/themables.js';
import { URI } from '../../../../base/common/uri.js';
import { localizeWithPath } from '../../../../nls.js';
import { getFullyQualifiedId } from '../common/chatAgents.js';
import { IChatAgentService, IChatAgentNameService } from '../common/chatAgents.service.js';
import { showExtensionsWithIdsCommandId } from '../../extensions/browser/extensionsActions.js';
import { verifiedPublisherIcon } from '../../extensions/browser/extensionsIcons.js';
import { IExtensionsWorkbenchService } from '../../extensions/common/extensions.service.js';
const _moduleId = "vs/workbench/contrib/chat/browser/chatAgentHover";
let ChatAgentHover = class ChatAgentHover extends Disposable {
    constructor(chatAgentService, extensionService, chatAgentNameService) {
        super();
        this.chatAgentService = chatAgentService;
        this.extensionService = extensionService;
        this.chatAgentNameService = chatAgentNameService;
        this._onDidChangeContents = this._register(( (new Emitter())));
        this.onDidChangeContents = this._onDidChangeContents.event;
        const hoverElement = h('.chat-agent-hover@root', [
            h('.chat-agent-hover-header', [
                h('.chat-agent-hover-icon@icon'),
                h('.chat-agent-hover-details', [
                    h('.chat-agent-hover-name@name'),
                    h('.chat-agent-hover-extension', [
                        h('.chat-agent-hover-extension-name@extensionName'),
                        h('.chat-agent-hover-separator@separator'),
                        h('.chat-agent-hover-publisher@publisher'),
                    ]),
                ]),
            ]),
            h('.chat-agent-hover-warning@warning'),
            h('span.chat-agent-hover-description@description'),
        ]);
        this.domNode = hoverElement.root;
        this.icon = hoverElement.icon;
        this.name = hoverElement.name;
        this.extensionName = hoverElement.extensionName;
        this.description = hoverElement.description;
        hoverElement.separator.textContent = '|';
        const verifiedBadge = $('span.extension-verified-publisher', undefined, renderIcon(verifiedPublisherIcon));
        this.publisherName = $('span.chat-agent-hover-publisher-name');
        append(hoverElement.publisher, verifiedBadge, this.publisherName);
        hoverElement.warning.appendChild(renderIcon(Codicon.warning));
        hoverElement.warning.appendChild($('span', undefined, ( localizeWithPath(_moduleId, 0, "This chat extension is using a reserved name."))));
    }
    setAgent(id) {
        const agent = this.chatAgentService.getAgent(id);
        if (agent.metadata.icon instanceof URI) {
            const avatarIcon = $('img.icon');
            avatarIcon.src = ( (FileAccess.uriToBrowserUri(agent.metadata.icon).toString(true)));
            this.icon.replaceChildren($('.avatar', undefined, avatarIcon));
        }
        else if (agent.metadata.themeIcon) {
            const avatarIcon = $(ThemeIcon.asCSSSelector(agent.metadata.themeIcon));
            this.icon.replaceChildren($('.avatar.codicon-avatar', undefined, avatarIcon));
        }
        this.domNode.classList.toggle('noExtensionName', !!agent.isDynamic);
        const isAllowed = this.chatAgentNameService.getAgentNameRestriction(agent);
        this.name.textContent = isAllowed ? `@${agent.name}` : getFullyQualifiedId(agent);
        this.extensionName.textContent = agent.extensionDisplayName;
        this.publisherName.textContent = agent.publisherDisplayName ?? agent.extensionPublisherId;
        let description = agent.description ?? '';
        if (description) {
            if (!description.match(/[\.\?\!] *$/)) {
                description += '.';
            }
        }
        this.description.textContent = description;
        this.domNode.classList.toggle('allowedName', isAllowed);
        this.domNode.classList.toggle('verifiedPublisher', false);
        if (!agent.isDynamic) {
            const cancel = this._register(( (new CancellationTokenSource())));
            this.extensionService.getExtensions([{ id: agent.extensionId.value }], cancel.token).then(extensions => {
                cancel.dispose();
                const extension = extensions[0];
                if (extension?.publisherDomain?.verified) {
                    this.domNode.classList.toggle('verifiedPublisher', true);
                    this._onDidChangeContents.fire();
                }
            });
        }
    }
};
ChatAgentHover = ( (__decorate([
    ( (__param(0, IChatAgentService))),
    ( (__param(1, IExtensionsWorkbenchService))),
    ( (__param(2, IChatAgentNameService)))
], ChatAgentHover)));
function getChatAgentHoverOptions(getAgent, commandService) {
    return {
        actions: [
            {
                commandId: showExtensionsWithIdsCommandId,
                label: ( localizeWithPath(_moduleId, 1, "View Extension")),
                run: () => {
                    const agent = getAgent();
                    if (agent) {
                        commandService.executeCommand(showExtensionsWithIdsCommandId, [agent.extensionId.value]);
                    }
                },
            }
        ]
    };
}
export { ChatAgentHover, getChatAgentHoverOptions };
