'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.IssueishTimeline = undefined;

var _class, _temp;

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _reactRelay = require('react-relay');

var _reactRelay2 = _interopRequireDefault(_reactRelay);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

var _commitsContainer = require('./timeline-items/commits-container.js');

var _commitsContainer2 = _interopRequireDefault(_commitsContainer);

var _issueCommentContainer = require('./timeline-items/issue-comment-container.js');

var _issueCommentContainer2 = _interopRequireDefault(_issueCommentContainer);

var _mergedEventContainer = require('./timeline-items/merged-event-container.js');

var _mergedEventContainer2 = _interopRequireDefault(_mergedEventContainer);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

const groupableTimelineItems = {
  Commit: _commitsContainer2.default
};

const nonGroupableTimelineItems = {
  IssueComment: _issueCommentContainer2.default,
  MergedEvent: _mergedEventContainer2.default
};

let IssueishTimeline = exports.IssueishTimeline = (_temp = _class = class IssueishTimeline extends _react2.default.Component {

  render() {
    const issueish = this.props.issue || this.props.pullRequest;
    const groupedEdges = this.groupEdges(issueish.timeline.edges);
    return _react2.default.createElement(
      'div',
      { className: 'github-PrTimeline' },
      groupedEdges.map((_ref) => {
        let type = _ref.type,
            edges = _ref.edges;

        if (groupableTimelineItems[type]) {
          return this.renderGroupableTimelineEvents(type, edges);
        } else if (nonGroupableTimelineItems[type]) {
          return this.renderNonGroupableTimelineEvents(type, edges);
        } else {
          // eslint-disable-next-line no-console
          console.warn(`unrecogized timeline event type: ${type}`);
          return null;
        }
      })
    );
  }

  renderGroupableTimelineEvents(type, edges) {
    const Component = groupableTimelineItems[type];
    return _react2.default.createElement(Component, {
      key: `${type}-${edges[0].cursor}`,
      nodes: edges.map(e => e.node),
      switchToIssueish: this.props.switchToIssueish
    });
  }

  renderNonGroupableTimelineEvents(type, edges) {
    const Component = nonGroupableTimelineItems[type];
    return edges.map((_ref2) => {
      let node = _ref2.node,
          cursor = _ref2.cursor;

      return _react2.default.createElement(Component, { key: `${type}-${cursor}`, item: node, switchToIssueish: this.props.switchToIssueish });
    });
  }

  groupEdges(edges) {
    let currentGroup;
    const groupedEdges = [];
    let lastEdgeType;
    edges.forEach((_ref3) => {
      let node = _ref3.node,
          cursor = _ref3.cursor;

      const currentEdgeType = node.__typename;
      if (currentEdgeType === lastEdgeType) {
        currentGroup.edges.push({ node, cursor });
      } else {
        currentGroup = {
          type: currentEdgeType,
          edges: [{ node, cursor }]
        };
        groupedEdges.push(currentGroup);
      }
      lastEdgeType = currentEdgeType;
    });
    return groupedEdges;
  }
}, _class.propTypes = {
  switchToIssueish: _propTypes2.default.func.isRequired,
  issue: _propTypes2.default.shape({
    timeline: _propTypes2.default.object.isRequired
  }),
  pullRequest: _propTypes2.default.shape({
    timeline: _propTypes2.default.object.isRequired
  })
}, _temp);
exports.default = _reactRelay2.default.createContainer(IssueishTimeline, {
  fragments: {
    issue: () => function (RQL_0, RQL_1) {
      return {
        children: [{
          calls: [{
            kind: 'Call',
            metadata: {},
            name: 'first',
            value: {
              kind: 'CallValue',
              callValue: 100
            }
          }],
          children: [{
            children: [{
              fieldName: 'cursor',
              kind: 'Field',
              metadata: {
                isRequisite: true
              },
              type: 'String'
            }, {
              children: [].concat.apply([], [{
                fieldName: '__typename',
                kind: 'Field',
                metadata: {
                  isRequisite: true
                },
                type: 'String'
              }, _reactRelay2.default.QL.__frag(RQL_0), _reactRelay2.default.QL.__frag(RQL_1), {
                children: [{
                  fieldName: 'id',
                  kind: 'Field',
                  metadata: {
                    isGenerated: true,
                    isRequisite: true
                  },
                  type: 'ID'
                }, {
                  fieldName: '__typename',
                  kind: 'Field',
                  metadata: {
                    isGenerated: true,
                    isRequisite: true
                  },
                  type: 'String'
                }],
                id: _reactRelay2.default.QL.__id(),
                kind: 'Fragment',
                metadata: {
                  isAbstract: true
                },
                name: 'IdFragment',
                type: 'Node'
              }]),
              fieldName: 'node',
              kind: 'Field',
              metadata: {
                canHaveSubselections: true,
                inferredRootCallName: 'node',
                inferredPrimaryKey: 'id',
                isAbstract: true,
                isRequisite: true
              },
              type: 'IssueTimelineItem'
            }],
            fieldName: 'edges',
            kind: 'Field',
            metadata: {
              canHaveSubselections: true,
              isPlural: true
            },
            type: 'IssueTimelineItemEdge'
          }, {
            children: [{
              fieldName: 'hasNextPage',
              kind: 'Field',
              metadata: {
                isGenerated: true,
                isRequisite: true
              },
              type: 'Boolean'
            }, {
              fieldName: 'hasPreviousPage',
              kind: 'Field',
              metadata: {
                isGenerated: true,
                isRequisite: true
              },
              type: 'Boolean'
            }],
            fieldName: 'pageInfo',
            kind: 'Field',
            metadata: {
              canHaveSubselections: true,
              isGenerated: true,
              isRequisite: true
            },
            type: 'PageInfo'
          }],
          fieldName: 'timeline',
          kind: 'Field',
          metadata: {
            canHaveSubselections: true,
            isConnection: true
          },
          type: 'IssueTimelineConnection'
        }, {
          fieldName: 'id',
          kind: 'Field',
          metadata: {
            isGenerated: true,
            isRequisite: true
          },
          type: 'ID'
        }],
        id: _reactRelay2.default.QL.__id(),
        kind: 'Fragment',
        metadata: {},
        name: 'Issueish_IssueRelayQL',
        type: 'Issue'
      };
    }(groupableTimelineItems.Commit.getFragment('nodes'), nonGroupableTimelineItems.IssueComment.getFragment('item')),
    pullRequest: () => function (RQL_0, RQL_1, RQL_2) {
      return {
        children: [{
          calls: [{
            kind: 'Call',
            metadata: {},
            name: 'first',
            value: {
              kind: 'CallValue',
              callValue: 100
            }
          }],
          children: [{
            children: [{
              fieldName: 'cursor',
              kind: 'Field',
              metadata: {
                isRequisite: true
              },
              type: 'String'
            }, {
              children: [].concat.apply([], [{
                fieldName: '__typename',
                kind: 'Field',
                metadata: {
                  isRequisite: true
                },
                type: 'String'
              }, _reactRelay2.default.QL.__frag(RQL_0), _reactRelay2.default.QL.__frag(RQL_1), _reactRelay2.default.QL.__frag(RQL_2), {
                children: [{
                  fieldName: 'id',
                  kind: 'Field',
                  metadata: {
                    isGenerated: true,
                    isRequisite: true
                  },
                  type: 'ID'
                }, {
                  fieldName: '__typename',
                  kind: 'Field',
                  metadata: {
                    isGenerated: true,
                    isRequisite: true
                  },
                  type: 'String'
                }],
                id: _reactRelay2.default.QL.__id(),
                kind: 'Fragment',
                metadata: {
                  isAbstract: true
                },
                name: 'IdFragment',
                type: 'Node'
              }]),
              fieldName: 'node',
              kind: 'Field',
              metadata: {
                canHaveSubselections: true,
                inferredRootCallName: 'node',
                inferredPrimaryKey: 'id',
                isAbstract: true,
                isRequisite: true
              },
              type: 'PullRequestTimelineItem'
            }],
            fieldName: 'edges',
            kind: 'Field',
            metadata: {
              canHaveSubselections: true,
              isPlural: true
            },
            type: 'PullRequestTimelineItemEdge'
          }, {
            children: [{
              fieldName: 'hasNextPage',
              kind: 'Field',
              metadata: {
                isGenerated: true,
                isRequisite: true
              },
              type: 'Boolean'
            }, {
              fieldName: 'hasPreviousPage',
              kind: 'Field',
              metadata: {
                isGenerated: true,
                isRequisite: true
              },
              type: 'Boolean'
            }],
            fieldName: 'pageInfo',
            kind: 'Field',
            metadata: {
              canHaveSubselections: true,
              isGenerated: true,
              isRequisite: true
            },
            type: 'PageInfo'
          }],
          fieldName: 'timeline',
          kind: 'Field',
          metadata: {
            canHaveSubselections: true,
            isConnection: true
          },
          type: 'PullRequestTimelineConnection'
        }, {
          fieldName: 'id',
          kind: 'Field',
          metadata: {
            isGenerated: true,
            isRequisite: true
          },
          type: 'ID'
        }],
        id: _reactRelay2.default.QL.__id(),
        kind: 'Fragment',
        metadata: {},
        name: 'Issueish_PullRequestRelayQL',
        type: 'PullRequest'
      };
    }(groupableTimelineItems.Commit.getFragment('nodes'), nonGroupableTimelineItems.IssueComment.getFragment('item'), nonGroupableTimelineItems.MergedEvent.getFragment('item'))
  }
});
//# sourceMappingURL=data:application/json;charset=utf-8;base64, 