"use strict";

var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault");

var _Object$defineProperty = require("@babel/runtime-corejs3/core-js-stable/object/define-property");

require("core-js/modules/es.function.name");

require("core-js/modules/es.regexp.exec");

require("core-js/modules/es.string.match");

require("core-js/modules/es.string.split");

_Object$defineProperty(exports, "__esModule", {
  value: true
});

exports.isObj = isObj;
exports.isImageAttachment = isImageAttachment;
exports.getLastImage = getLastImage;
exports.defaultGetPreset = defaultGetPreset;
exports.leftPad = leftPad;
exports.getDateString = getDateString;
exports.parseDateString = parseDateString;

var _isNan = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/number/is-nan"));

var _concat = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/concat"));

var _isArray = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/array/is-array"));

var _filter = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/filter"));

var _typeof2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/typeof"));

var _mime = _interopRequireDefault(require("mime"));

var _data_analysis = require("../lib/data_analysis");

// @flow
function isObj(value
/*: any*/
)
/*: boolean*/
{
  var type = (0, _typeof2.default)(value);
  return value !== null && (type === 'object' || type === 'function');
}

function isImageAttachment(attachment
/*: Attachment*/
)
/*: boolean*/
{
  var mimeType = attachment.type || _mime.default.getType(attachment.id);

  return mimeType && mimeType.split('/')[0] === 'image';
}

function getLastImage(observation
/*: Observation*/
)
/*: Attachment | void*/
{
  var _context;

  var imageAttachments = (0, _filter.default)(_context = observation.attachments || []).call(_context, isImageAttachment);
  if (!imageAttachments) return;
  return imageAttachments[imageAttachments.length - 1];
}

var hiddenTags = {
  categoryId: true,
  notes: true,
  note: true
};

function defaultGetPreset(observation
/*: Observation*/
, stats
/*:: ?: Statistics*/
)
/*: PresetWithAdditionalFields*/
{
  var _context2;

  return {
    id: observation.id,
    geometry: ['point'],
    name: observation.tags && observation.tags.name || '',
    tags: {},
    fields: [],
    additionalFields: (0, _filter.default)(_context2 = (0, _data_analysis.getFields)(observation.tags, stats)).call(_context2, function (field) {
      // Hacky - change. Hide categoryId and notes fields.
      var fieldKey = (0, _isArray.default)(field.key) ? field.key[0] : field.key;
      if (hiddenTags[fieldKey]) return false;
      return true;
    })
  };
}

function leftPad(str
/*: string*/
, len
/*: number*/
, char
/*: string*/
)
/*: string*/
{
  // doesn't need to pad
  len = len - str.length;
  if (len <= 0) return str;
  var pad = '';

  while (true) {
    if (len & 1) pad += char;
    len >>= 1;
    if (len) char += char;else break;
  }

  return pad + str;
}

function getDateString(date
/*: Date*/
)
/*: string | void*/
{
  var _context3, _context4;

  if (!(date instanceof Date)) return;
  var YYYY = date.getFullYear();
  var MM = leftPad(date.getMonth() + 1 + '', 2, '0');
  var DD = leftPad(date.getDate() + '', 2, '0');
  return (0, _concat.default)(_context3 = (0, _concat.default)(_context4 = "".concat(YYYY, "-")).call(_context4, MM, "-")).call(_context3, DD);
}

var shortDateRegExp = /^(\d{4})-(\d{2})-(\d{2})$/;
/**
 * This is necessary because Date.parse() of a string of the form 'YYYY-MM-DD'
 * will assume the timezone is UTC, so in different timezones the returned date
 * will not be what is expected.
 */

function parseDateString(str
/*: string*/
)
/*: Date | void*/
{
  if (!str) return;
  var match = str.match(shortDateRegExp);

  if (!match) {
    var date = Date.parse(str);
    return (0, _isNan.default)(date) ? undefined : new Date(date);
  }

  return new Date(+match[1], +match[2] - 1, +match[3]);
}
//# sourceMappingURL=helpers.js.map