'use strict';

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

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _ramda = require('ramda');

var _ramda2 = _interopRequireDefault(_ramda);

require('./Filters.css');

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

var MultiSelectFilter = exports.MultiSelectFilter = function MultiSelectFilter(_ref) {
  var values = _ref.values,
      onFilterChange = _ref.onFilterChange,
      selectAllCheckbox = _ref.selectAllCheckbox,
      defaultChecked = _ref.defaultChecked;

  var domCheckboxes = [];
  var allCheckbox = null;

  var onAllChange = function onAllChange(event) {
    var isAllChecked = event.target.checked;
    _ramda2.default.forEach(function (checkbox) {
      return checkbox.checked = isAllChecked;
    }, domCheckboxes);

    return isAllChecked ? onFilterChange(values) : onFilterChange([]);
  };
  var onOptionChange = function onOptionChange(event) {
    if (selectAllCheckbox) {
      if (!event.target.checked) {
        allCheckbox.checked = false;
      }
      allCheckbox.checked = _ramda2.default.all(function (domCheckbox) {
        return domCheckbox.checked;
      }, domCheckboxes);
    }

    var selectedValuesAsStrings = _ramda2.default.pipe(_ramda2.default.filter(function (checkbox) {
      return checkbox.checked;
    }), _ramda2.default.map(function (checkbox) {
      return checkbox.value;
    }))(domCheckboxes);
    var selectedValues = _ramda2.default.filter(function (_ref2) {
      var value = _ref2.value;
      return _ramda2.default.contains('' + value, selectedValuesAsStrings);
    }, values);
    return onFilterChange(selectedValues);
  };

  var createCheckboxes = function createCheckboxes(val, idx) {
    return _react2.default.createElement(
      'label',
      { key: idx },
      _react2.default.createElement('input', { ref: function ref(checkbox) {
          return domCheckboxes.push(checkbox);
        }, key: idx, type: 'checkbox', value: val.value,
        onChange: onOptionChange, defaultChecked: defaultChecked }),
      val.label
    );
  };

  var mapIndexes = _ramda2.default.addIndex(_ramda2.default.map);
  var checkboxes = mapIndexes(createCheckboxes, values);
  var selectAll = function selectAll() {
    var selectDiv = _react2.default.createElement('div', null);
    if (!_ramda2.default.isEmpty(values)) {
      if (selectAllCheckbox) {
        selectDiv = _react2.default.createElement(
          'div',
          { className: 'select-all' },
          _react2.default.createElement(
            'label',
            null,
            _react2.default.createElement('input', { ref: function ref(checkbox) {
                return allCheckbox = checkbox;
              }, type: 'checkbox', value: '', onChange: onAllChange, defaultChecked: defaultChecked }),
            'All'
          )
        );
      }
    } else {
      selectDiv = _react2.default.createElement(
        'div',
        { className: 'filter-error' },
        'No Filters Available'
      );
    }
    return selectDiv;
  };

  return _react2.default.createElement(
    'div',
    { className: 'selection' },
    selectAll(),
    _react2.default.createElement(
      'div',
      { className: 'checkboxes' },
      checkboxes
    )
  );
};