import _extends from "@babel/runtime/helpers/extends";
import _Checkbox from "@alifd/next/es/checkbox";
import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/objectWithoutPropertiesLoose";
var _excluded = ["style", "disabled", "checked", "direction", "dataSource", "value", "allChecked", "defaultValue", "defaultChecked"];
import * as React from 'react';
import "./index.scss";
var Checklist = function Checklist(_ref) {
  var _ref$style = _ref.style,
    style = _ref$style === void 0 ? {} : _ref$style,
    disabled = _ref.disabled,
    checked = _ref.checked,
    direction = _ref.direction,
    dataSource = _ref.dataSource,
    value = _ref.value,
    allChecked = _ref.allChecked,
    defaultValue = _ref.defaultValue,
    defaultChecked = _ref.defaultChecked,
    otherProps = _objectWithoutPropertiesLoose(_ref, _excluded);
  var plainOptions = dataSource;
  var defaultCheckedList = [];
  dataSource.forEach(function (item, index) {
    defaultCheckedList.push(item.value);
  });
  var _style = style || {};
  var _otherProps = otherProps || {};
  _otherProps.style = _style;
  if (direction == '水平排列') direction = 'hoz';
  if (direction == '垂直排列') direction = 'ver';
  var _React$useState = React.useState(defaultCheckedList),
    checkedList = _React$useState[0],
    setCheckedList = _React$useState[1];
  var _React$useState2 = React.useState(true),
    indeterminate = _React$useState2[0],
    setIndeterminate = _React$useState2[1];
  var _React$useState3 = React.useState(false),
    checkAll = _React$useState3[0],
    setCheckAll = _React$useState3[1];
  var onChange = function onChange(list) {
    setCheckedList(list);
    setIndeterminate(!!list.length && list.length < plainOptions.length);
    setCheckAll(list.length === plainOptions.length);
  };
  var onCheckAllChange = function onCheckAllChange(checked, e) {
    setCheckedList(e.target.checked ? defaultCheckedList : []);
    setIndeterminate(false);
    setCheckAll(e.target.checked);
  };
  if (allChecked) {
    return /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement(_Checkbox, {
      onChange: onCheckAllChange,
      checked: checkAll,
      indeterminate: indeterminate
    }, "\u5168\u9009"), /*#__PURE__*/React.createElement("div", {
      className: "divHeight"
    }), /*#__PURE__*/React.createElement(_Checkbox.Group, _extends({
      dataSource: plainOptions,
      value: checkedList,
      disabled: disabled,
      direction: direction,
      prefix: "next-",
      onChange: onChange
    }, otherProps)));
  } else {
    return /*#__PURE__*/React.createElement(_Checkbox.Group, _extends({
      dataSource: plainOptions,
      disabled: disabled,
      direction: direction,
      prefix: "next-"
    }, otherProps));
  }
};
Checklist.displayName = 'Checklist';
export default Checklist;