import copy from 'copy-to-clipboard';
import ContentCopyIcon from 'mdi-react/ContentCopyIcon';
import DotsHorizontalIcon from 'mdi-react/DotsHorizontalIcon';
import FileDocumentIcon from 'mdi-react/FileDocumentIcon';
import * as React from 'react';
import { Link } from 'react-router-dom';
import { Timestamp } from '../../components/time/Timestamp';
import { Tooltip } from '../../components/tooltip/Tooltip';
import { eventLogger } from '../../tracking/eventLogger';
import { UserAvatar } from '../../user/UserAvatar';
import { pluralize } from '../../util/strings';
const GitCommitNodeByline = ({ author, committer, className, compact }) => {
    if (committer &&
        committer.person.email !== author.person.email &&
        ((!committer.person.name && !author.person.name) || committer.person.name !== author.person.name)) {
        // The author and committer both exist and are different people.
        return (React.createElement("small", { className: `git-commit-node-byline git-commit-node-byline--has-committer ${className}` },
            React.createElement(UserAvatar, { className: "icon-inline", user: author.person, tooltip: `${author.person.displayName} (author)` }),
            ' ',
            React.createElement(UserAvatar, { className: "icon-inline mr-1", user: committer.person, tooltip: `${committer.person.displayName} (committer)` }),
            ' ',
            React.createElement("strong", null, author.person.displayName),
            " ",
            !compact && 'authored',
            " and",
            ' ',
            React.createElement("strong", null, committer.person.displayName),
            ' ',
            !compact && (React.createElement(React.Fragment, null,
                "committed ",
                React.createElement(Timestamp, { date: committer.date })))));
    }
    return (React.createElement("small", { className: `git-commit-node-byline git-commit-node-byline--no-committer ${className}` },
        React.createElement(UserAvatar, { className: "icon-inline mr-1", user: author.person, tooltip: author.person.displayName }),
        ' ',
        React.createElement("strong", null, author.person.displayName),
        ' ',
        !compact && (React.createElement(React.Fragment, null,
            "committed ",
            React.createElement(Timestamp, { date: author.date })))));
};
/** Displays a Git commit. */
export class GitCommitNode extends React.PureComponent {
    constructor() {
        super(...arguments);
        this.state = {
            showCommitMessageBody: false,
            flashCopiedToClipboardMessage: false,
        };
        this.toggleShowCommitMessageBody = () => {
            eventLogger.log('CommitBodyToggled');
            this.setState(prevState => ({ showCommitMessageBody: !prevState.showCommitMessageBody }));
        };
        this.copyToClipboard = () => {
            eventLogger.log('CommitSHACopiedToClipboard');
            copy(this.props.node.oid);
            this.setState({ flashCopiedToClipboardMessage: true }, () => {
                Tooltip.forceUpdate();
                setTimeout(() => {
                    this.setState({ flashCopiedToClipboardMessage: false }, () => Tooltip.forceUpdate());
                }, 1500);
            });
        };
    }
    render() {
        const bylineElement = (React.createElement(GitCommitNodeByline, { className: "text-muted git-commit-node__byline", author: this.props.node.author, committer: this.props.node.committer, compact: Boolean(this.props.compact) }));
        const messageElement = (React.createElement("div", { className: "git-commit-node__message" },
            React.createElement(Link, { to: this.props.node.canonicalURL, className: "git-commit-node__message-subject", title: this.props.node.message }, this.props.node.subject),
            this.props.node.body &&
                !this.props.expandCommitMessageBody && (React.createElement("button", { type: "button", className: "btn btn-secondary btn-sm git-commit-node__message-toggle", onClick: this.toggleShowCommitMessageBody },
                React.createElement(DotsHorizontalIcon, { className: "icon-inline" }))),
            this.props.compact && (React.createElement("small", { className: "text-muted git-commit-node__message-timestamp" },
                React.createElement(Timestamp, { noAbout: true, date: this.props.node.committer ? this.props.node.committer.date : this.props.node.author.date })))));
        const oidElement = React.createElement("code", { className: "git-commit-node__oid" }, this.props.node.abbreviatedOID);
        return (React.createElement("div", { key: this.props.node.id, className: `git-commit-node ${this.props.compact ? 'git-commit-node--compact' : ''} ${this.props
                .className || ''}` },
            React.createElement("div", { className: "git-commit-node__row git-commit-node__main" }, !this.props.compact ? (React.createElement(React.Fragment, null,
                React.createElement("div", { className: "git-commit-node__signature" },
                    messageElement,
                    bylineElement),
                React.createElement("div", { className: "git-commit-node__actions" },
                    !this.props.showSHAAndParentsRow && (React.createElement("div", { className: "btn-group btn-group-sm mr-2", role: "group" },
                        React.createElement(Link, { className: "btn btn-secondary", to: this.props.node.canonicalURL, "data-tooltip": "View this commit" },
                            React.createElement("strong", null, oidElement)),
                        React.createElement("button", { type: "button", className: "btn btn-secondary", onClick: this.copyToClipboard, "data-tooltip": this.state.flashCopiedToClipboardMessage ? 'Copied!' : 'Copy full SHA' },
                            React.createElement(ContentCopyIcon, { className: "icon-inline" })))),
                    this.props.node.tree && (React.createElement(Link, { className: "btn btn-secondary btn-sm", to: this.props.node.tree.canonicalURL, "data-tooltip": "View files at this commit" },
                        React.createElement(FileDocumentIcon, { className: "icon-inline" })))))) : (React.createElement(React.Fragment, null,
                bylineElement,
                messageElement,
                React.createElement(Link, { to: this.props.node.canonicalURL }, oidElement)))),
            (this.props.expandCommitMessageBody || this.state.showCommitMessageBody) && (React.createElement("div", { className: "git-commit-node__row" },
                React.createElement("pre", { className: "git-commit-node__message-body" }, this.props.node.body))),
            this.props.showSHAAndParentsRow && (React.createElement("div", { className: "git-commit-node__row git-commit-node__sha-and-parents" },
                React.createElement("code", { className: "git-ref-tag-2 git-commit-node__sha-and-parents-sha" },
                    this.props.node.oid,
                    ' ',
                    React.createElement("button", { type: "button", className: "btn btn-icon git-commit-node__sha-and-parents-copy", onClick: this.copyToClipboard, "data-tooltip": this.state.flashCopiedToClipboardMessage ? 'Copied!' : 'Copy full SHA' },
                        React.createElement(ContentCopyIcon, { className: "icon-inline" }))),
                React.createElement("div", { className: "git-commit-node__sha-and-parents-parents" }, this.props.node.parents.length > 0 ? (React.createElement(React.Fragment, null,
                    React.createElement("span", { className: "git-commit-node__sha-and-parents-label" },
                        this.props.node.parents.length === 1
                            ? 'Parent'
                            : `${this.props.node.parents.length} ${pluralize('parent', this.props.node.parents.length)}`,
                        ":"),
                    ' ',
                    this.props.node.parents.map((parent, i) => (React.createElement(Link, { key: i, className: "git-ref-tag-2 git-commit-node__sha-and-parents-parent", to: `/${this.props.repoName}/-/commit/${parent.oid}` },
                        React.createElement("code", null, parent.abbreviatedOID)))))) : ('(root commit)'))))));
    }
}
//# sourceMappingURL=GitCommitNode.js.map