import { LoadingSpinner } from '@sourcegraph/react-loading-spinner';
import { upperFirst } from 'lodash';
import * as React from 'react';
import { Link } from 'react-router-dom';
import { Subject, Subscription } from 'rxjs';
import { switchMap } from 'rxjs/operators';
import { REPO_DELETE_CONFIRMATION_MESSAGE } from '.';
import { Form } from '../../components/Form';
import { PageTitle } from '../../components/PageTitle';
import { deleteRepository, setRepositoryEnabled } from '../../site-admin/backend';
import { eventLogger } from '../../tracking/eventLogger';
import { fetchRepository } from './backend';
import { ActionContainer } from './components/ActionContainer';
/**
 * The repository settings options page.
 */
export class RepoSettingsOptionsPage extends React.PureComponent {
    constructor(props) {
        super(props);
        this.repoUpdates = new Subject();
        this.subscriptions = new Subscription();
        this.enableRepository = () => setRepositoryEnabled(this.state.repo.id, true)
            .toPromise()
            .then(() => {
            this.repoUpdates.next();
            this.props.onDidUpdateRepository({ enabled: true });
        });
        this.disableRepository = () => setRepositoryEnabled(this.state.repo.id, false)
            .toPromise()
            .then(() => {
            this.repoUpdates.next();
            this.props.onDidUpdateRepository({ enabled: false });
        });
        this.deleteRepository = () => {
            if (!window.confirm(REPO_DELETE_CONFIRMATION_MESSAGE)) {
                return Promise.resolve();
            }
            return deleteRepository(this.state.repo.id)
                .toPromise()
                .then(() => {
                this.repoUpdates.next();
                this.props.history.push('/explore');
            });
        };
        this.state = {
            loading: false,
            repo: props.repo,
        };
    }
    componentDidMount() {
        eventLogger.logViewEvent('RepoSettings');
        this.subscriptions.add(this.repoUpdates
            .pipe(switchMap(() => fetchRepository(this.props.repo.name)))
            .subscribe(repo => this.setState({ repo }), err => this.setState({ error: err.message })));
    }
    componentWillUnmount() {
        this.subscriptions.unsubscribe();
    }
    render() {
        return (React.createElement("div", { className: "repo-settings-options-page" },
            React.createElement(PageTitle, { title: "Repository settings" }),
            React.createElement("h2", null, "Settings"),
            this.state.loading && React.createElement(LoadingSpinner, { className: "icon-inline" }),
            this.state.error && React.createElement("div", { className: "alert alert-danger" }, upperFirst(this.state.error)),
            React.createElement(Form, null,
                React.createElement("div", { className: "form-group" },
                    React.createElement("label", { htmlFor: "repo-settings-options-page__name" }, "Repository name"),
                    React.createElement("input", { id: "repo-settings-options-page__name", type: "text", className: "form-control", readOnly: true, disabled: true, value: this.state.repo.name, required: true, spellCheck: false, autoCapitalize: "off", autoCorrect: "off", "aria-describedby": "repo-settings-options-page__name-help" }),
                    React.createElement("small", { id: "repo-settings-options-page__name-help", className: "form-text text-muted" },
                        "This repository's name is set by its",
                        ' ',
                        this.state.repo.viewerCanAdminister ? (React.createElement(Link, { to: "/site-admin/configuration" }, "code host configuration")) : ('code host configuration'),
                        ' ',
                        "and can't be changed."),
                    React.createElement("button", { className: "btn btn-primary mt-1", disabled: true, type: "submit" }, "Rename"))),
            React.createElement(ActionContainer, { title: this.state.repo.enabled ? 'Disable access' : 'Enable access', description: this.state.repo.enabled
                    ? 'Disable access to the repository to prevent users from searching and browsing the repository.'
                    : 'The repository is disabled. Enable it to allow users to search and view the repository.', buttonClassName: this.state.repo.enabled ? 'btn-danger' : 'btn-success', buttonLabel: this.state.repo.enabled ? 'Disable access' : 'Enable access', flashText: "Updated", run: this.state.repo.enabled ? this.disableRepository : this.enableRepository }),
            React.createElement(ActionContainer, { title: "Delete repository", description: "Permanently removes this repository and all associated data from Sourcegraph. The original repository on the code host is not affected. If this repository was added by a configured code host, then it will be re-added during the next sync.", buttonClassName: "btn-danger", buttonLabel: "Delete this repository", run: this.deleteRepository })));
    }
}
//# sourceMappingURL=RepoSettingsOptionsPage.js.map