'use strict';

exports.__esModule = true;

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

var _classnames = require('classnames');

var _classnames2 = _interopRequireDefault(_classnames);

var _is = require('is');

var _is2 = _interopRequireDefault(_is);

var _Input = require('../Input');

var _Input2 = _interopRequireDefault(_Input);

var _error = require('../Input/error');

var _error2 = _interopRequireDefault(_error);

var _Icon = require('../../base/Icon');

var _Icon2 = _interopRequireDefault(_Icon);

var _context = require('../Input/context');

require('./style.css');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

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; }

var CheckBox = function CheckBox(props) {
  var context = (0, _react.useContext)(_context.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;


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

  var handleClick = function handleClick(e) {
    e.persist();
    context.updateState({ value: !value ? props.value : !value }, function () {
      if (_is2.default.fn(onChange)) {
        onChange(e, context.getValue(), id);
      }
    });
    if (!!value && required) {
      context.updateState({ showError: true });
    } else {
      context.updateState({ showError: false });
    }
  };
  var checkboxClasses = (0, _classnames2.default)({
    '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 (_is2.default.fn(onKeyDown)) {
    inputProps.onKeyDown = onKeyDown;
  }

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

CheckBox.propTypes = process.env.NODE_ENV !== "production" ? {
  icon: _propTypes2.default.shape(_Icon2.default.propTypes),
  label: _propTypes2.default.string,
  disabled: _propTypes2.default.bool,
  required: _propTypes2.default.bool,
  id: _propTypes2.default.string,
  value: _propTypes2.default.string,
  onChange: _propTypes2.default.func,
  onKeyDown: _propTypes2.default.func,
  defaultValue: _propTypes2.default.string,
  tabIndex: _propTypes2.default.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 _react2.default.createElement(
    _Input2.default,
    inputProps,
    _react2.default.createElement(CheckBox, checkBoxProps),
    _react2.default.createElement(_error2.default, { 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: _propTypes2.default.string,
  /** Value of the input that is associated with in the formContext. (required) */
  value: _propTypes2.default.string.isRequired,
  /** Default input value. */
  defaultValue: _propTypes2.default.string,
  /** Tab index for the checkbox input. */
  tabIndex: _propTypes2.default.number,
  /** Label for the checkbox input. */
  label: _propTypes2.default.string,
  /** Icon that renders after the label. */
  icon: _propTypes2.default.shape(_Icon2.default.propTypes),
  /** Custom callback function called when input checked value is changed. */
  onChange: _propTypes2.default.func,
  /** Custom callback function called when a keyboard action is triggered. */
  onKeyDown: _propTypes2.default.func,
  /** Whether the input is disabled. */
  disabled: _propTypes2.default.bool,
  /** Whether checked is required. */
  required: _propTypes2.default.bool,
  /** The label text for the input field, can be a string or a component */
  labelText: _propTypes2.default.oneOfType([_propTypes2.default.string, _propTypes2.default.object]).isRequired,
  /** Pass array of classNames to input wrapper div */
  classes: _propTypes2.default.arrayOf(_propTypes2.default.string)
} : {};

exports.default = InputCheckBox;
module.exports = exports['default'];