"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = exports.Index = void 0;
var _react = _interopRequireDefault(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _styles = require("@material-ui/core/styles");
var _i18n = require("../../../../../Framework/i18n");
var _isFunction = _interopRequireDefault(require("lodash/isFunction"));
var _Checkbox = _interopRequireDefault(require("@material-ui/core/Checkbox"));
var _FormControlLabel = _interopRequireDefault(require("@material-ui/core/FormControlLabel"));
var _Icon = _interopRequireDefault(require("@material-ui/core/Icon"));
var _Button = _interopRequireDefault(require("@material-ui/core/Button"));
var _Popover = _interopRequireDefault(require("@material-ui/core/Popover"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); }
function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); }
function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
var styles = function styles() {
  return {
    ImageLibraryCheckboxRoot: {
      padding: '6px 12px'
    },
    imageLibraryFormControl: {
      display: 'flex'
    },
    popOverContent: {
      padding: 16,
      width: 228
    },
    dropdownButton: {
      borderBottom: '1px solid #C4C4C4',
      borderRadius: 0,
      textTransform: 'none',
      minWidth: 130,
      marginBottom: 16
    },
    filterPopoverIcon: {
      marginLeft: 40
    },
    applyFilterButton: {
      textTransform: 'none'
    }
  };
};
var Index = exports.Index = function Index(_ref) {
  var tags = _ref.tags,
    classes = _ref.classes,
    applyFilter = _ref.applyFilter;
  var _React$useState = _react["default"].useState(null),
    _React$useState2 = _slicedToArray(_React$useState, 2),
    anchorElement = _React$useState2[0],
    setAnchorElement = _React$useState2[1];
  var _React$useState3 = _react["default"].useState(tags || []),
    _React$useState4 = _slicedToArray(_React$useState3, 2),
    filterTags = _React$useState4[0],
    setFilteredTags = _React$useState4[1];
  var handleClick = function handleClick(event) {
    setAnchorElement(event.currentTarget);
  };
  var handleClose = function handleClose() {
    setAnchorElement(null);
  };
  var onSelectTag = function onSelectTag(tag) {
    return function (event) {
      var index = filterTags.findIndex(function (elem) {
        return elem.title === tag.title;
      });
      var newTags = [].concat(_toConsumableArray(filterTags.slice(0, index)), [{
        checked: event.target.checked,
        title: tag.title
      }], _toConsumableArray(filterTags.slice(index + 1)));
      setFilteredTags(newTags);
    };
  };
  var onApplyFilter = function onApplyFilter() {
    if ((0, _isFunction["default"])(applyFilter)) {
      applyFilter(filterTags);
    }
    handleClose();
  };
  var open = Boolean(anchorElement);
  var id = open ? 'filter-popover' : undefined;
  return /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, /*#__PURE__*/_react["default"].createElement(_Button["default"], {
    "aria-describedby": id,
    variant: "text",
    className: classes.dropdownButton,
    onClick: handleClick
  }, (0, _i18n.t)('imageLibrary.filter'), /*#__PURE__*/_react["default"].createElement(_Icon["default"], {
    className: classes.filterPopoverIcon
  }, "keyboard_arrow_down")), /*#__PURE__*/_react["default"].createElement(_Popover["default"], {
    id: id,
    open: open,
    anchorEl: anchorElement,
    onClose: handleClose,
    anchorOrigin: {
      vertical: 'top',
      horizontal: 'left'
    },
    transformOrigin: {
      vertical: 'top',
      horizontal: 'left'
    }
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: classes.popOverContent
  }, filterTags && filterTags.map(function (tag) {
    return /*#__PURE__*/_react["default"].createElement(_FormControlLabel["default"], {
      className: classes.imageLibraryFormControl,
      key: tag.title,
      control: /*#__PURE__*/_react["default"].createElement(_Checkbox["default"], {
        checked: tag.checked,
        className: classes.ImageLibraryCheckboxRoot,
        onClick: onSelectTag(tag),
        value: tag.title,
        color: "primary",
        key: tag.title
      }),
      label: tag.title
    });
  }), /*#__PURE__*/_react["default"].createElement(_Button["default"], {
    variant: "outlined",
    fullWidth: true,
    onClick: onApplyFilter,
    className: classes.applyFilterButton
  }, (0, _i18n.t)('imageLibrary.applyFilter')))));
};
var _default = exports["default"] = (0, _styles.withStyles)(styles, {
  name: 'Index'
})(Index);