import { upperFirst } from 'lodash';
import AlertCircleIcon from 'mdi-react/AlertCircleIcon';
import DoNotDisturbIcon from 'mdi-react/DoNotDisturbIcon';
import MapSearchIcon from 'mdi-react/MapSearchIcon';
import * as React from 'react';
import { Route, Switch } from 'react-router';
import { Subject, Subscription } from 'rxjs';
import { switchMap } from 'rxjs/operators';
import { HeroPage } from '../../components/HeroPage';
import { RepoHeaderContributionPortal } from '../RepoHeaderContributionPortal';
import { fetchRepository } from './backend';
import { RepoSettingsIndexPage } from './RepoSettingsIndexPage';
import { RepoSettingsMirrorPage } from './RepoSettingsMirrorPage';
import { RepoSettingsOptionsPage } from './RepoSettingsOptionsPage';
import { RepoSettingsSidebar } from './RepoSettingsSidebar';
const NotFoundPage = () => (React.createElement(HeroPage, { icon: MapSearchIcon, title: "404: Not Found", subtitle: "Sorry, the requested repository page was not found." }));
/**
 * Renders a layout of a sidebar and a content area to display pages related to
 * a repository's settings.
 */
export class RepoSettingsArea extends React.Component {
    constructor() {
        super(...arguments);
        this.state = {};
        this.repoChanges = new Subject();
        this.subscriptions = new Subscription();
    }
    componentDidMount() {
        this.subscriptions.add(this.repoChanges
            .pipe(switchMap(({ name }) => fetchRepository(name)))
            .subscribe(repo => this.setState({ repo }), err => this.setState({ error: err.message })));
        this.repoChanges.next(this.props.repo);
    }
    componentWillReceiveProps(props) {
        if (props.repo !== this.props.repo) {
            this.repoChanges.next(props.repo);
        }
    }
    componentWillUnmount() {
        this.subscriptions.unsubscribe();
    }
    render() {
        if (this.state.error) {
            return React.createElement(HeroPage, { icon: AlertCircleIcon, title: "Error", subtitle: upperFirst(this.state.error) });
        }
        if (this.state.repo === undefined) {
            return null;
        }
        if (this.state.repo === null) {
            return React.createElement(NotFoundPage, null);
        }
        if (!this.state.repo.viewerCanAdminister) {
            return (React.createElement(HeroPage, { icon: DoNotDisturbIcon, title: "Forbidden", subtitle: "You are not authorized to view or change this repository's settings." }));
        }
        if (!this.props.user) {
            return null;
        }
        const transferProps = {
            user: this.props.user,
            repo: this.state.repo,
        };
        return (React.createElement("div", { className: "repo-settings-area area" },
            React.createElement(RepoHeaderContributionPortal, { position: "nav", element: React.createElement("span", { key: "graph", className: "repo-settings-area__header-item" }, "Settings"), repoHeaderContributionsLifecycleProps: this.props.repoHeaderContributionsLifecycleProps }),
            React.createElement(RepoSettingsSidebar, Object.assign({ className: "area__sidebar" }, this.props, transferProps)),
            React.createElement("div", { className: "area__content" },
                React.createElement(Switch, null,
                    React.createElement(Route, { path: `${this.props.match.url}`, key: "hardcoded-key" // see https://github.com/ReactTraining/react-router/issues/4578#issuecomment-334489490
                        , exact: true, 
                        // tslint:disable-next-line:jsx-no-lambda
                        render: routeComponentProps => (React.createElement(RepoSettingsOptionsPage, Object.assign({}, routeComponentProps, transferProps, { onDidUpdateRepository: this.props.onDidUpdateRepository }))) }),
                    React.createElement(Route, { path: `${this.props.match.url}/index`, key: "hardcoded-key" // see https://github.com/ReactTraining/react-router/issues/4578#issuecomment-334489490
                        , exact: true, 
                        // tslint:disable-next-line:jsx-no-lambda
                        render: routeComponentProps => (React.createElement(RepoSettingsIndexPage, Object.assign({}, routeComponentProps, transferProps))) }),
                    React.createElement(Route, { path: `${this.props.match.url}/mirror`, key: "hardcoded-key" // see https://github.com/ReactTraining/react-router/issues/4578#issuecomment-334489490
                        , exact: true, 
                        // tslint:disable-next-line:jsx-no-lambda
                        render: routeComponentProps => (React.createElement(RepoSettingsMirrorPage, Object.assign({}, routeComponentProps, transferProps, { onDidUpdateRepository: this.props.onDidUpdateRepository }))) }),
                    React.createElement(Route, { key: "hardcoded-key", component: NotFoundPage })))));
    }
}
//# sourceMappingURL=RepoSettingsArea.js.map