'use strict';

var _get = require('babel-runtime/helpers/get')['default'];

var _inherits = require('babel-runtime/helpers/inherits')['default'];

var _classCallCheck = require('babel-runtime/helpers/class-call-check')['default'];

var _interopRequireDefault = require('babel-runtime/helpers/interop-require-default')['default'];

Object.defineProperty(exports, '__esModule', {
  value: true
});

var _util = require('util');

var _util2 = _interopRequireDefault(_util);

var _uuidV4 = require('uuid-v4');

var _uuidV42 = _interopRequireDefault(_uuidV4);

var _middlewareCsError = require('../../middleware/csError');

var _middlewareCsError2 = _interopRequireDefault(_middlewareCsError);

var _getProperties2 = require('./getProperties');

var _getProperties3 = _interopRequireDefault(_getProperties2);

var _branchNameParse = require('./branchNameParse');

var _branchNameParse2 = _interopRequireDefault(_branchNameParse);

//TODO: do we want this kind of library to know about status codes?

var DeveoCommitMalformedError = (function (_CSError) {
  _inherits(DeveoCommitMalformedError, _CSError);

  function DeveoCommitMalformedError(error, pushEvent) {
    _classCallCheck(this, DeveoCommitMalformedError);

    _get(Object.getPrototypeOf(DeveoCommitMalformedError.prototype), 'constructor', this).call(this, ['There was an unexpected error when processing your Deveo push event.']);
    this.originalError = error;
    this.pushEvent = pushEvent;
  }

  return DeveoCommitMalformedError;
})(_middlewareCsError2['default']);

var deveoTranslator = {
  family: 'Deveo',
  translatePush: function translatePush(pushEvent, instanceId, digestId, inboxId) {
    try {
      var _ret = (function () {
        var branch = (0, _branchNameParse2['default'])(pushEvent.ref);
        var repository = {
          id: pushEvent.repository.uuid,
          name: pushEvent.project.name + '/' + pushEvent.repository.name
        };
        var project = {
          name: pushEvent.project.name,
          url: pushEvent.project.url
        };

        return {
          v: pushEvent.commits.map(function (aCommit) {
            var commit = {
              sha: aCommit.id,
              commit: {
                author: aCommit.author.name,
                committer: {
                  name: aCommit.author.name,
                  email: aCommit.author.email,
                  date: aCommit.timestamp
                },
                message: aCommit.message
              },
              html_url: aCommit.url,
              repository: repository,
              branch: branch,
              project: project,
              originalMessage: aCommit
            };
            return {
              eventId: (0, _uuidV42['default'])(),
              eventType: 'DeveoCommitReceived',
              data: commit,
              metadata: {
                instanceId: instanceId,
                digestId: digestId,
                inboxId: inboxId
              }
            };
          })
        };
      })();

      if (typeof _ret === 'object') return _ret.v;
    } catch (ex) {
      var otherEx = new DeveoCommitMalformedError(ex, pushEvent);
      //console.log(otherEx, otherEx.originalError.stack);
      throw otherEx;
    }
  },
  canTranslate: function canTranslate(request) {
    var headers = request.headers;
    return headers.hasOwnProperty('x-deveo-event') && headers['x-deveo-event'] === 'push';
  },
  getProperties: function getProperties(event) {
    var props = {
      repo: event.project.name + '/' + event.repository.name,
      branchHref: event.project.url + '/repositories/' + event.repository.name + '/tree/' + branch,
      repoHref: event.repository.url,
    };

    return props;
  }
};

exports['default'] = deveoTranslator;
module.exports = exports['default'];

@@ -1862,166 +1862,23 @@
 ent.
-project.name + '/' + pushEvent.repository.name%0A        %7D;%0A        var project = %7B%0A          name: pushEvent.project.name,%0A          url: pushEvent.project.url
+repository.name
 %0A   
@@ -2435,40 +2435,8 @@
 ch,%0A
-              project: project,%0A
     
@@ -3304,115 +3304,621 @@
 var 
-props = %7B%0A      repo: event.project.name + '/' + event.repository.
+commit = event.commit;%0A    var branch = event.branch;%0A    var html_url = event.html_url;%0A    var props = %7B%0A      repo: '',%0A      branchHref: '',%0A      repoHref: ''%0A    %7D;%0A%0A    var urlParts = html_url.match(/.+%5C/(.+)%5C/projects%5C/(.+)%5C/repositories%5C/(.+)%5C/.+%5C/.+/g);%0A    var serverUrl = html_url.match(/(http.?:)%5C/%5C/(.*?)%5C//);%0A%0A    if (urlParts !== null && serverUrl !== null) %7B%0A      var company_name = urlParts%5B0%5D%0A      var project_name = urlParts%5B1%5D;%0A      var repo_name = urlParts%5B2%5D;%0A      props.repo = project_name + '/' + repo_
 name
-,
 %0A      
-branchHref: event.
+props.repoHref = serverUrl + company_name + '/projects/' + 
 project
-.url
+_name
  + '
@@ -3939,29 +3939,57 @@
 ' + 
-event.repository.name
+repo_name%0A      props.branchHref = props.repoHref
  + '
@@ -4008,54 +4008,102 @@
 anch
-,
+%0A
 %0A    
-  repoHref: event.repository.url,
+%7D else %7B%0A      throw 'Could not parse DeveoCommitReceived event props correctly.';
 %0A    %7D
-;
 %0A%0A  

Use regexp to parse props.