import _taggedTemplateLiteralLoose from "@babel/runtime/helpers/esm/taggedTemplateLiteralLoose";
import _extends from "@babel/runtime/helpers/esm/extends";
import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";

function _templateObject3() {
  var data = _taggedTemplateLiteralLoose(["checker"]);

  _templateObject3 = function _templateObject3() {
    return data;
  };

  return data;
}

function _templateObject2() {
  var data = _taggedTemplateLiteralLoose(["inner"]);

  _templateObject2 = function _templateObject2() {
    return data;
  };

  return data;
}

function _templateObject() {
  var data = _taggedTemplateLiteralLoose(["wrapper"]);

  _templateObject = function _templateObject() {
    return data;
  };

  return data;
}

import React, { useContext, useCallback } from 'react';
import PropTypes from 'prop-types';
import { useControlled, partitionHTMLProps, useClassNames, TypeChecker } from '../utils';
import { CheckboxGroupContext } from '../CheckboxGroup';
var defaultProps = {
  as: 'div',
  classPrefix: 'checkbox',
  checkable: true,
  tabIndex: 0,
  inputProps: {}
};
var Checkbox = /*#__PURE__*/React.forwardRef(function (props, ref) {
  var _useContext = useContext(CheckboxGroupContext),
      inlineContext = _useContext.inline,
      nameContext = _useContext.name,
      disabledContext = _useContext.disabled,
      readOnlyContext = _useContext.readOnly,
      plaintextContext = _useContext.plaintext,
      groupValue = _useContext.value,
      controlled = _useContext.controlled,
      onGroupChange = _useContext.onChange;

  var Component = props.as,
      controlledChecked = props.checked,
      className = props.className,
      children = props.children,
      classPrefix = props.classPrefix,
      checkable = props.checkable,
      defaultChecked = props.defaultChecked,
      title = props.title,
      inputRef = props.inputRef,
      inputProps = props.inputProps,
      indeterminate = props.indeterminate,
      tabIndex = props.tabIndex,
      _props$disabled = props.disabled,
      disabled = _props$disabled === void 0 ? disabledContext : _props$disabled,
      _props$readOnly = props.readOnly,
      readOnly = _props$readOnly === void 0 ? readOnlyContext : _props$readOnly,
      _props$plaintext = props.plaintext,
      plaintext = _props$plaintext === void 0 ? plaintextContext : _props$plaintext,
      _props$inline = props.inline,
      inline = _props$inline === void 0 ? inlineContext : _props$inline,
      _props$name = props.name,
      name = _props$name === void 0 ? nameContext : _props$name,
      value = props.value,
      onClick = props.onClick,
      onCheckboxClick = props.onCheckboxClick,
      onChange = props.onChange,
      rest = _objectWithoutPropertiesLoose(props, ["as", "checked", "className", "children", "classPrefix", "checkable", "defaultChecked", "title", "inputRef", "inputProps", "indeterminate", "tabIndex", "disabled", "readOnly", "plaintext", "inline", "name", "value", "onClick", "onCheckboxClick", "onChange"]);

  var isChecked = useCallback(function () {
    if (typeof groupValue !== 'undefined' && typeof value !== 'undefined') {
      return groupValue.some(function (i) {
        return i === value;
      });
    }

    return controlledChecked;
  }, [controlledChecked, groupValue, value]);

  var _useControlled = useControlled(isChecked(), defaultChecked),
      checked = _useControlled[0],
      setChecked = _useControlled[1];

  var _useClassNames = useClassNames(classPrefix),
      merge = _useClassNames.merge,
      prefix = _useClassNames.prefix,
      withClassPrefix = _useClassNames.withClassPrefix;

  var classes = merge(className, withClassPrefix({
    inline: inline,
    indeterminate: indeterminate,
    disabled: disabled,
    checked: checked
  }));

  var _partitionHTMLProps = partitionHTMLProps(rest),
      htmlInputProps = _partitionHTMLProps[0],
      restProps = _partitionHTMLProps[1];

  if (typeof controlled !== 'undefined') {
    // In uncontrolled situations, use defaultChecked instead of checked
    htmlInputProps[controlled ? 'checked' : 'defaultChecked'] = checked;
  }

  var handleChange = useCallback(function (event) {
    var nextChecked = !checked;

    if (disabled || readOnly) {
      return;
    }

    setChecked(nextChecked);
    onChange === null || onChange === void 0 ? void 0 : onChange(value, nextChecked, event);
    onGroupChange === null || onGroupChange === void 0 ? void 0 : onGroupChange(value, nextChecked, event);
  }, [checked, disabled, readOnly, setChecked, onChange, value, onGroupChange]);

  if (plaintext) {
    return checked ? /*#__PURE__*/React.createElement(Component, _extends({}, restProps, {
      ref: ref,
      className: classes
    }), children) : null;
  }

  var input = /*#__PURE__*/React.createElement("span", {
    className: prefix(_templateObject()),
    onClick: onCheckboxClick,
    "aria-disabled": disabled
  }, /*#__PURE__*/React.createElement("input", _extends({}, htmlInputProps, inputProps, {
    name: name,
    value: value,
    type: "checkbox",
    ref: inputRef,
    tabIndex: tabIndex,
    readOnly: readOnly,
    disabled: disabled,
    "aria-disabled": disabled,
    "aria-checked": indeterminate ? 'mixed' : checked,
    onClick: function onClick(event) {
      return event.stopPropagation();
    },
    onChange: handleChange
  })), /*#__PURE__*/React.createElement("span", {
    className: prefix(_templateObject2()),
    "aria-hidden": true,
    role: "presentation"
  }));
  return /*#__PURE__*/React.createElement(Component, _extends({}, restProps, {
    ref: ref,
    onClick: onClick,
    className: classes
  }), /*#__PURE__*/React.createElement("div", {
    className: prefix(_templateObject3())
  }, /*#__PURE__*/React.createElement("label", {
    title: title
  }, checkable ? input : null, children)));
});
Checkbox.displayName = 'Checkbox';
Checkbox.defaultProps = defaultProps;
Checkbox.propTypes = {
  as: PropTypes.elementType,
  title: PropTypes.string,
  className: PropTypes.string,
  inline: PropTypes.bool,
  disabled: PropTypes.bool,
  checked: PropTypes.bool,
  defaultChecked: PropTypes.bool,
  indeterminate: PropTypes.bool,
  onChange: PropTypes.func,
  onClick: PropTypes.func,
  inputProps: PropTypes.any,
  inputRef: TypeChecker.refType,
  value: PropTypes.any,
  children: PropTypes.node,
  classPrefix: PropTypes.string,
  tabIndex: PropTypes.number,
  checkable: PropTypes.bool,
  onCheckboxClick: PropTypes.func
};
export default Checkbox;