import * as React from 'react';
import { Subject, Subscription } from 'rxjs';
import { distinctUntilChanged, map, startWith } from 'rxjs/operators';
import { gql, queryGraphQL } from '../../backend/graphql';
import { FilteredConnection } from '../../components/FilteredConnection';
import { createAggregateError } from '../../util/errors';
import { GitCommitNode } from '../commits/GitCommitNode';
import { gitCommitFragment } from '../commits/RepositoryCommitsPage';
function queryRepositoryComparisonCommits(args) {
    return queryGraphQL(gql `
            query RepositoryComparisonCommits($repo: ID!, $base: String, $head: String, $first: Int) {
                node(id: $repo) {
                    ... on Repository {
                        comparison(base: $base, head: $head) {
                            commits(first: $first) {
                                nodes {
                                    ...GitCommitFields
                                }
                                pageInfo {
                                    hasNextPage
                                }
                            }
                        }
                    }
                }
            }
            ${gitCommitFragment}
        `, args).pipe(map(({ data, errors }) => {
        if (!data || !data.node) {
            throw createAggregateError(errors);
        }
        const repo = data.node;
        if (!repo.comparison || !repo.comparison.commits || errors) {
            throw createAggregateError(errors);
        }
        return repo.comparison.commits;
    }));
}
/** A page with a list of commits in the comparison. */
export class RepositoryCompareCommitsPage extends React.PureComponent {
    constructor() {
        super(...arguments);
        this.componentUpdates = new Subject();
        this.updates = new Subject();
        this.subscriptions = new Subscription();
        this.queryCommits = (args) => queryRepositoryComparisonCommits(Object.assign({}, args, { repo: this.props.repo.id, base: this.props.base.rev || null, head: this.props.head.rev || null }));
    }
    componentDidMount() {
        this.subscriptions.add(this.componentUpdates
            .pipe(startWith(this.props), distinctUntilChanged((a, b) => a.repo.id === b.repo.id && a.base.rev === b.base.rev && a.head.rev === b.head.rev))
            .subscribe(() => this.updates.next()));
    }
    componentWillReceiveProps(nextProps) {
        this.componentUpdates.next(nextProps);
    }
    componentWillUnmount() {
        this.subscriptions.unsubscribe();
    }
    render() {
        return (React.createElement("div", { className: "repository-compare-page" },
            React.createElement("div", { className: "card" },
                React.createElement("div", { className: "card-header" }, "Commits"),
                React.createElement(FilteredConnection, { listClassName: "list-group list-group-flush", noun: "commit", pluralNoun: "commits", compact: true, queryConnection: this.queryCommits, nodeComponent: GitCommitNode, nodeComponentProps: {
                        repoName: this.props.repo.name,
                        className: 'list-group-item',
                        compact: true,
                    }, defaultFirst: 50, hideSearch: true, noSummaryIfAllNodesVisible: true, updates: this.updates, history: this.props.history, location: this.props.location }))));
    }
}
//# sourceMappingURL=RepositoryCompareCommitsPage.js.map