"use strict";

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

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

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));

var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));

var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));

var _getPrototypeOf3 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));

var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));

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

var _memoize = _interopRequireDefault(require("lodash/memoize"));

var _propTypes = _interopRequireDefault(require("prop-types"));

var _reactImmutableProptypes = _interopRequireDefault(require("react-immutable-proptypes"));

var _IconPlus = _interopRequireDefault(require("@instructure/ui-icons/lib/Line/IconPlus"));

var _Button = _interopRequireDefault(require("@instructure/ui-buttons/lib/components/Button"));

var _Checkbox = _interopRequireDefault(require("@instructure/ui-forms/lib/components/Checkbox"));

var _Flex = _interopRequireWildcard(require("@instructure/ui-layout/lib/components/Flex"));

var _Text = _interopRequireDefault(require("@instructure/ui-elements/lib/components/Text"));

var _TextInput = _interopRequireDefault(require("@instructure/ui-forms/lib/components/TextInput"));

var _AccessibleContent = _interopRequireDefault(require("@instructure/ui-a11y/lib/components/AccessibleContent"));

var _View = _interopRequireDefault(require("@instructure/ui-layout/lib/components/View"));

var _lib = _interopRequireDefault(require("@instructure/ui-themeable/lib"));

var _formatMessage = _interopRequireDefault(require("@instructure/quiz-i18n/lib/format-message"));

var _TagSuggestSelect = _interopRequireDefault(require("../../../common/components/shared/TagSuggestSelect"));

var _theme = _interopRequireDefault(require("./theme"));

var _dec, _class, _class2, _temp;

var styles = {
  componentId: 'eyMTa',
  template: function template(theme) {
    return "\n\n.quiz-core__BankSearch--interactionType {\n  padding: ".concat(theme.searchInteractionsPaddingTopBottom || 'inherit', " ").concat(theme.searchInteractionsPaddingLeftRight || 'inherit', ";\n}\n\n.quiz-core__BankSearch--filterContainer {\n  display: flex;\n  margin-top: 1rem;\n}\n\n.quiz-core__BankSearch--filterColumn {\n  flex: 1;\n  margin-right: 2rem;\n}\n");
  },
  'interactionType': 'quiz-core__BankSearch--interactionType',
  'filterContainer': 'quiz-core__BankSearch--filterContainer',
  'filterColumn': 'quiz-core__BankSearch--filterColumn'
};

var tagShape = _propTypes.default.shape({
  id: _propTypes.default.string,
  value: _propTypes.default.string.isRequired
});

var BankSearch = (_dec = (0, _lib.default)(_theme.default, styles), _dec(_class = (_temp = _class2 =
/*#__PURE__*/
function (_Component) {
  (0, _inherits2.default)(BankSearch, _Component);

  function BankSearch() {
    var _getPrototypeOf2;

    var _this;

    (0, _classCallCheck2.default)(this, BankSearch);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = (0, _possibleConstructorReturn2.default)(this, (_getPrototypeOf2 = (0, _getPrototypeOf3.default)(BankSearch)).call.apply(_getPrototypeOf2, [this].concat(args)));
    _this.toggleInteraction = (0, _memoize.default)(function (it) {
      return function () {
        return _this.props.toggle('interactionTypes', it);
      };
    });

    _this.setTags = function (tags) {
      return _this.props.setFilter('tags', tags);
    };

    _this.updateSearch = function (_ref) {
      var value = _ref.target.value;
      return _this.props.update(value);
    };

    return _this;
  }

  (0, _createClass2.default)(BankSearch, [{
    key: "renderFilters",
    value: function renderFilters() {
      var _this2 = this;

      var _this$props = this.props,
          allInteractions = _this$props.allInteractions,
          search = _this$props.search,
          showFilters = _this$props.showFilters,
          toggleAllInteractions = _this$props.toggleAllInteractions,
          interactionTypes = _this$props.interactionTypes;

      if (showFilters) {
        var its = interactionTypes;
        var shown = allInteractions ? its : its.slice(0, 5);
        var active = search.get('interactionTypes');
        var interactionFilters = shown.map(function (_ref2) {
          var count = _ref2.count,
              it = _ref2.interaction_type;
          var label = "".concat(it.name, " (").concat(count, ")");
          return _react.default.createElement("div", {
            key: it.id,
            className: styles.interactionType
          }, _react.default.createElement(_Checkbox.default, {
            checked: active.has(it.id),
            label: label,
            onChange: _this2.toggleInteraction(it.id),
            value: "medium"
          }));
        });
        var suggestions = this.props.tags.map(function (_ref3) {
          var tag = _ref3.tag;
          return tag;
        });
        var activeTags = this.props.search.get('tags');

        var tagSelect = _react.default.createElement(_TagSuggestSelect.default, {
          noMatchText: "No matching tags found",
          parentType: "Bank",
          parentId: "0",
          onChange: this.setTags,
          tags: activeTags,
          defaultSuggestions: suggestions
        });

        var toggleButton = function toggleButton() {
          if (shown.length < its.length || allInteractions) {
            var hide = function hide() {
              return "Show Less";
            };

            var show = function show() {
              return "Show More";
            };

            var text = allInteractions ? hide() : show();
            return _react.default.createElement(_Button.default, {
              onClick: toggleAllInteractions,
              margin: "none small",
              variant: "link"
            }, _react.default.createElement(_AccessibleContent.default, {
              alt: text
            }, text));
          }
        };

        return _react.default.createElement("div", {
          className: styles.filterContainer
        }, _react.default.createElement("div", {
          className: styles.filterColumn
        }, tagSelect), _react.default.createElement("div", {
          className: styles.filterColumn
        }, _react.default.createElement(_View.default, {
          margin: "small"
        }, _react.default.createElement(_Text.default, {
          weight: "bold"
        }, "Item Type")), interactionFilters, toggleButton()));
      }
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props2 = this.props,
          showFilters = _this$props2.showFilters,
          toggleFilters = _this$props2.toggleFilters;
      return _react.default.createElement("div", null, _react.default.createElement(_Flex.default, {
        as: "div"
      }, _react.default.createElement(_Flex.FlexItem, {
        grow: true,
        shrink: true
      }, _react.default.createElement(_TextInput.default, {
        type: "search",
        placeholder: "Search",
        onChange: this.updateSearch,
        value: this.props.search.get('text', '')
      })), _react.default.createElement(_Flex.FlexItem, null, _react.default.createElement(_Button.default, {
        onClick: toggleFilters,
        margin: "none none none small",
        icon: _IconPlus.default,
        "data-automation": "sdk-search-filter-button"
      }, _react.default.createElement(_AccessibleContent.default, {
        alt: showFilters ? "Hide search filters" : "Show search filters"
      }, "Filters")))), this.renderFilters());
    }
  }]);
  BankSearch.displayName = "BankSearch";
  return BankSearch;
}(_react.Component), _class2.propTypes = {
  allInteractions: _propTypes.default.bool,
  interactionTypes: _propTypes.default.list,
  showFilters: _propTypes.default.bool.isRequired,
  tags: _propTypes.default.arrayOf(tagShape),
  toggleFilters: _propTypes.default.func.isRequired,
  toggleAllInteractions: _propTypes.default.func.isRequired,
  search: _reactImmutableProptypes.default.map.isRequired,
  toggle: _propTypes.default.func.isRequired,
  setFilter: _propTypes.default.func.isRequired,
  update: _propTypes.default.func.isRequired
}, _class2.defaultProps = {
  allInteractions: false,
  interactionTypes: [],
  tags: []
}, _temp)) || _class);
exports.default = BankSearch;