import * as React from 'react';
import { Link } from 'react-router-dom';
import { Subscription } from 'rxjs';
import { authRequired } from '../auth';
import { parseSearchURLQuery } from '../search';
import { SearchNavbarItem } from '../search/input/SearchNavbarItem';
import { NavLinks } from './NavLinks';
export class GlobalNavbar extends React.PureComponent {
    constructor(props) {
        super(props);
        this.state = {};
        this.subscriptions = new Subscription();
        /**
         * Reads initial state from the props (i.e. URL parameters).
         */
        const options = parseSearchURLQuery(props.location.search || '');
        if (options) {
            props.onNavbarQueryChange(options.query);
        }
        else {
            // If we have no component state, then we may have gotten unmounted during a route change.
            const state = props.location.state;
            props.onNavbarQueryChange(state ? state.query : '');
        }
    }
    componentDidMount() {
        this.subscriptions.add(authRequired.subscribe(authRequired => this.setState({ authRequired })));
    }
    componentDidUpdate(prevProps) {
        if (prevProps.location.search !== this.props.location.search) {
            const options = parseSearchURLQuery(this.props.location.search || '');
            if (options) {
                this.props.onNavbarQueryChange(options.query);
            }
        }
    }
    componentWillUnmount() {
        this.subscriptions.unsubscribe();
    }
    render() {
        const logo = React.createElement("img", { className: "global-navbar__logo", src: "/.assets/img/sourcegraph-mark.svg" });
        return (React.createElement("div", { className: `global-navbar ${this.props.lowProfile ? '' : 'global-navbar--bg'}` },
            this.props.lowProfile ? (React.createElement("div", null)) : (React.createElement(React.Fragment, null,
                this.state.authRequired ? (React.createElement("div", { className: "global-navbar__logo-link" }, logo)) : (React.createElement(Link, { to: "/search", className: "global-navbar__logo-link" }, logo)),
                !this.state.authRequired && (React.createElement("div", { className: "global-navbar__search-box-container" },
                    React.createElement(SearchNavbarItem, Object.assign({}, this.props, { navbarSearchQuery: this.props.navbarSearchQuery, onChange: this.props.onNavbarQueryChange })))))),
            !this.state.authRequired && React.createElement(NavLinks, Object.assign({}, this.props))));
    }
}
//# sourceMappingURL=GlobalNavbar.js.map