import * as React from 'react';
import { map } from 'rxjs/operators';
import { gql, queryGraphQL } from '../../backend/graphql';
import { createAggregateError } from '../../util/errors';
import { FileDiffConnection } from './FileDiffConnection';
import { FileDiffNode } from './FileDiffNode';
export function queryRepositoryComparisonFileDiffs(args) {
    return queryGraphQL(gql `
            query RepositoryComparisonDiff($repo: ID!, $base: String, $head: String, $first: Int) {
                node(id: $repo) {
                    ... on Repository {
                        comparison(base: $base, head: $head) {
                            fileDiffs(first: $first) {
                                nodes {
                                    ...FileDiffFields
                                }
                                totalCount
                                pageInfo {
                                    hasNextPage
                                }
                                diffStat {
                                    ...DiffStatFields
                                }
                            }
                        }
                    }
                }
            }

            fragment FileDiffFields on FileDiff {
                oldPath
                newPath
                mostRelevantFile {
                    url
                }
                hunks {
                    oldRange {
                        ...FileDiffHunkRangeFields
                    }
                    oldNoNewlineAt
                    newRange {
                        ...FileDiffHunkRangeFields
                    }
                    section
                    body
                }
                stat {
                    ...DiffStatFields
                }
                internalID
            }

            fragment FileDiffHunkRangeFields on FileDiffHunkRange {
                startLine
                lines
            }

            fragment DiffStatFields on DiffStat {
                added
                changed
                deleted
            }
        `, args).pipe(map(({ data, errors }) => {
        if (!data || !data.node) {
            throw createAggregateError(errors);
        }
        const repo = data.node;
        if (!repo.comparison || !repo.comparison.fileDiffs || errors) {
            throw createAggregateError(errors);
        }
        return repo.comparison.fileDiffs;
    }));
}
/** A page with the file diffs in the comparison. */
export class RepositoryCompareDiffPage extends React.PureComponent {
    constructor() {
        super(...arguments);
        this.queryDiffs = (args) => queryRepositoryComparisonFileDiffs(Object.assign({}, args, { repo: this.props.repo.id, base: this.props.base.commitID, head: this.props.head.commitID }));
    }
    render() {
        return (React.createElement("div", { className: "repository-compare-page" },
            React.createElement(FileDiffConnection, { listClassName: "list-group list-group-flush", noun: "changed file", pluralNoun: "changed files", queryConnection: this.queryDiffs, nodeComponent: FileDiffNode, nodeComponentProps: {
                    base: Object.assign({}, this.props.base, { rev: this.props.base.rev || 'HEAD' }),
                    head: Object.assign({}, this.props.head, { rev: this.props.head.rev || 'HEAD' }),
                    lineNumbers: true,
                    extensions: this.props.extensions,
                    location: this.props.location,
                    history: this.props.history,
                    hoverifier: this.props.hoverifier,
                    extensionsController: this.props.extensionsController,
                }, defaultFirst: 25, hideSearch: true, noSummaryIfAllNodesVisible: true, history: this.props.history, location: this.props.location, extensionsOnVisibleTextDocumentsChange: this.props.extensionsOnVisibleTextDocumentsChange })));
    }
}
//# sourceMappingURL=RepositoryCompareDiffPage.js.map