var _excluded = ["checkboxGroupAttribute", "onClick", "onChange", "multiple", "list", "selected", "value", "pickerProps"];

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

function _createForOfIteratorHelperLoose(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (it) return (it = it.call(o)).next.bind(it); if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; return function () { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

import React, { forwardRef } from 'react';
import Checkbox from './../Checkbox';
import Radio from './../Radio';
var SelectGroup = forwardRef(function (_ref, ref) {
  var _ref$checkboxGroupAtt = _ref.checkboxGroupAttribute,
      checkboxGroupAttribute = _ref$checkboxGroupAtt === void 0 ? {} : _ref$checkboxGroupAtt,
      onClick = _ref.onClick,
      onChange = _ref.onChange,
      _ref$multiple = _ref.multiple,
      multiple = _ref$multiple === void 0 ? false : _ref$multiple,
      _ref$list = _ref.list,
      list = _ref$list === void 0 ? [] : _ref$list,
      selected = _ref.selected,
      value = _ref.value,
      _ref$pickerProps = _ref.pickerProps,
      pickerProps = _ref$pickerProps === void 0 ? {} : _ref$pickerProps,
      others = _objectWithoutPropertiesLoose(_ref, _excluded);

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

  var selectedMap = {};

  function updateSelectedMap() {
    for (var _iterator = _createForOfIteratorHelperLoose(list), _step; !(_step = _iterator()).done;) {
      var item = _step.value;

      if (selected && selected.length) {
        // selected匹配选中项
        for (var _iterator2 = _createForOfIteratorHelperLoose(selected), _step2; !(_step2 = _iterator2()).done;) {
          var selectedItem = _step2.value;

          if (item.id !== undefined && item.id === selectedItem.id || item.name === selectedItem.name) {
            selectedMap[item.id || item.name] = item;
          }
        }
      } else if (value) {
        var selectedValues = [value];

        if (multiple) {
          selectedValues = value.split(pickerProps.split || ',');
        } // value匹配选中项


        if (selectedValues.indexOf(item.name) !== -1) {
          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 /*#__PURE__*/React.createElement("div", _extends({
    ref: ref,
    id: others.id ? others.id : ''
  }, checkboxGroupAttribute, {
    className: "inputselect-checkbox-group" + (others.className ? ' ' + others.className : '')
  }), list.map(function (item, index) {
    return /*#__PURE__*/React.createElement("div", {
      className: "inputselect-checkbox-item",
      key: index
    }, multiple && /*#__PURE__*/React.createElement(Checkbox, {
      disabled: others.disabled,
      caption: item.name,
      checked: isSelected(item),
      onClick: function onClick(e, checked) {
        return clickHandler(e, checked, item, index);
      }
    }), !multiple && /*#__PURE__*/React.createElement(Radio, {
      disabled: others.disabled,
      caption: item.name,
      checked: isSelected(item),
      onClick: function onClick(e, checked) {
        return clickHandler(e, checked, item, index);
      }
    }));
  }));
});
export default SelectGroup;