"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports["default"] = void 0;
var _button = _interopRequireDefault(require("antd/lib/button"));
var _divider = _interopRequireDefault(require("antd/lib/divider"));
var _row = _interopRequireDefault(require("antd/lib/row"));
var _col = _interopRequireDefault(require("antd/lib/col"));
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _checkbox = _interopRequireDefault(require("antd/lib/checkbox"));
var _objectWithoutPropertiesLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutPropertiesLoose"));
var React = _interopRequireWildcard(require("react"));
require("./index.less");
var _excluded = ["isGroup", "isControlAll", "isLinkGroup", "controlAllContet", "isGroupRow", "ColNum", "style", "ref", "children", "onChange", "disabled", "checked", "defaultChecked", "defaultCheckedChecked", "indeterminate", "Options", "autoFocus", "value"];
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; }
var antCheckbox = function antCheckbox(_ref) {
  var isGroup = _ref.isGroup,
    isControlAll = _ref.isControlAll,
    isLinkGroup = _ref.isLinkGroup,
    controlAllContet = _ref.controlAllContet,
    isGroupRow = _ref.isGroupRow,
    ColNum = _ref.ColNum,
    style = _ref.style,
    ref = _ref.ref,
    children = _ref.children,
    onChange = _ref.onChange,
    disabled = _ref.disabled,
    checked = _ref.checked,
    defaultChecked = _ref.defaultChecked,
    defaultCheckedChecked = _ref.defaultCheckedChecked,
    indeterminate = _ref.indeterminate,
    Options = _ref.Options,
    autoFocus = _ref.autoFocus,
    value = _ref.value,
    otherProps = (0, _objectWithoutPropertiesLoose2["default"])(_ref, _excluded);
  var _style = style || {};
  _style['width'] = _style['width'] ? _style['width'] : '300px';
  var _otherProps = otherProps || {};
  _otherProps.style = _style;
  // eslint-disable-next-line react-hooks/rules-of-hooks
  //单个Checked的value
  var _useState = (0, React.useState)(false),
    singleChecked = _useState[0],
    setSingleChecked = _useState[1];
  var _useState2 = (0, React.useState)(false),
    Indeterminate = _useState2[0],
    setIndeterminate = _useState2[1];
  var _useState3 = (0, React.useState)([]),
    CheckedList = _useState3[0],
    setCheckedList = _useState3[1];
  var _useState4 = (0, React.useState)(false),
    CheckAll = _useState4[0],
    setCheckAll = _useState4[1];
  //联动示例的------------------------------------
  var _useState5 = (0, React.useState)(true),
    checked01 = _useState5[0],
    setChecked01 = _useState5[1];
  var _useState6 = (0, React.useState)(false),
    disabled01 = _useState6[0],
    setDisabled01 = _useState6[1];
  //-------------------------------------------

  var defaultComponent = function defaultComponent() {
    //单选复选框属性
    var defaultProps = {
      style: style,
      ref: ref,
      // defaultChecked,
      disabled: disabled,
      autoFocus: autoFocus,
      // checked: singleChecked, //受控是否选中
      indeterminate: indeterminate,
      onChange: onChangeSelf
    };
    !checked ? defaultProps.defaultChecked = defaultChecked : defaultProps.checked = singleChecked;
    function onChangeSelf(e) {
      setSingleChecked(e.target.checked);
    }
    return /*#__PURE__*/React.createElement(_checkbox["default"], defaultProps, children);
  };
  //多选框
  var isGroupComponent = function isGroupComponent() {
    var isGroupProps = {
      disabled: disabled,
      onChange: onChange
      // style,
    };

    if ((value === null || value === void 0 ? void 0 : value[0]) !== '') {
      (value === null || value === void 0 ? void 0 : value.length) >= 0 ? isGroupProps.value = [].concat(value) : isGroupProps.defaultValue = defaultCheckedChecked !== undefined ? [].concat(defaultCheckedChecked) : [];
    } else {
      isGroupProps.defaultValue = [].concat(defaultCheckedChecked);
    }
    function isGroupRowComponent() {
      var Options01 = [];
      for (var i = 0, j = Options.length; i < j; i += ColNum) {
        Options01.push(Options.slice(i, i + ColNum));
      }
      var spanNum = 24 / ColNum;
      spanNum = Math.trunc(spanNum);
      function RowCom() {
        var extendsProps = {
          value: CheckedList,
          onChange: onCheck
        };
        var isGroupProps01 = {};
        if (isControlAll) {
          isGroupProps01 = (0, _extends2["default"])({}, isGroupProps, extendsProps);
        } else {
          isGroupProps01 = (0, _extends2["default"])({}, isGroupProps);
        }
        console.log(isGroupProps01, '----isGroupProps01');
        return /*#__PURE__*/React.createElement(_checkbox["default"].Group, (0, _extends2["default"])({
          style: {
            width: '100%'
          }
        }, isGroupProps01), Options01.map(function (item) {
          return /*#__PURE__*/React.createElement(_row["default"], null, item.map(function (i) {
            return /*#__PURE__*/React.createElement(_col["default"], {
              span: spanNum
            }, /*#__PURE__*/React.createElement(_checkbox["default"], {
              value: i.value
            }, i.label));
          }));
        }));
      }
      return /*#__PURE__*/React.createElement("div", {
        style: style
      }, isControlAll ? /*#__PURE__*/React.createElement("div", null, commandCom(), RowCom()) : RowCom());
    }
    function getEvalue() {
      //获取options的全部value
      var eValue = [];
      Options.forEach(function (item) {
        eValue.push(item === null || item === void 0 ? void 0 : item.value);
      });
      return eValue;
    }
    function onCheckAllChange(e) {
      var eValue = getEvalue();
      setCheckedList(e.target.checked ? eValue : []);
      setIndeterminate(false);
      setCheckAll(e.target.checked);
    }
    function onCheck(list) {
      var eValue = getEvalue();
      setCheckedList(list);
      setIndeterminate(!!list.length && list.length < eValue.length);
      setCheckAll(list.length === eValue.length);
    }
    function commandCom() {
      return /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement(_checkbox["default"], {
        indeterminate: Indeterminate,
        onChange: onCheckAllChange,
        checked: CheckAll
      }, controlAllContet), /*#__PURE__*/React.createElement(_divider["default"], null));
    }
    function isGroupSingleComponent() {
      isGroupProps.options = Options;
      console.log(isGroupProps, '-----------isGroupProps');
      return /*#__PURE__*/React.createElement("div", {
        style: style
      }, isControlAll ? /*#__PURE__*/React.createElement("div", null, commandCom(), /*#__PURE__*/React.createElement(_checkbox["default"].Group, (0, _extends2["default"])({}, isGroupProps, {
        value: CheckedList,
        onChange: onCheck
      }))) : /*#__PURE__*/React.createElement(_checkbox["default"].Group, isGroupProps));
    }
    return isGroupRow ? isGroupRowComponent() : isGroupSingleComponent();
  };
  //联动多选框示例
  var Example = function Example() {
    function toggleChecked() {
      setChecked01(!checked01);
    }
    ;
    function toggleDisable() {
      setDisabled01(!disabled01);
    }
    ;
    function onChange(e) {
      console.log('checked = ', e.target.checked);
      setChecked01(e.target.checked);
    }
    ;
    var label = (checked01 ? 'Checked' : 'Unchecked') + "-" + (disabled01 ? 'Disabled' : 'Enabled');
    return /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement("p", {
      style: {
        marginBottom: '20px'
      }
    }, /*#__PURE__*/React.createElement(_checkbox["default"], {
      checked: checked01,
      disabled: disabled01,
      onChange: onChange
    }, label)), /*#__PURE__*/React.createElement("p", null, /*#__PURE__*/React.createElement(_button["default"], {
      type: "primary",
      size: "small",
      onClick: toggleChecked
    }, !checked01 ? 'Check' : 'Uncheck'), /*#__PURE__*/React.createElement(_button["default"], {
      style: {
        margin: '0 10px'
      },
      type: "primary",
      size: "small",
      onClick: toggleDisable
    }, !disabled01 ? 'Disable' : 'Enable')));
  };
  return isLinkGroup ? Example() : isGroup ? isGroupComponent() : defaultComponent();
};
antCheckbox.displayName = 'antCheckbox';
var _default = antCheckbox;
exports["default"] = _default;