"use strict";

var _interopRequireWildcard = require("@babel/runtime-corejs3/helpers/interopRequireWildcard");

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.object.to-string");

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

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

exports.default = void 0;

var _getIterator2 = _interopRequireDefault(require("@babel/runtime-corejs3/core-js/get-iterator"));

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

var _getIteratorMethod2 = _interopRequireDefault(require("@babel/runtime-corejs3/core-js/get-iterator-method"));

var _symbol = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/symbol"));

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

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

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

var _react = _interopRequireWildcard(require("react"));

var _ImageGrid = _interopRequireDefault(require("./ImageGrid"));

var _helpers = require("../utils/helpers");

function _createForOfIteratorHelper(o, allowArrayLike) { var it; if (typeof _symbol.default === "undefined" || (0, _getIteratorMethod2.default)(o) == null) { if ((0, _isArray.default)(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = (0, _getIterator2.default)(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }

function _unsupportedIterableToArray(o, minLen) { var _context; if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = (0, _slice.default)(_context = Object.prototype.toString.call(o)).call(_context, 8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return (0, _from.default)(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }

/**
 * Displays a grid of all photos attached to observations.
 */
var MediaViewContent = function MediaViewContent(_ref) {
  var observations = _ref.observations,
      onClick = _ref.onClick,
      getMedia = _ref.getMedia;
  var images = (0, _react.useMemo)(function () {
    var images = [];
    var sortedObservations = (0, _sort.default)(observations).call(observations, function (a, b) {
      return a.created_at > b.created_at ? -1 : a.created_at < b.created_at ? 1 : 0;
    });

    var _iterator = _createForOfIteratorHelper(sortedObservations),
        _step;

    try {
      for (_iterator.s(); !(_step = _iterator.n()).done;) {
        var obs = _step.value;
        var attachments = obs.attachments || [];

        for (var i = 0; i < attachments.length; i++) {
          // Only return attachments with images
          if (!(0, _helpers.isImageAttachment)(attachments[i])) continue; // check we can actually get an image src for each one before adding it

          var media = getMedia(attachments[i], {
            width: 200,
            height: 200
          });
          if (media) images.push({
            index: i,
            src: media.src,
            observationId: obs.id
          });
        }
      }
    } catch (err) {
      _iterator.e(err);
    } finally {
      _iterator.f();
    }

    return images;
  }, [observations, getMedia]);
  return /*#__PURE__*/_react.default.createElement(_ImageGrid.default, {
    images: images,
    onImageClick: onClick
  });
};

var _default = MediaViewContent;
exports.default = _default;
//# sourceMappingURL=MediaViewContent.js.map