import _extends from "@babel/runtime/helpers/extends";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
var _excluded = ["checked", "disabled", "className", "size", "label", "children", "indeterminate", "onChange"];

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); 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 = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }

import { IconCheckmarkMediumOutline, IconCheckmarkSmallOutline, IconMinusSmallOutline } from '@teamleader/ui-icons';
import cx from 'classnames';
import omit from 'lodash.omit';
import React, { forwardRef } from 'react';
import Box, { omitBoxProps, pickBoxProps } from '../box';
import { TextBodyCompact, TextDisplay, TextSmall } from '../typography';
import theme from './theme.css';
var Checkbox = /*#__PURE__*/forwardRef(function (_ref, ref) {
  var _cx;

  var _ref$checked = _ref.checked,
      checked = _ref$checked === void 0 ? false : _ref$checked,
      _ref$disabled = _ref.disabled,
      disabled = _ref$disabled === void 0 ? false : _ref$disabled,
      className = _ref.className,
      _ref$size = _ref.size,
      size = _ref$size === void 0 ? 'medium' : _ref$size,
      label = _ref.label,
      children = _ref.children,
      _ref$indeterminate = _ref.indeterminate,
      indeterminate = _ref$indeterminate === void 0 ? false : _ref$indeterminate,
      onChange = _ref.onChange,
      others = _objectWithoutProperties(_ref, _excluded);

  var handleToggle = function handleToggle(event) {
    if (onChange) {
      onChange(event.target.checked, event);
    }
  };

  var TextElement = size === 'small' ? TextSmall : size === 'medium' ? TextBodyCompact : TextDisplay;
  var IconCheckmark = size === 'large' ? IconCheckmarkMediumOutline : IconCheckmarkSmallOutline;
  var restProps = omit(others, ['onChange']);

  var boxProps = _objectSpread(_objectSpread({}, pickBoxProps(restProps)), {}, {
    onClick: restProps.onClick
  });

  var inputProps = omitBoxProps(restProps);
  var classNames = cx(theme['checkbox'], theme["is-".concat(size)], (_cx = {}, _defineProperty(_cx, theme['is-checked'], checked || indeterminate), _defineProperty(_cx, theme['is-disabled'], disabled), _cx), className);
  return /*#__PURE__*/React.createElement(Box, _extends({
    element: "label",
    "data-teamleader-ui": "checkbox",
    className: classNames
  }, boxProps), /*#__PURE__*/React.createElement("input", _extends({
    className: theme['input'],
    type: "checkbox",
    checked: checked,
    disabled: disabled,
    onChange: handleToggle,
    ref: ref,
    readOnly: true
  }, inputProps)), /*#__PURE__*/React.createElement("span", {
    className: theme['shape']
  }, checked ? /*#__PURE__*/React.createElement(IconCheckmark, {
    className: theme['icon']
  }) : /*#__PURE__*/React.createElement(IconMinusSmallOutline, {
    className: theme['icon']
  })), (label || children) && /*#__PURE__*/React.createElement("span", {
    className: theme['label']
  }, label && /*#__PURE__*/React.createElement(TextElement, {
    element: "span",
    color: disabled ? 'neutral' : 'teal'
  }, label), children));
});
Checkbox.displayName = 'Checkbox';
export default Checkbox;