'use strict';

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

var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of');

var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf);

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');

var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);

var _inherits2 = require('babel-runtime/helpers/inherits');

var _inherits3 = _interopRequireDefault(_inherits2);

require('source-map-support/register');

var _requireDir = require('require-dir');

var _requireDir2 = _interopRequireDefault(_requireDir);

var _Plugin2 = require('./Plugin');

var _formatJson = require('format-json');

var _formatJson2 = _interopRequireDefault(_formatJson);

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

var EventHandler = exports.EventHandler = function (_Plugin) {
    (0, _inherits3.default)(EventHandler, _Plugin);

    function EventHandler(eventData, log) {
        (0, _classCallCheck3.default)(this, EventHandler);

        var _this = (0, _possibleConstructorReturn3.default)(this, (EventHandler.__proto__ || (0, _getPrototypeOf2.default)(EventHandler)).call(this));

        _this.log = log;

        log.debug('incoming event: ' + _formatJson2.default.plain(eventData));

        _this.eventId = eventData.ghEventId;
        _this.eventType = eventData.ghEventType;
        _this.taskConfig = eventData.ghTaskConfig;
        _this.eventData = eventData;
        _this.MessageHandle = eventData.MessageHandle;

        // Handle Older Task Format
        try {
            _this.tasks = _this.taskConfig[_this.eventType].tasks;
        } catch (error) {
            log.error('No Tasks Defined for Event Type ' + _this.eventType);
            log.debug(error);
            _this.tasks = {};
        }

        log.info('----------------------------');
        log.info('New Event Received');
        log.info('Event ID: ' + _this.eventId);
        log.info('Event Type: ' + _this.eventType);
        log.info('Tasks: ' + _formatJson2.default.plain(_this.tasks));
        log.debug(eventData);
        return _this;
    }

    return EventHandler;
}(_Plugin2.Plugin);

(0, _requireDir2.default)('../handlers');