'use strict';

exports.__esModule = true;

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _Checkbox = require('./../Checkbox');

var _Checkbox2 = _interopRequireDefault(_Checkbox);

var _Radio = require('./../Radio');

var _Radio2 = _interopRequireDefault(_Radio);

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

function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }

var SelectGroup = (0, _react.forwardRef)(function (_ref, ref) {
  var onClick = _ref.onClick,
      onChange = _ref.onChange,
      _ref$multiple = _ref.multiple,
      multiple = _ref$multiple === undefined ? false : _ref$multiple,
      _ref$list = _ref.list,
      list = _ref$list === undefined ? [] : _ref$list,
      selected = _ref.selected,
      _ref$pickerProps = _ref.pickerProps,
      pickerProps = _ref$pickerProps === undefined ? {} : _ref$pickerProps,
      others = _objectWithoutProperties(_ref, ['onClick', 'onChange', 'multiple', 'list', 'selected', 'pickerProps']);

  // 过滤非法数据
  list = list.filter(function (item) {
    if (!item || !item.name) return false;
    return true;
  });

  // 构建选中项
  var selectedMap = {};
  function updateSelectedMap() {
    for (var _iterator = list, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
      var _ref2;

      if (_isArray) {
        if (_i >= _iterator.length) break;
        _ref2 = _iterator[_i++];
      } else {
        _i = _iterator.next();
        if (_i.done) break;
        _ref2 = _i.value;
      }

      var item = _ref2;

      if (selected && selected.length) {
        // selected匹配选中项
        for (var _iterator2 = selected, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : _iterator2[Symbol.iterator]();;) {
          var _ref3;

          if (_isArray2) {
            if (_i2 >= _iterator2.length) break;
            _ref3 = _iterator2[_i2++];
          } else {
            _i2 = _iterator2.next();
            if (_i2.done) break;
            _ref3 = _i2.value;
          }

          var selectedItem = _ref3;

          if (item.id !== undefined && item.id === selectedItem.id || item.name === selectedItem.name) {
            selectedMap[item.id || item.name] = item;
          }
        }
      } else if (others.value && others.value.split(pickerProps.split || ',').indexOf(item.name) !== -1) {
        // value匹配选中项
        selectedMap[item.id || item.name] = item;
      }
    }
  }
  updateSelectedMap();
  // 判断是否选中
  function isSelected(currentItem) {
    if (selectedMap[currentItem.id || currentItem.name]) return true;
    return false;
  }
  // 点击事件
  function clickHandler(e, checked, item, index) {
    if (!others.readOnly) {
      // 非只读时才能修改选中值
      if (multiple) {
        // 多选
        if (selectedMap[item.id || item.name]) {
          delete selectedMap[item.id || item.name];
        } else {
          selectedMap[item.id || item.name] = item;
        }
      } else {
        var _selectedMap;

        // 单选
        selectedMap = (_selectedMap = {}, _selectedMap[item.id || item.name] = item, _selectedMap);
      }
    }
    // 构建value
    var value = [];
    for (var selectedMapPropertyName in selectedMap) {
      value.push(selectedMap[selectedMapPropertyName].name);
    }
    value = value.join(pickerProps.split || ',');

    if (!others.readOnly && onChange) onChange(e, value, Object.values(selectedMap));
    if (onClick) onClick(e, value);
  }
  return _react2.default.createElement(
    'div',
    { ref: ref, className: 'inputselect-checkbox-group' + (others.className ? ' ' + others.className : '') },
    list.map(function (item, index) {
      return _react2.default.createElement(
        'div',
        { className: 'inputselect-checkbox-item', key: index },
        multiple && _react2.default.createElement(_Checkbox2.default, {
          disabled: others.disabled,
          caption: item.name,
          checked: isSelected(item),
          onClick: function onClick(e, checked) {
            return clickHandler(e, checked, item, index);
          } }),
        !multiple && _react2.default.createElement(_Radio2.default, {
          disabled: others.disabled,
          caption: item.name,
          checked: isSelected(item),
          onClick: function onClick(e, checked) {
            return clickHandler(e, checked, item, index);
          } })
      );
    })
  );
});

exports.default = SelectGroup;
module.exports = exports['default'];