import { ReplaySubject } from 'rxjs';
import { filter, mergeMap, take, tap } from 'rxjs/operators';
import { authRequired } from '../auth';
import { gql, queryGraphQL } from '../backend/graphql';
import { createAggregateError } from '../util/errors';
/**
 * The latest state of the site flags.
 */
export const siteFlags = new ReplaySubject(1);
/**
 * refreshSiteFlags refreshes the site flags. The result is available from
 * the siteFlags const.
 */
export function refreshSiteFlags() {
    return authRequired.pipe(take(1), filter(authRequired => !authRequired), mergeMap(() => queryGraphQL(gql `
                query SiteFlags {
                    site {
                        needsRepositoryConfiguration
                        noRepositoriesEnabled
                        alerts {
                            type
                            message
                            isDismissibleWithKey
                        }
                        hasCodeIntelligence
                        externalAuthEnabled
                        disableBuiltInSearches
                        sendsEmailVerificationEmails
                        updateCheck {
                            pending
                            checkedAt
                            errorMessage
                            updateVersionAvailable
                        }
                    }
                }
            `)), tap(({ data, errors }) => {
        if (!data || !data.site) {
            throw createAggregateError(errors);
        }
        siteFlags.next(data.site);
    }), mergeMap(() => []));
}
refreshSiteFlags()
    .toPromise()
    .then(() => void 0, err => console.error(err));
//# sourceMappingURL=backend.js.map