"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports["default"] = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _objectWithoutPropertiesLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutPropertiesLoose"));
var _checkbox = _interopRequireDefault(require("@alifd/next/lib/checkbox"));
var React = _interopRequireWildcard(require("react"));
var _excluded = ["style", "disabled", "checked", "direction", "dataSource", "value", "borderFlag", "allChecked", "defaultValue", "defaultChecked"];
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
// import './index.scss';
var CheckboxGroup = _checkbox["default"].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 = (0, _objectWithoutPropertiesLoose2["default"])(_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 = (0, React.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["default"], {
      onChange: onCheckAllChange,
      checked: checkAll,
      indeterminate: indeterminate
    }, "\u5168\u9009"), /*#__PURE__*/React.createElement("div", {
      className: "divHeight"
    }), /*#__PURE__*/React.createElement(CheckboxGroup, (0, _extends2["default"])({
      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, (0, _extends2["default"])({
      defaultValue: defaultValue,
      dataSource: plainOptions,
      disabled: disabled,
      direction: direction,
      prefix: "next-",
      onChange: onChange
    }, otherProps)));
  }
};
Checklist.displayName = 'Checklist';
var _default = Checklist;
exports["default"] = _default;