import * as React from 'react';
import { View } from 'remax/one';
import { getPrefixCls } from './../common';
import Icon from '../icon';
var prefixCls = getPrefixCls('checkbox');

var Checkbox = function Checkbox(props) {
  var children = props.children,
      checked = props.checked,
      _props$color = props.color,
      color = _props$color === void 0 ? '#1890FF' : _props$color,
      value = props.value,
      extra = props.extra,
      style = props.style,
      onChange = props.onChange;

  var handleClick = function handleClick(e) {
    onChange === null || onChange === void 0 ? void 0 : onChange(!checked, e, value);
  };

  return /*#__PURE__*/React.createElement(View, {
    className: prefixCls,
    style: style
  }, /*#__PURE__*/React.createElement(View, {
    className: "".concat(prefixCls, "-container"),
    onTap: handleClick
  }, checked ? /*#__PURE__*/React.createElement(Icon, {
    type: "roundcheckfill",
    size: 24,
    color: color
  }) : /*#__PURE__*/React.createElement(Icon, {
    type: "round",
    size: 24,
    color: "#999"
  }), children ? /*#__PURE__*/React.createElement(View, {
    className: "".concat(prefixCls, "-container-label")
  }, children) : null), /*#__PURE__*/React.createElement(View, {
    className: "".concat(prefixCls, "-extra")
  }, extra));
};

var getCheckboxs = function getCheckboxs(children) {
  var value = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
  var onChange = arguments.length > 2 ? arguments[2] : undefined;

  var onGroupChange = function onGroupChange(checked, e, v) {
    var val = value || [];
    var newVal = (val === null || val === void 0 ? void 0 : val.includes(v)) ? val === null || val === void 0 ? void 0 : val.filter(function (i) {
      return i !== v;
    }) : val === null || val === void 0 ? void 0 : val.concat(v);
    var newValue = !Array.isArray(newVal) || newVal.length === 0 ? undefined : newVal;
    onChange === null || onChange === void 0 ? void 0 : onChange(newValue, e);
  };

  var checkboxs = React.Children.map(children, function (checkbox) {
    var _a, _b;

    var p = (checkbox === null || checkbox === void 0 ? void 0 : checkbox.props) || {};
    var checked = p.checked;

    if ((p.value || p.value === 0) && ((_b = (_a = value) === null || _a === void 0 ? void 0 : _a.includes) === null || _b === void 0 ? void 0 : _b.call(_a, p.value))) {
      checked = !checked;
    }

    return Object.assign(Object.assign({}, checkbox), {
      props: Object.assign(Object.assign({}, checkbox.props), {
        checked: checked,
        onChange: onGroupChange
      })
    });
  });
  return checkboxs;
};

Checkbox.Group = function (props) {
  var _props$value = props.value,
      value = _props$value === void 0 ? [] : _props$value,
      children = props.children,
      _props$direction = props.direction,
      direction = _props$direction === void 0 ? 'row' : _props$direction,
      onChange = props.onChange;
  var checkboxs = getCheckboxs(children, value, onChange);
  return /*#__PURE__*/React.createElement(View, {
    style: {
      display: 'flex',
      flexDirection: direction
    }
  }, checkboxs);
};

export default Checkbox; // 注意不要加 React.memo，会导致保错。