"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;
var _withWidth = _interopRequireDefault(require("@material-ui/core/withWidth"));
var _ComponentContainer = require("../../../../Framework/ComponentContainer");
var _createSelector = _interopRequireDefault(require("../../../../Utils/Function/createSelector"));
var _findIndex = _interopRequireDefault(require("lodash/findIndex"));
var _AddGalleryImage = _interopRequireDefault(require("../Components/AddGalleryImage"));
var _Progress = _interopRequireDefault(require("../../../Shared/Components/Progress/Progress"));
var _Actions = require("../../../Reducers/ImageGallery/Actions");
var _Selectors = require("../../../Reducers/UI/Selectors");
var _getUrl = require("../../../Utils/getUrl");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
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(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : 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); }
var getTags = (0, _createSelector["default"])(function (imageGalleryState) {
  return imageGalleryState.tags;
}, function (imageGalleryState) {
  return imageGalleryState.selectedTag;
}, function (imageGalleryState) {
  return imageGalleryState.selectedTags;
}, function (tags, selectedTag, selectedTags) {
  return tags.map(function (tag) {
    return {
      title: tag.title,
      checked: selectedTags && (0, _findIndex["default"])(selectedTags, function (selectedTag) {
        if (selectedTag.title === tag.title) {
          return selectedTag.checked;
        }
      }) > -1
    };
  });
});
var getGalleryImages = (0, _createSelector["default"])(function (imageGalleryState) {
  return imageGalleryState.gallery;
}, function (images) {
  return images.map(function (image) {
    return _objectSpread(_objectSpread({}, image), {}, {
      url: (0, _getUrl.getUrl)(image.thumbUrl)
    });
  });
});
var getLoadingState = (0, _Selectors.createBackgroundWorkStateSelector)(_Actions.WN_LOAD_TAGS, _Actions.WN_LOAD_IMAGES);
var mapStateToProps = function mapStateToProps(state) {
  return {
    Component: _AddGalleryImage["default"],
    ProgressComponent: _Progress["default"],
    tags: getTags(state.imageGallery),
    images: getGalleryImages(state.imageGallery),
    loading: getLoadingState(state.ui)
  };
};
var mapDispatchToProps = function mapDispatchToProps(dispatch) {
  return {
    loadTags: function loadTags() {
      return dispatch((0, _Actions.fetchTags)());
    },
    onSelectTag: function onSelectTag(tags) {
      return dispatch((0, _Actions.selectTags)(tags));
    },
    dismissError: function dismissError() {
      return dispatch((0, _Actions.dismissError)());
    }
  };
};
var _default = exports["default"] = (0, _withWidth["default"])()((0, _ComponentContainer.containerConnect)(mapStateToProps, mapDispatchToProps));