import { upperFirst } from 'lodash';
import * as React from 'react';
import { Link } from 'react-router-dom';
import { Subscription } from 'rxjs';
import { catchError, map } from 'rxjs/operators';
import { gql, queryGraphQL } from '../backend/graphql';
import { PageTitle } from '../components/PageTitle';
import { SettingsArea } from '../settings/SettingsArea';
import { createAggregateError, isErrorLike } from '../util/errors';
function querySiteConfigDeprecatedSettings() {
    return queryGraphQL(gql `
        query SiteConfigDeprecatedSettings {
            site {
                deprecatedSiteConfigurationSettings
            }
        }
    `).pipe(map(({ data, errors }) => {
        if (!data || !data.site) {
            throw createAggregateError(errors);
        }
        return data.site.deprecatedSiteConfigurationSettings;
    }));
}
export class SiteAdminSettingsPage extends React.Component {
    constructor() {
        super(...arguments);
        this.state = {};
        this.subscriptions = new Subscription();
    }
    componentDidMount() {
        this.subscriptions.add(querySiteConfigDeprecatedSettings()
            .pipe(catchError(error => [error]), map(c => ({ deprecatedSettingsOrError: c })))
            .subscribe(stateUpdate => this.setState(stateUpdate), err => console.error(err)));
    }
    componentWillUnmount() {
        this.subscriptions.unsubscribe();
    }
    render() {
        return (React.createElement(React.Fragment, null,
            React.createElement(PageTitle, { title: "Site settings" }),
            React.createElement(SettingsArea, Object.assign({}, this.props, { subject: this.props.site, authenticatedUser: this.props.user, extraHeader: React.createElement(React.Fragment, null,
                    React.createElement("p", null, "Global settings apply to all organizations and users. Settings for a user or organization override global settings."),
                    this.state.deprecatedSettingsOrError !== undefined &&
                        this.state.deprecatedSettingsOrError !== null &&
                        (isErrorLike(this.state.deprecatedSettingsOrError) ? (React.createElement("div", { className: "alert alert-danger my-2" }, upperFirst(this.state.deprecatedSettingsOrError.message))) : (React.createElement("div", { className: "alert alert-warning my-2" },
                            React.createElement("p", null,
                                "Your ",
                                React.createElement(Link, { to: "/site-admin/configuration" }, "site configuration"),
                                " contains a ",
                                React.createElement("strong", null, "deprecated"),
                                " ",
                                React.createElement("code", null, "settings"),
                                " field (contents below). Support for providing settings in the site configuration file will be removed in a future Sourcegraph release."),
                            React.createElement("p", null,
                                "To fix this problem: Add these settings to the editable global settings below, and remove them from",
                                ' ',
                                React.createElement(Link, { to: "/site-admin/configuration" }, "site configuration"),
                                "."),
                            React.createElement("pre", { className: "form-control" },
                                React.createElement("code", null, this.state.deprecatedSettingsOrError)))))) }))));
    }
}
//# sourceMappingURL=SiteAdminSettingsPage.js.map