"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _base = _interopRequireDefault(require("@emotion/styled/base"));
var _react = _interopRequireDefault(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _reactWaypoint = require("react-waypoint");
var _immutable = require("immutable");
var _decapCmsUiDefault = require("decap-cms-ui-default");
var _reactWindow = require("react-window");
var _reactVirtualizedAutoSizer = _interopRequireDefault(require("react-virtualized-auto-sizer"));
var _MediaLibraryCard = _interopRequireDefault(require("./MediaLibraryCard"));
var _react2 = require("@emotion/react");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _EMOTION_STRINGIFIED_CSS_ERROR__() { return "You have tried to stringify object returned from `css` function. It isn't supposed to be used directly (e.g. as value of the `className` prop), but rather handed to emotion so it can handle it (e.g. as value of `css` prop)."; }
function CardWrapper(props) {
  const {
    rowIndex,
    columnIndex,
    style,
    data: {
      mediaItems,
      isSelectedFile,
      onAssetClick,
      cardDraftText,
      cardWidth,
      cardHeight,
      isPrivate,
      displayURLs,
      loadDisplayURL,
      columnCount,
      gutter
    }
  } = props;
  const index = rowIndex * columnCount + columnIndex;
  if (index >= mediaItems.length) {
    return null;
  }
  const file = mediaItems[index];
  return (0, _react2.jsx)("div", {
    tabIndex: "0",
    style: _objectSpread(_objectSpread({}, style), {}, {
      left: style.left + gutter * columnIndex,
      top: style.top + gutter,
      width: style.width - gutter,
      height: style.height - gutter
    })
  }, (0, _react2.jsx)(_MediaLibraryCard.default, {
    key: file.key,
    isSelected: isSelectedFile(file),
    text: file.name,
    onClick: () => onAssetClick(file),
    isDraft: file.draft,
    draftText: cardDraftText,
    width: cardWidth,
    height: cardHeight,
    margin: '0px',
    isPrivate: isPrivate,
    displayURL: displayURLs.get(file.id, file.url ? (0, _immutable.Map)({
      url: file.url
    }) : (0, _immutable.Map)()),
    loadDisplayURL: () => loadDisplayURL(file),
    type: file.type,
    isViewableImage: file.isViewableImage
  }));
}
function VirtualizedGrid(props) {
  const {
    mediaItems,
    setScrollContainerRef
  } = props;
  return (0, _react2.jsx)(CardGridContainer, {
    ref: setScrollContainerRef
  }, (0, _react2.jsx)(_reactVirtualizedAutoSizer.default, null, ({
    height,
    width
  }) => {
    const cardWidth = parseInt(props.cardWidth, 10);
    const cardHeight = parseInt(props.cardHeight, 10);
    const gutter = parseInt(props.cardMargin, 10);
    const columnWidth = cardWidth + gutter;
    const rowHeight = cardHeight + gutter;
    const columnCount = Math.floor(width / columnWidth);
    const rowCount = Math.ceil(mediaItems.length / columnCount);
    return (0, _react2.jsx)(_reactWindow.FixedSizeGrid, {
      columnCount: columnCount,
      columnWidth: columnWidth,
      rowCount: rowCount,
      rowHeight: rowHeight,
      width: width,
      height: height,
      itemData: _objectSpread(_objectSpread({}, props), {}, {
        gutter,
        columnCount
      })
    }, CardWrapper);
  }));
}
function PaginatedGrid({
  setScrollContainerRef,
  mediaItems,
  isSelectedFile,
  onAssetClick,
  cardDraftText,
  cardWidth,
  cardHeight,
  cardMargin,
  isPrivate,
  displayURLs,
  loadDisplayURL,
  canLoadMore,
  onLoadMore,
  isPaginating,
  paginatingMessage
}) {
  return (0, _react2.jsx)(CardGridContainer, {
    ref: setScrollContainerRef
  }, (0, _react2.jsx)(CardGrid, null, mediaItems.map(file => (0, _react2.jsx)(_MediaLibraryCard.default, {
    key: file.key,
    isSelected: isSelectedFile(file),
    text: file.name,
    onClick: () => onAssetClick(file),
    isDraft: file.draft,
    draftText: cardDraftText,
    width: cardWidth,
    height: cardHeight,
    margin: cardMargin,
    isPrivate: isPrivate,
    displayURL: displayURLs.get(file.id, file.url ? (0, _immutable.Map)({
      url: file.url
    }) : (0, _immutable.Map)()),
    loadDisplayURL: () => loadDisplayURL(file),
    type: file.type,
    isViewableImage: file.isViewableImage
  })), !canLoadMore ? null : (0, _react2.jsx)(_reactWaypoint.Waypoint, {
    onEnter: onLoadMore
  })), !isPaginating ? null : (0, _react2.jsx)(PaginatingMessage, {
    isPrivate: isPrivate
  }, paginatingMessage));
}
const CardGridContainer = /*#__PURE__*/(0, _base.default)("div", {
  target: "e1l5oka22",
  label: "CardGridContainer"
})(process.env.NODE_ENV === "production" ? {
  name: "bv8zlo",
  styles: "overflow-y:auto;overflow-x:hidden"
} : {
  name: "bv8zlo",
  styles: "overflow-y:auto;overflow-x:hidden",
  map: "/*# sourceMappingURL=data:application/json;charset=utf-8;base64,[encoded data, 7844 characters] */",
  toString: _EMOTION_STRINGIFIED_CSS_ERROR__
});
const CardGrid = /*#__PURE__*/(0, _base.default)("div", {
  target: "e1l5oka21",
  label: "CardGrid"
})(process.env.NODE_ENV === "production" ? {
  name: "15jhlc1",
  styles: "display:flex;flex-wrap:wrap;margin-left:-10px;margin-right:-10px"
} : {
  name: "15jhlc1",
  styles: "display:flex;flex-wrap:wrap;margin-left:-10px;margin-right:-10px",
  map: "/*# sourceMappingURL=data:application/json;charset=utf-8;base64,[encoded data, 7844 characters] */",
  toString: _EMOTION_STRINGIFIED_CSS_ERROR__
});
const PaginatingMessage = /*#__PURE__*/(0, _base.default)("h1", {
  target: "e1l5oka20",
  label: "PaginatingMessage"
})("color:", props => props.isPrivate && _decapCmsUiDefault.colors.textFieldBorder, ";" + (process.env.NODE_ENV === "production" ? "" : "/*# sourceMappingURL=data:application/json;charset=utf-8;base64,[encoded data, 7844 characters] */"));
function MediaLibraryCardGrid(props) {
  const {
    canLoadMore,
    isPaginating
  } = props;
  if (canLoadMore || isPaginating) {
    return (0, _react2.jsx)(PaginatedGrid, props);
  }
  return (0, _react2.jsx)(VirtualizedGrid, props);
}
MediaLibraryCardGrid.propTypes = {
  setScrollContainerRef: _propTypes.default.func.isRequired,
  mediaItems: _propTypes.default.arrayOf(_propTypes.default.shape({
    displayURL: _propTypes.default.oneOfType([_propTypes.default.string, _propTypes.default.object]),
    id: _propTypes.default.string.isRequired,
    key: _propTypes.default.string.isRequired,
    name: _propTypes.default.string.isRequired,
    type: _propTypes.default.string.isRequired,
    draft: _propTypes.default.bool
  })).isRequired,
  isSelectedFile: _propTypes.default.func.isRequired,
  onAssetClick: _propTypes.default.func.isRequired,
  canLoadMore: _propTypes.default.bool,
  onLoadMore: _propTypes.default.func.isRequired,
  isPaginating: _propTypes.default.bool,
  paginatingMessage: _propTypes.default.string,
  cardDraftText: _propTypes.default.string.isRequired,
  cardWidth: _propTypes.default.string.isRequired,
  cardMargin: _propTypes.default.string.isRequired,
  loadDisplayURL: _propTypes.default.func.isRequired,
  isPrivate: _propTypes.default.bool,
  displayURLs: _propTypes.default.instanceOf(_immutable.Map).isRequired
};
var _default = exports.default = MediaLibraryCardGrid;