import { upperFirst } from 'lodash';
import * as React from 'react';
import { Subscription } from 'rxjs';
import { catchError, map } from 'rxjs/operators';
import { gql, queryGraphQL } from '../backend/graphql';
import { PageTitle } from '../components/PageTitle';
import { eventLogger } from '../tracking/eventLogger';
import { asError, createAggregateError, isErrorLike } from '../util/errors';
import { SiteAdminLangServers } from './SiteAdminLangServers';
/**
 * A page displaying information about code intelligence on this site.
 */
export class SiteAdminCodeIntelligencePage extends React.PureComponent {
    constructor() {
        super(...arguments);
        this.state = {};
        this.subscriptions = new Subscription();
    }
    componentDidMount() {
        eventLogger.logViewEvent('SiteAdminCodeIntelligence');
        this.subscriptions.add(this.queryLanguageServerManagementStatus()
            .pipe(catchError(err => [asError(err)]), map(v => ({ statusOrError: v })))
            .subscribe(stateUpdate => this.setState(stateUpdate), err => console.error(err)));
    }
    componentWillUnmount() {
        this.subscriptions.unsubscribe();
    }
    render() {
        if (this.state.statusOrError === undefined) {
            return null;
        }
        return (React.createElement("div", null,
            React.createElement(PageTitle, { title: "Code intelligence" }),
            React.createElement("h2", null, "Code intelligence"),
            React.createElement("p", null,
                "Sourcegraph uses language servers built on the",
                ' ',
                React.createElement("a", { href: "https://langserver.org/" }, "Language Server Protocol"),
                " (LSP) standard to provide code intelligence: hovers, definitions, references, implementations, etc. See \u201C",
                React.createElement("a", { href: "https://about.sourcegraph.com/docs/code-intelligence", target: "_blank" }, "Code intelligence overview"),
                "\u201D for more information."),
            this.state.statusOrError &&
                (isErrorLike(this.state.statusOrError) ? (React.createElement("div", { className: "alert alert-danger" },
                    "Error querying language server management capabilities:",
                    ' ',
                    upperFirst(this.state.statusOrError.message))) : (!this.state.statusOrError.siteCanManage &&
                    this.state.statusOrError.reason && (React.createElement("div", { className: "alert alert-info" }, upperFirst(this.state.statusOrError.reason))))),
            React.createElement(SiteAdminLangServers, null)));
    }
    queryLanguageServerManagementStatus() {
        return queryGraphQL(gql `
            query LanguageServerManagementStatus {
                site {
                    languageServerManagementStatus {
                        siteCanManage
                        reason
                    }
                }
            }
        `).pipe(map(({ data, errors }) => {
            if (!data || !data.site || !data.site.languageServerManagementStatus) {
                throw createAggregateError(errors);
            }
            return data.site.languageServerManagementStatus;
        }));
    }
}
//# sourceMappingURL=SiteAdminCodeIntelligencePage.js.map