
import { localize } from '../../../../nls.js';
import { $, append, createTextNode } from '../../../../base/browser/dom.js';
import { parseLinkedText } from '../../../../base/common/linkedText.js';
import Severity$1 from '../../../../base/common/severity.js';
import { SeverityIcon } from '../../../../base/browser/ui/severityIcon/severityIcon.js';
import { TextSearchCompleteMessageType } from '../../../services/search/common/searchExtTypes.js';
import { Schemas } from '../../../../base/common/network.js';
import { Link } from '../../../../platform/opener/browser/link.js';
import { URI } from '../../../../base/common/uri.js';

const renderSearchMessage = (
    message,
    instantiationService,
    notificationService,
    openerService,
    commandService,
    disposableStore,
    triggerSearch
) => {
    const div = $("div.providerMessage");
    const linkedText = parseLinkedText(message.text);
    append(div, $("." + SeverityIcon.className(
        message.type === TextSearchCompleteMessageType.Information ? Severity$1.Info : Severity$1.Warning
    ).split(" ").join(".")));
    for (const node of linkedText.nodes) {
        if (typeof node === "string") {
            append(div, createTextNode(node));
        } else {
            const link = instantiationService.createInstance(Link, div, node, {
                opener: async href => {
                    if (!message.trusted) {
                        return;
                    }
                    const parsed = ( URI.parse(href, true));
                    if (parsed.scheme === Schemas.command && message.trusted) {
                        const result = await commandService.executeCommand(parsed.path);
                        if (result?.triggerSearch) {
                            triggerSearch();
                        }
                    } else if (parsed.scheme === Schemas.https) {
                        openerService.open(parsed);
                    } else {
                        if (parsed.scheme === Schemas.command && !message.trusted) {
                            notificationService.error(( localize(13413, "Unable to open command link from untrusted source: {0}", href)));
                        } else {
                            notificationService.error(( localize(13414, "Unable to open unknown link: {0}", href)));
                        }
                    }
                }
            });
            disposableStore.add(link);
        }
    }
    return div;
};

export { renderSearchMessage };
