'use strict';

import _extends from "@babel/runtime/helpers/extends";
import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/objectWithoutPropertiesLoose";
import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import React from 'react';
import PropTypes from 'prop-types';
import { FormControl, isIndeterminate } from './FormControl';
import Checkbox from 'react-bootstrap/lib/Checkbox';
export var CheckboxControl =
/*#__PURE__*/
function (_FormControl) {
  _inheritsLoose(CheckboxControl, _FormControl);

  function CheckboxControl() {
    return _FormControl.apply(this, arguments) || this;
  }

  var _proto = CheckboxControl.prototype;

  _proto.getValue = function getValue() {
    return this.getInputDOMNode().checked;
  };

  _proto.inputConverter = function inputConverter(value) {
    return {
      checked: value
    };
  };

  _proto.outputConverter = function outputConverter(e) {
    return e.target.checked;
  };

  _proto.getInputDOMNode = function getInputDOMNode() {
    return this.input;
  };

  _proto.componentWillReceiveProps = function componentWillReceiveProps(newProps) {
    this.getInputDOMNode().indeterminate = isIndeterminate(newProps.checked);
  };

  _proto.componentDidMount = function componentDidMount() {
    this.componentWillReceiveProps(this.props);
    this.testAutomationIdHandler();
  };

  _proto.componentDidUpdate = function componentDidUpdate() {
    this.testAutomationIdHandler();
  };

  _proto.testAutomationIdHandler = function testAutomationIdHandler() {
    if (this.input && !this.props.hasFormGroupContainer) {
      this.input.parentNode.setAttribute('data-test-automation-id', this.props.testAutomationId);
    }
  };

  _proto.render = function render() {
    var _this = this;

    var _this$getPropsFromCon = this.getPropsFromContext(),
        checked = _this$getPropsFromCon.checked,
        readOnly = _this$getPropsFromCon.readOnly,
        disabled = _this$getPropsFromCon.disabled,
        children = _this$getPropsFromCon.children,
        hiddenLabel = _this$getPropsFromCon.hiddenLabel,
        removeName = _this$getPropsFromCon.removeName,
        props = _objectWithoutPropertiesLoose(_this$getPropsFromCon, ["checked", "readOnly", "disabled", "children", "hiddenLabel", "removeName"]);

    return React.createElement(Checkbox, _extends({
      disabled: readOnly || disabled,
      checked: checked,
      inputRef: function inputRef(ref) {
        return _this.input = ref;
      },
      "data-test-automation-id": "input"
    }, this.getSafeProps(props), {
      name: removeName ? null : props.name
    }), React.createElement("span", {
      "data-test-automation-id": "label",
      className: !!hiddenLabel ? 'sr-only' : null
    }, children));
  };

  return CheckboxControl;
}(FormControl);

_defineProperty(CheckboxControl, "propTypes", Object.assign({}, FormControl.propTypes, {
  removeName: PropTypes.bool
}));

_defineProperty(CheckboxControl, "defaultProps", Object.assign({}, FormControl.defaultProps, {
  testAutomationId: 'checkbox',
  removeName: false
}));