import _extends from "@babel/runtime/helpers/extends";
import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/objectWithoutPropertiesLoose";
import _Checkbox from "@alifd/next/es/checkbox";
var _excluded = ["style", "disabled", "checked", "direction", "dataSource", "value", "borderFlag", "allChecked", "defaultValue", "defaultChecked"];
import * as React from 'react';
import { useRef } from 'react';
// import './index.scss';
var CheckboxGroup = _Checkbox.Group;
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,
    _ref$borderFlag = _ref.borderFlag,
    borderFlag = _ref$borderFlag === void 0 ? false : _ref$borderFlag,
    allChecked = _ref.allChecked,
    defaultValue = _ref.defaultValue,
    defaultChecked = _ref.defaultChecked,
    otherProps = _objectWithoutPropertiesLoose(_ref, _excluded);
  var plainOptions = dataSource;
  var defaultCheckedList = [];
  if (dataSource) {
    dataSource.forEach(function (item, index) {
      defaultCheckedList.push(item.value);
    });
  }
  var _style = style || {};
  var _otherProps = otherProps || {};
  _otherProps.style = _style;
  var childRef = useRef(null);
  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) {
    console.log(childRef.current);
    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);
  };
  console.log(plainOptions, '数据源');
  if (allChecked) {
    return plainOptions.length == 0 ? /*#__PURE__*/React.createElement("div", null, "\u6682\u65E0\u6570\u636E") : /*#__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(CheckboxGroup, _extends({
      dataSource: plainOptions,
      value: checkedList,
      disabled: disabled,
      direction: direction,
      prefix: "next-",
      onChange: onChange,
      defaultValue: defaultValue
    }, otherProps)));
  } else {
    return !plainOptions || plainOptions.length == 0 ? /*#__PURE__*/React.createElement("div", null, "\u6682\u65E0\u6570\u636E") : /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement(CheckboxGroup, _extends({
      defaultValue: defaultValue,
      dataSource: plainOptions,
      disabled: disabled,
      direction: direction,
      prefix: "next-",
      onChange: onChange
    }, otherProps)));
  }
};
Checklist.displayName = 'Checklist';
export default Checklist;