'use strict';

var _regenerator = require('babel-runtime/regenerator');

var _regenerator2 = _interopRequireDefault(_regenerator);

var _stringify = require('babel-runtime/core-js/json/stringify');

var _stringify2 = _interopRequireDefault(_stringify);

var _asyncToGenerator2 = require('babel-runtime/helpers/asyncToGenerator');

var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2);

var handle = function () {
    var _ref = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee(event, context, callback) {
        var _consumer;

        return _regenerator2.default.wrap(function _callee$(_context) {
            while (1) {
                switch (_context.prev = _context.next) {
                    case 0:
                        /* eslint-disable */
                        console.log('---------------------------------');
                        console.log('Github-Results: ');
                        console.log('---------------------------------');
                        console.log('Payload', json.plain(event));
                        /* eslint-enable */

                        _context.prev = 4;
                        _context.next = 7;
                        return getQueue();

                    case 7:
                        _consumer = _context.sent;


                        _consumer.on('error', function (err) {
                            console.log(err.message);
                            throw err;
                        });

                        _consumer.on('empty', function () {
                            console.log('results queue is empty.');
                            _consumer.stop();
                            var response = {
                                statusCode: 200,
                                body: (0, _stringify2.default)({
                                    input: event
                                })
                            };
                            return callback(null, response);
                        });

                        _consumer.start();
                        _context.next = 17;
                        break;

                    case 13:
                        _context.prev = 13;
                        _context.t0 = _context['catch'](4);

                        console.log(_context.t0.message);
                        return _context.abrupt('return', callback(null, {
                            statusCode: 401,
                            headers: { 'Content-Type': 'text/plain' },
                            body: _context.t0.message
                        }));

                    case 17:
                    case 'end':
                        return _context.stop();
                }
            }
        }, _callee, this, [[4, 13]]);
    }));

    return function handle(_x, _x2, _x3) {
        return _ref.apply(this, arguments);
    };
}();

var getQueue = function () {
    var _ref2 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee2() {
        var awsOptions;
        return _regenerator2.default.wrap(function _callee2$(_context2) {
            while (1) {
                switch (_context2.prev = _context2.next) {
                    case 0:
                        awsOptions = {
                            queueUrl: process.env.SQS_RESULTS_QUEUE_URL,
                            waitTimeSeconds: 10,
                            handleMessage: githubUtils.handleEventTaskResult
                        };


                        if (process.env.AWS_PROXY) {
                            AWS.config.update({
                                httpOptions: {
                                    agent: proxy(process.env.AWS_PROXY)
                                }
                            });
                        }

                        _context2.next = 4;
                        return consumer.create(awsOptions);

                    case 4:
                        return _context2.abrupt('return', _context2.sent);

                    case 5:
                    case 'end':
                        return _context2.stop();
                }
            }
        }, _callee2, this);
    }));

    return function getQueue() {
        return _ref2.apply(this, arguments);
    };
}();

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

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

require('source-map-support').install();
var json = require('format-json');
var consumer = require('sqs-consumer');
var githubUtils = require('../gtmGithubUtils.js');
var proxy = require('proxy-agent');
var AWS = require('aws-sdk');

AWS.config.update({ region: process.env.GTM_AWS_REGION });

module.exports = {
    handle: handle,
    getQueue: getQueue
};