import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

import { createBEM } from '@zarm-design/bem';
import { Minus as MinusIcon, Success as SuccessIcon } from '@zarm-design/icons';
import includes from 'lodash/includes';
import React, { forwardRef, useContext, useEffect, useImperativeHandle, useRef, useState } from 'react';
import Button from '../button';
import { ConfigContext } from '../config-provider';
import List from '../list';
import { CheckboxGroupContext } from './context';

var getChecked = function getChecked(props, defaultChecked) {
  var _ref, _props$checked;

  return (_ref = (_props$checked = props.checked) !== null && _props$checked !== void 0 ? _props$checked : props.defaultChecked) !== null && _ref !== void 0 ? _ref : defaultChecked;
};

;
var Checkbox = /*#__PURE__*/forwardRef(function (props, ref) {
  var inputRef = useRef(null);

  var _useState = useState(getChecked(props, false)),
      _useState2 = _slicedToArray(_useState, 2),
      checked = _useState2[0],
      setChecked = _useState2[1];

  var disabled = props.disabled;
  var groupContext = useContext(CheckboxGroupContext);

  if (groupContext && props.value !== undefined) {
    checked = includes(groupContext.value, props.value);

    setChecked = function setChecked(changedChecked) {
      if (changedChecked) {
        groupContext.check(props.value);
      } else {
        groupContext.uncheck(props.value);
      }
    };

    disabled = disabled || groupContext.disabled;
  }

  var _useContext = useContext(ConfigContext),
      prefixCls = _useContext.prefixCls;

  var bem = createBEM('checkbox', {
    prefixCls: prefixCls
  });
  var cls = bem([{
    disabled: disabled,
    checked: checked && !props.indeterminate,
    untext: !props.children,
    indeterminate: props.indeterminate
  }, props.className]);

  var currentProps = _objectSpread(_objectSpread({}, props), {}, {
    checked: checked
  });

  var textRender = props.children && /*#__PURE__*/React.createElement("span", {
    className: bem('text')
  }, props.children);
  var iconRender = /*#__PURE__*/React.createElement("span", {
    className: bem('icon')
  }, props.renderIcon ? props.renderIcon(currentProps) : /*#__PURE__*/React.createElement("span", {
    className: bem('tick')
  }, props.indeterminate ? /*#__PURE__*/React.createElement(MinusIcon, null) : /*#__PURE__*/React.createElement(SuccessIcon, null)));
  var inputRender = /*#__PURE__*/React.createElement("input", {
    ref: inputRef,
    id: props.id,
    type: "checkbox",
    className: bem('input'),
    "aria-checked": checked,
    disabled: disabled,
    value: props.value,
    checked: checked,
    onChange: function onChange(e) {
      var _props$onChange;

      if (disabled) return;

      if (!('checked' in props)) {
        setChecked(e.target.checked);
      }

      (_props$onChange = props.onChange) === null || _props$onChange === void 0 ? void 0 : _props$onChange.call(props, e);
    }
  });
  useImperativeHandle(ref, function () {
    return {
      check: function check() {
        if (checked) return;
        inputRef.current.click();
      },
      uncheck: function uncheck() {
        if (!checked) return;
        inputRef.current.click();
      },
      toggle: function toggle() {
        inputRef.current.click();
      }
    };
  });
  useEffect(function () {
    if (props.checked === undefined) return;
    if (props.checked === checked) return;
    setChecked(getChecked({
      checked: props.checked,
      defaultChecked: props.defaultChecked
    }, false));
  }, [props.checked, props.defaultChecked]);

  if ((groupContext === null || groupContext === void 0 ? void 0 : groupContext.type) === 'button') {
    return /*#__PURE__*/React.createElement("label", {
      className: cls,
      style: props.style
    }, inputRender, /*#__PURE__*/React.createElement(Button, {
      disabled: disabled,
      theme: checked ? 'primary' : 'default',
      size: "xs",
      block: groupContext === null || groupContext === void 0 ? void 0 : groupContext.block,
      onClick: function onClick() {
        inputRef.current.click();
      }
    }, props.children));
  }

  if ((groupContext === null || groupContext === void 0 ? void 0 : groupContext.type) === 'list') {
    var tickRender = /*#__PURE__*/React.createElement(React.Fragment, null, inputRender, iconRender);
    return /*#__PURE__*/React.createElement(List.Item, {
      hasArrow: false,
      className: cls,
      style: props.style,
      prefix: (groupContext === null || groupContext === void 0 ? void 0 : groupContext.iconAlign) === 'before' ? tickRender : undefined,
      suffix: (groupContext === null || groupContext === void 0 ? void 0 : groupContext.iconAlign) === 'after' ? tickRender : undefined,
      title: textRender,
      onClick: !disabled ? function () {
        if (disabled) return;
        inputRef.current.click();
      } : undefined
    });
  }

  var contentRender = props.render ? props.render(currentProps) : /*#__PURE__*/React.createElement(React.Fragment, null, iconRender, textRender);
  return /*#__PURE__*/React.createElement("label", {
    className: cls,
    style: props.style
  }, inputRender, contentRender);
});
Checkbox.displayName = 'Checkbox';
Checkbox.defaultProps = {
  indeterminate: false
};
export default Checkbox;