function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }

import React, { useContext, useEffect } from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import is from 'is';

import Input from '../Input';
import Error from '../Input/error';
import Icon from '../../base/Icon';
import { InputContext } from '../Input/context';
import './style.css';

var CheckBox = function CheckBox(props) {
  var context = useContext(InputContext);
  var value = context.value;
  var icon = props.icon,
      label = props.label,
      disabled = props.disabled,
      required = props.required,
      id = props.id,
      defaultValue = props.defaultValue,
      onKeyDown = props.onKeyDown,
      onChange = props.onChange,
      tabIndex = props.tabIndex;


  useEffect(function () {
    context.updateState({ value: defaultValue });
  }, [defaultValue]);

  var handleClick = function handleClick(e) {
    e.persist();
    context.updateState({ value: !value ? props.value : !value }, function () {
      if (is.fn(onChange)) {
        onChange(e, context.getValue(), id);
      }
    });
    if (!!value && required) {
      context.updateState({ showError: true });
    } else {
      context.updateState({ showError: false });
    }
  };
  var checkboxClasses = classNames({
    'ma__input-checkbox': true,
    'ma__input-checkbox--disabled': disabled
  });
  var inputProps = {
    type: 'checkbox',
    id: id,
    value: props.value,
    checked: value === props.value,
    onClick: handleClick,
    tabIndex: tabIndex,
    disabled: disabled
  };

  if (is.fn(onKeyDown)) {
    inputProps.onKeyDown = onKeyDown;
  }

  return React.createElement(
    'span',
    { className: checkboxClasses },
    React.createElement('input', inputProps),
    icon && icon.name && icon,
    React.createElement(
      'label',
      { htmlFor: id, tabIndex: -1 },
      React.createElement(
        'span',
        null,
        label
      )
    )
  );
};

CheckBox.propTypes = process.env.NODE_ENV !== "production" ? {
  icon: PropTypes.shape(Icon.propTypes),
  label: PropTypes.string,
  disabled: PropTypes.bool,
  required: PropTypes.bool,
  id: PropTypes.string,
  value: PropTypes.string,
  onChange: PropTypes.func,
  onKeyDown: PropTypes.func,
  defaultValue: PropTypes.string,
  tabIndex: PropTypes.number
} : {};

var InputCheckBox = function InputCheckBox(props) {
  var icon = props.icon,
      label = props.label,
      onChange = props.onChange,
      onKeyDown = props.onKeyDown,
      value = props.value,
      tabIndex = props.tabIndex,
      inputProps = _objectWithoutProperties(props, ['icon', 'label', 'onChange', 'onKeyDown', 'value', 'tabIndex']);
  // Input and checkBox share the props.checked, props.id values.


  var checkBoxProps = {
    icon: icon,
    label: label,
    id: props.id,
    value: value,
    required: props.required,
    onChange: onChange,
    onKeyDown: onKeyDown,
    tabIndex: tabIndex,
    disabled: props.disabled,
    defaultValue: props.defaultValue
  };
  return React.createElement(
    Input,
    inputProps,
    React.createElement(CheckBox, checkBoxProps),
    React.createElement(Error, { id: props.id })
  );
};

InputCheckBox.propTypes = process.env.NODE_ENV !== "production" ? {
  /** Id of the input that the label is tied to and the value is associated with in the formContext. */
  id: PropTypes.string,
  /** Value of the input that is associated with in the formContext. (required) */
  value: PropTypes.string.isRequired,
  /** Default input value. */
  defaultValue: PropTypes.string,
  /** Tab index for the checkbox input. */
  tabIndex: PropTypes.number,
  /** Label for the checkbox input. */
  label: PropTypes.string,
  /** Icon that renders after the label. */
  icon: PropTypes.shape(Icon.propTypes),
  /** Custom callback function called when input checked value is changed. */
  onChange: PropTypes.func,
  /** Custom callback function called when a keyboard action is triggered. */
  onKeyDown: PropTypes.func,
  /** Whether the input is disabled. */
  disabled: PropTypes.bool,
  /** Whether checked is required. */
  required: PropTypes.bool,
  /** The label text for the input field, can be a string or a component */
  labelText: PropTypes.oneOfType([PropTypes.string, PropTypes.object]).isRequired,
  /** Pass array of classNames to input wrapper div */
  classes: PropTypes.arrayOf(PropTypes.string)
} : {};

export default InputCheckBox;