import * as React from 'react';
import { parseSearchURLQuery } from '..';
import { Form } from '../../components/Form';
import { PageTitle } from '../../components/PageTitle';
import { OpenHelpPopoverButton } from '../../global/OpenHelpPopoverButton';
import { eventLogger } from '../../tracking/eventLogger';
import { limitString } from '../../util';
import { queryIndexOfScope, submitSearch } from '../helpers';
import { SavedQueries } from '../saved-queries/SavedQueries';
import { QueryInput } from './QueryInput';
import { SearchButton } from './SearchButton';
import { SearchFilterChips } from './SearchFilterChips';
/**
 * The search page
 */
export class SearchPage extends React.Component {
    constructor(props) {
        super(props);
        this.onUserQueryChange = (userQuery) => {
            this.setState({ userQuery });
            if (window.context.sourcegraphDotComMode) {
                if (queryIndexOfScope(userQuery, 'repogroup:sample') !== -1) {
                    localStorage.removeItem(SearchPage.HIDE_REPOGROUP_SAMPLE_STORAGE_KEY);
                }
                else {
                    localStorage.setItem(SearchPage.HIDE_REPOGROUP_SAMPLE_STORAGE_KEY, 'true');
                }
            }
        };
        this.onSubmit = (event) => {
            event.preventDefault();
            submitSearch(this.props.history, { query: this.state.userQuery }, 'home');
        };
        const searchOptions = parseSearchURLQuery(props.location.search);
        this.state = {
            userQuery: (searchOptions && searchOptions.query) || '',
        };
    }
    componentDidMount() {
        eventLogger.logViewEvent('Home');
        if (window.context.sourcegraphDotComMode &&
            !localStorage.getItem(SearchPage.HIDE_REPOGROUP_SAMPLE_STORAGE_KEY) &&
            !this.state.userQuery) {
            this.setState({ userQuery: 'repogroup:sample' });
        }
    }
    render() {
        return (React.createElement("div", { className: "search-page" },
            React.createElement(PageTitle, { title: this.getPageTitle() }),
            React.createElement("img", { className: "search-page__logo", src: `${window.context.assetsRoot}/img/sourcegraph` +
                    (this.props.isLightTheme ? '-light' : '') +
                    '-head-logo.svg' }),
            React.createElement(Form, { className: "search search-page__container", onSubmit: this.onSubmit },
                React.createElement("div", { className: "search-page__input-container" },
                    React.createElement(QueryInput, Object.assign({}, this.props, { value: this.state.userQuery, onChange: this.onUserQueryChange, autoFocus: 'cursor-at-end', hasGlobalQueryBehavior: true })),
                    React.createElement(SearchButton, null),
                    React.createElement(OpenHelpPopoverButton, { onHelpPopoverToggle: this.props.onHelpPopoverToggle })),
                React.createElement("div", { className: "search-page__input-sub-container" },
                    React.createElement(SearchFilterChips, { location: this.props.location, history: this.props.history, query: this.state.userQuery }))),
            React.createElement("div", { className: "search search-page__query-container" },
                React.createElement(SavedQueries, Object.assign({}, this.props, { hideExampleSearches: true })))));
    }
    getPageTitle() {
        const options = parseSearchURLQuery(this.props.location.search);
        if (options && options.query) {
            return `${limitString(this.state.userQuery, 25, true)}`;
        }
        return undefined;
    }
}
SearchPage.HIDE_REPOGROUP_SAMPLE_STORAGE_KEY = 'SearchPage/hideRepogroupSample';
//# sourceMappingURL=SearchPage.js.map