import * as React from 'react';
import { Link } from 'react-router-dom';
import { map } from 'rxjs/operators';
import { gql, queryGraphQL } from '../backend/graphql';
import { LinkOrSpan } from '../components/LinkOrSpan';
import { Timestamp } from '../components/time/Timestamp';
import { createAggregateError } from '../util/errors';
import { memoizeObservable } from '../util/memoize';
import { numberWithCommas } from '../util/strings';
export const GitRefNode = ({ node, url, rootIsLink, children }) => {
    const mostRecentSig = node.target.commit &&
        (node.target.commit.committer && node.target.commit.committer.date > node.target.commit.author.date
            ? node.target.commit.committer
            : node.target.commit.author);
    const behindAhead = node.target.commit && node.target.commit.behindAhead;
    url = url !== undefined ? url : node.url;
    return (React.createElement(LinkOrSpan, { key: node.id, className: "git-ref-node list-group-item", to: url },
        React.createElement("span", null,
            rootIsLink ? (React.createElement("code", { className: "git-ref-tag-2" }, node.displayName)) : (React.createElement(Link, { className: "git-ref-tag-2", to: url },
                React.createElement("code", null, node.displayName))),
            mostRecentSig && (React.createElement("small", { className: "text-muted pl-2" },
                "Updated ",
                React.createElement(Timestamp, { date: mostRecentSig.date }),
                ' ',
                mostRecentSig.person && React.createElement(React.Fragment, null,
                    "by ",
                    mostRecentSig.person.displayName)))),
        behindAhead && (React.createElement("small", { className: "text-muted" },
            numberWithCommas(behindAhead.behind),
            " behind, ",
            numberWithCommas(behindAhead.ahead),
            " ahead")),
        children));
};
export const gitRefFragment = gql `
    fragment GitRefFields on GitRef {
        id
        displayName
        name
        abbrevName
        url
        target {
            commit {
                author {
                    ...SignatureFields
                }
                committer {
                    ...SignatureFields
                }
                behindAhead(revspec: "HEAD") @include(if: $withBehindAhead) {
                    behind
                    ahead
                }
            }
        }
    }

    fragment SignatureFields on Signature {
        person {
            displayName
        }
        date
    }
`;
export const queryGitRefs = memoizeObservable((args) => queryGraphQL(gql `
                query RepositoryGitRefs(
                    $repo: ID!
                    $first: Int
                    $query: String
                    $type: GitRefType!
                    $withBehindAhead: Boolean!
                ) {
                    node(id: $repo) {
                        ... on Repository {
                            gitRefs(first: $first, query: $query, type: $type, orderBy: AUTHORED_OR_COMMITTED_AT) {
                                nodes {
                                    ...GitRefFields
                                }
                                totalCount
                                pageInfo {
                                    hasNextPage
                                }
                            }
                        }
                    }
                }
                ${gitRefFragment}
            `, Object.assign({}, args, { withBehindAhead: args.withBehindAhead !== undefined ? args.withBehindAhead : args.type === "GIT_BRANCH" /* GIT_BRANCH */ })).pipe(map(({ data, errors }) => {
    if (!data || !data.node || !data.node.gitRefs) {
        throw createAggregateError(errors);
    }
    return data.node.gitRefs;
})), args => `${args.repo}:${args.first}:${args.query}:${args.type}`);
//# sourceMappingURL=GitRef.js.map