'use strict';

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

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _createClass2 = require('babel-runtime/helpers/createClass');

var _createClass3 = _interopRequireDefault(_createClass2);

var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');

var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);

var _inherits2 = require('babel-runtime/helpers/inherits');

var _inherits3 = _interopRequireDefault(_inherits2);

var _dec, _class, _class2, _temp2;

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _memoize = require('lodash/memoize');

var _memoize2 = _interopRequireDefault(_memoize);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

var _reactImmutableProptypes = require('react-immutable-proptypes');

var _reactImmutableProptypes2 = _interopRequireDefault(_reactImmutableProptypes);

var _immutable = require('immutable');

var _IconPlusLine = require('instructure-icons/lib/Line/IconPlusLine');

var _IconPlusLine2 = _interopRequireDefault(_IconPlusLine);

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

var _Button2 = _interopRequireDefault(_Button);

var _Heading = require('@instructure/ui-elements/lib/components/Heading');

var _Heading2 = _interopRequireDefault(_Heading);

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

var _Checkbox2 = _interopRequireDefault(_Checkbox);

var _Select = require('@instructure/ui-forms/lib/components/Select');

var _Select2 = _interopRequireDefault(_Select);

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

var _TextInput2 = _interopRequireDefault(_TextInput);

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

var _AccessibleContent2 = _interopRequireDefault(_AccessibleContent);

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

var _lib2 = _interopRequireDefault(_lib);

var _formatMessage = require('../../../common/format-message');

var _formatMessage2 = _interopRequireDefault(_formatMessage);

var _theme = require('./theme');

var _theme2 = _interopRequireDefault(_theme);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var styles = {
  template: function template(theme) {
    return '/*  imported from styles.css  */\n\n.BankSearch__styles__searchBar {\n  display: flex;\n  flex-direction: row;\n}\n\n.BankSearch__styles__interactionType {\n  padding: ' + theme.searchInteractionsPaddingTopBottom + ' ' + theme.searchInteractionsPaddingLeftRight + ';\n}\n\n.BankSearch__styles__filterContainer {\n  display: flex;\n  margin-top: 1rem;\n}\n\n.BankSearch__styles__filterColumn {\n  flex: 1;\n  margin-right: 2rem;\n}\n';
  },
  'searchBar': 'BankSearch__styles__searchBar',
  'interactionType': 'BankSearch__styles__interactionType',
  'filterContainer': 'BankSearch__styles__filterContainer',
  'filterColumn': 'BankSearch__styles__filterColumn'
};
var BankSearch = (_dec = (0, _lib2.default)(_theme2.default, styles), _dec(_class = (_temp2 = _class2 = function (_Component) {
  (0, _inherits3.default)(BankSearch, _Component);

  function BankSearch() {
    var _ref;

    var _temp, _this, _ret;

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

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

    return _ret = (_temp = (_this = (0, _possibleConstructorReturn3.default)(this, (_ref = BankSearch.__proto__ || Object.getPrototypeOf(BankSearch)).call.apply(_ref, [this].concat(args))), _this), _this.toggleInteraction = (0, _memoize2.default)(function (it) {
      return function () {
        return _this.props.toggle('interactionTypes', it);
      };
    }), _this.updateTags = function (event, options) {
      var active = _this.props.search.get('tags');
      var next = (0, _immutable.Set)(options.map(function (_ref2) {
        var id = _ref2.id;
        return id;
      }));

      var toggle = function toggle(id) {
        return _this.props.toggle('tags', id);
      };
      next.subtract(active).toJS().map(toggle);
      active.subtract(next).toJS().map(toggle);
    }, _this.updateSearch = function (_ref3) {
      var value = _ref3.target.value;
      return _this.props.update(value);
    }, _temp), (0, _possibleConstructorReturn3.default)(_this, _ret);
  }

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

      var _props = this.props,
          allInteractions = _props.allInteractions,
          search = _props.search,
          showFilters = _props.showFilters,
          toggleAllInteractions = _props.toggleAllInteractions,
          interactionTypes = _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 (_ref4) {
          var count = _ref4.count,
              it = _ref4.interaction_type;

          var label = it.name + ' (' + count + ')';
          return _react2.default.createElement(
            'div',
            { key: it.id, className: styles.interactionType },
            _react2.default.createElement(_Checkbox2.default, {
              checked: active.has(it.id),
              label: label,
              onChange: _this2.toggleInteraction(it.id),
              value: 'medium'
            })
          );
        });

        var tagOptions = this.props.tags.map(function (_ref5) {
          var tag = _ref5.tag;

          return _react2.default.createElement(
            'option',
            { value: tag.id, key: tag.id },
            tag.value
          );
        });
        var tagSelect = _react2.default.createElement(
          _Select2.default,
          {
            label: 'Tags',
            multiple: true,
            editable: true,
            onChange: this.updateTags
          },
          tagOptions
        );

        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 _react2.default.createElement(
              _Button2.default,
              {
                onClick: toggleAllInteractions,
                margin: 'none small',
                variant: 'link'
              },
              _react2.default.createElement(
                _AccessibleContent2.default,
                { alt: text },
                text
              )
            );
          }
        };

        return _react2.default.createElement(
          'div',
          { className: styles.filterContainer },
          _react2.default.createElement(
            'div',
            { className: styles.filterColumn },
            tagSelect
          ),
          _react2.default.createElement(
            'div',
            { className: styles.filterColumn },
            _react2.default.createElement(
              _Heading2.default,
              { level: 'h4', margin: 'small' },
              'Item Type'
            ),
            interactionFilters,
            toggleButton()
          )
        );
      }
    }
  }, {
    key: 'render',
    value: function render() {
      var toggleFilters = this.props.toggleFilters;

      return _react2.default.createElement(
        'div',
        null,
        _react2.default.createElement(
          'div',
          { className: styles.searchBar },
          _react2.default.createElement(_TextInput2.default, {
            type: 'search',
            placeholder: '\u691C\u7D22',
            onChange: this.updateSearch,
            value: this.props.search.get('text', '')
          }),
          _react2.default.createElement(
            _Button2.default,
            { onClick: toggleFilters, margin: 'none small', icon: _IconPlusLine2.default },
            _react2.default.createElement(
              _AccessibleContent2.default,
              { alt: 'Show search filters' },
              'Filters'
            )
          )
        ),
        this.renderFilters()
      );
    }
  }]);
  BankSearch.displayName = 'BankSearch'
  ;
  return BankSearch;
}(_react.Component), _class2.propTypes = {
  allInteractions: _propTypes2.default.bool,
  interactionTypes: _propTypes2.default.list,
  showFilters: _propTypes2.default.bool.isRequired,
  tags: _propTypes2.default.list,
  toggleFilters: _propTypes2.default.func.isRequired,
  toggleAllInteractions: _propTypes2.default.func.isRequired,
  search: _reactImmutableProptypes2.default.map.isRequired,
  toggle: _propTypes2.default.func.isRequired,
  update: _propTypes2.default.func.isRequired
}, _class2.defaultProps = {
  allInteractions: false,
  interactionTypes: [],
  tags: []
}, _temp2)) || _class);
exports.default = BankSearch;