import { LoadingSpinner } from '@sourcegraph/react-loading-spinner';
import { upperFirst } from 'lodash';
import AlertCircleIcon from 'mdi-react/AlertCircleIcon';
import FileIcon from 'mdi-react/FileIcon';
import SearchIcon from 'mdi-react/SearchIcon';
import TimerSandIcon from 'mdi-react/TimerSandIcon';
import * as React from 'react';
import { Link } from 'react-router-dom';
import { buildSearchURLQuery, parseSearchURLQuery } from '..';
import { FileMatch } from '../../components/FileMatch';
import { ModalContainer } from '../../components/ModalContainer';
import { OpenHelpPopoverButton } from '../../global/OpenHelpPopoverButton';
import { eventLogger } from '../../tracking/eventLogger';
import { isErrorLike } from '../../util/errors';
import { RepositoryIcon } from '../../util/icons'; // TODO: Switch to mdi icon
import { SavedQueryCreateForm } from '../saved-queries/SavedQueryCreateForm';
import { CommitSearchResult } from './CommitSearchResult';
import { RepositorySearchResult } from './RepositorySearchResult';
import { SearchResultsInfoBar } from './SearchResultsInfoBar';
export class SearchResultsListOld extends React.PureComponent {
    constructor() {
        super(...arguments);
        this.logEvent = () => eventLogger.log('SearchResultClicked');
    }
    render() {
        const parsedQuery = parseSearchURLQuery(this.props.location.search);
        return (React.createElement("div", { className: "search-results-list" },
            this.props.showSavedQueryModal && (React.createElement(ModalContainer, { onClose: this.props.onSavedQueryModalClose, component: React.createElement(SavedQueryCreateForm, { user: this.props.user, values: { query: parsedQuery ? parsedQuery.query : '' }, onDidCancel: this.props.onSavedQueryModalClose, onDidCreate: this.props.onDidCreateSavedQuery }) })),
            this.props.resultsOrError === undefined ? (React.createElement("div", { className: "text-center" },
                React.createElement(LoadingSpinner, { className: "icon-inline" }),
                " Loading")) : isErrorLike(this.props.resultsOrError) ? (
            /* GraphQL, network, query syntax error */
            React.createElement("div", { className: "alert alert-warning" },
                React.createElement(AlertCircleIcon, { className: "icon-inline" }),
                upperFirst(this.props.resultsOrError.message))) : ((() => {
                const results = this.props.resultsOrError;
                return (React.createElement(React.Fragment, null,
                    React.createElement(SearchResultsInfoBar, { user: this.props.user, results: results, allExpanded: this.props.allExpanded, didSave: this.props.didSave, onDidCreateSavedQuery: this.props.onDidCreateSavedQuery, onExpandAllResultsToggle: this.props.onExpandAllResultsToggle, onSaveQueryClick: this.props.onSaveQueryClick, onShowMoreResultsClick: this.props.onShowMoreResultsClick }),
                    results.results
                        .slice(0, this.props.uiLimit)
                        .map((result, i) => this.renderResult(result, i <= 15)),
                    (results.limitHit || results.results.length > this.props.uiLimit) && (React.createElement("button", { className: "btn btn-secondary btn-block", onClick: this.props.onShowMoreResultsClick }, "Show more")),
                    results.alert ? (React.createElement("div", { className: "alert alert-info" },
                        React.createElement("h3", null,
                            React.createElement(AlertCircleIcon, { className: "icon-inline" }),
                            " ",
                            results.alert.title),
                        React.createElement("p", null, results.alert.description),
                        results.alert.proposedQueries && (React.createElement(React.Fragment, null,
                            React.createElement("h4", null, "Did you mean:"),
                            React.createElement("ul", { className: "list-unstyled" }, results.alert.proposedQueries.map(proposedQuery => (React.createElement("li", { key: proposedQuery.query },
                                React.createElement(Link, { className: "btn btn-secondary btn-sm", to: '/search?' + buildSearchURLQuery(proposedQuery) }, proposedQuery.query || proposedQuery.description),
                                proposedQuery.query &&
                                    proposedQuery.description &&
                                    ` — ${proposedQuery.description}`)))))),
                        ' ')) : (results.results.length === 0 &&
                        (results.timedout.length > 0 ? (
                        /* No results, but timeout hit */
                        React.createElement("div", { className: "alert alert-warning" },
                            React.createElement("h3", null,
                                React.createElement(TimerSandIcon, { className: "icon-inline" }),
                                " Search timed out"),
                            this.renderRecommendations([
                                React.createElement(React.Fragment, null, "Try narrowing your query , or specifying a longer \"timeout:\" in your query."),
                                /* If running on non-cluster, give some smart advice */
                                ...(!window.context.sourcegraphDotComMode &&
                                    !window.context.isClusterDeployment
                                    ? [
                                        React.createElement(React.Fragment, null, "Upgrade to Sourcegraph Enterprise for a highly scalable Kubernetes cluster deployment option."),
                                        window.context.likelyDockerOnMac
                                            ? 'Use Docker Machine instead of Docker for Mac for better performance on macOS'
                                            : 'Run Sourcegraph on a server with more CPU and memory, or faster disk IO',
                                    ]
                                    : []),
                            ]))) : (React.createElement(React.Fragment, null,
                            React.createElement("div", { className: "alert alert-info d-flex" },
                                React.createElement("h3", { className: "m-0" },
                                    React.createElement(SearchIcon, { className: "icon-inline" }),
                                    " No results"))))))));
            })()),
            React.createElement("div", { className: "pb-4" }),
            this.props.resultsOrError !== undefined && (React.createElement(OpenHelpPopoverButton, { className: "mb-2", onHelpPopoverToggle: this.props.onHelpPopoverToggle, text: "Not seeing expected results?" }))));
    }
    /**
     * Renders the given recommendations in a list if multiple, otherwise returns the first one or undefined
     */
    renderRecommendations(recommendations) {
        if (recommendations.length <= 1) {
            return recommendations[0];
        }
        return (React.createElement(React.Fragment, null,
            React.createElement("h4", null, "Recommendations:"),
            React.createElement("ul", null, recommendations.map((recommendation, i) => React.createElement("li", { key: i }, recommendation)))));
    }
    renderResult(result, expanded) {
        switch (result.__typename) {
            case 'Repository':
                return React.createElement(RepositorySearchResult, { key: 'repo:' + result.id, result: result, onSelect: this.logEvent });
            case 'FileMatch':
                return (React.createElement(FileMatch, { key: 'file:' + result.file.url, icon: result.lineMatches && result.lineMatches.length > 0 ? RepositoryIcon : FileIcon, result: result, onSelect: this.logEvent, expanded: false, showAllMatches: false, isLightTheme: this.props.isLightTheme, allExpanded: this.props.allExpanded }));
            case 'CommitSearchResult':
                return (React.createElement(CommitSearchResult, { key: 'commit:' + result.commit.id, location: this.props.location, result: result, onSelect: this.logEvent, expanded: expanded, allExpanded: this.props.allExpanded }));
        }
        return undefined;
    }
}
//# sourceMappingURL=SearchResultsListOld.js.map