'use strict';

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _https = require('https');

var _https2 = _interopRequireDefault(_https);

var _querystring = require('querystring');

var _querystring2 = _interopRequireDefault(_querystring);

var _parseArgs2 = require('./parseArgs');

var _parseArgs3 = _interopRequireDefault(_parseArgs2);

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

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

var IFTTTMaker = function () {
  function IFTTTMaker(apiKey) {
    _classCallCheck(this, IFTTTMaker);

    this.apiKey = apiKey;
  }

  _createClass(IFTTTMaker, [{
    key: 'send',
    value: function send() {
      var _this = this;

      var _parseArgs = _parseArgs3.default.apply(undefined, arguments);

      var event = _parseArgs.event;
      var values = _parseArgs.values;
      var callback = _parseArgs.callback;


      return new Promise(function (resolve, reject) {
        var url = 'https://maker.ifttt.com';
        url += '/trigger/' + _querystring2.default.escape(event) + '/with/key/' + _querystring2.default.escape(_this.apiKey);
        url += '?' + _querystring2.default.stringify(values);

        _https2.default.get(url, function (_ref) {
          var statusCode = _ref.statusCode;

          if (!statusCode || statusCode !== 200) {
            var err = 'Bad response code: ' + statusCode;
            reject(err);
            callback(err);
          } else {
            resolve();
            callback(false);
          }
        }).on('error', function (err) {
          reject(err);
          callback(err);
        });
      });
    }
  }]);

  return IFTTTMaker;
}();

module.exports = function (apiKey) {
  return new IFTTTMaker(apiKey);
};