'use strict';

exports.__esModule = true;
exports.buildUrl = exports.calculateDuration = exports.formatTime = exports.getRandomKey = undefined;

var _moment = require('moment');

var _moment2 = _interopRequireDefault(_moment);

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

/** The following helper function was implemented following the same logic/strucure
 *  as react-add-to-calendar helpers class.
 *  (https://github.com/jasonsalzman/react-add-to-calendar/blob/master/src/helpers/index.js)
*/

var getRandomKey = exports.getRandomKey = function getRandomKey() {
  var n = Math.floor(Math.random() * 999999999999).toString();
  return new Date().getTime().toString() + '_' + n;
};

var formatTime = exports.formatTime = function formatTime(date) {
  var formattedDate = _moment2.default.utc(date).format('YYYYMMDDTHHmmssZ');
  return formattedDate.replace('+00:00', 'Z');
};

var calculateDuration = exports.calculateDuration = function calculateDuration(startTime, endTime) {
  // snag parameters and format properly in UTC
  var end = _moment2.default.utc(endTime).format('DD/MM/YYYY HH:mm:ss');
  var start = _moment2.default.utc(startTime).format('DD/MM/YYYY HH:mm:ss');

  // calculate the difference in milliseconds between the start and end times
  var difference = (0, _moment2.default)(end, 'DD/MM/YYYY HH:mm:ss').diff((0, _moment2.default)(start, 'DD/MM/YYYY HH:mm:ss'));

  // convert difference from above to a proper momentJs duration object
  var duration = _moment2.default.duration(difference);

  return Math.floor(duration.asHours()) + _moment2.default.utc(difference).format(':mm');
};

var buildUrl = exports.buildUrl = function buildUrl(event, type, window) {
  var calendarUrl = '';

  // allow mobile browsers to open the gmail data URI within native calendar app
  // type = (type == "google" && this.isMobile()) ? "outlook" : type;

  switch (type) {
    case 'google':
      calendarUrl = 'https://calendar.google.com/calendar/render';
      calendarUrl += '?action=TEMPLATE';
      calendarUrl += '&dates=' + formatTime(event.startDate);
      calendarUrl += '/' + formatTime(event.endDate);
      calendarUrl += '&location=' + encodeURIComponent(event.location);
      calendarUrl += '&text=' + encodeURIComponent(event.title);
      calendarUrl += '&details=' + encodeURIComponent(event.description);
      break;

    case 'yahoo':
      {
        // yahoo doesn't utilize endTime so we need to calulate duration
        var duration = calculateDuration(event.startDate, event.endDate);
        calendarUrl = 'https://calendar.yahoo.com/?v=60&view=d&type=20';
        calendarUrl += '&title=' + encodeURIComponent(event.title);
        calendarUrl += '&st=' + formatTime(event.startDate);
        calendarUrl += '&dur=' + duration;
        calendarUrl += '&desc=' + encodeURIComponent(event.description);
        calendarUrl += '&in_loc=' + encodeURIComponent(event.location);
        break;
      }

    case 'outlookcom':
      calendarUrl = 'https://outlook.live.com/owa/?rru=addevent';
      calendarUrl += '&startdt=' + formatTime(event.startDate);
      calendarUrl += '&enddt=' + formatTime(event.endDate);
      calendarUrl += '&subject=' + encodeURIComponent(event.title);
      calendarUrl += '&location=' + encodeURIComponent(event.location);
      calendarUrl += '&body=' + encodeURIComponent(event.description);
      calendarUrl += '&allday=false';
      calendarUrl += '&uid=' + getRandomKey();
      calendarUrl += '&path=/calendar/view/Month';
      break;

    default:
      calendarUrl = ['BEGIN:VCALENDAR', 'VERSION:2.0', 'BEGIN:VEVENT', 'URL:' + document.URL, 'DTSTART:' + formatTime(event.startDate), 'DTEND:' + formatTime(event.endDate), 'SUMMARY:' + event.title, 'DESCRIPTION:' + event.description, 'LOCATION:' + event.location, 'END:VEVENT', 'END:VCALENDAR'].join('\n');
      if (window) {
        calendarUrl = window.URL.createObjectURL(new Blob([calendarUrl], { type: 'text/calendar;charset=utf-8' }));
      }
  }

  return calendarUrl;
};